From c024dccab31ac64f192eb23570fe403c2bbe4e75 Mon Sep 17 00:00:00 2001 From: Arunas Mazeika Date: Thu, 5 Jul 2018 13:02:22 +0200 Subject: [PATCH] Revert "Merge pull request #23 from joomlatools/feature/22-resource" This reverts commit feb01037282ec3bd6af68fbfa2e903112bab75ef, reversing changes made to 4ad206a7443494e543a477bbbb9a2cbfb3cea8ca. --- activity/interface.php | 7 --- activity/resource/interface.php | 46 -------------- controller/behavior/resourceable.php | 65 +++++++++++++++++--- model/entity/activity.php | 89 ++++------------------------ model/entity/resource.php | 41 +------------ 5 files changed, 69 insertions(+), 179 deletions(-) delete mode 100644 activity/resource/interface.php diff --git a/activity/interface.php b/activity/interface.php index 479ed80..e78dd73 100644 --- a/activity/interface.php +++ b/activity/interface.php @@ -152,11 +152,4 @@ public function setLocale($locale); * @return string The activity locale. */ public function getLocale(); - - /** - * Resource getter. - * - * @return ComActivitiesActivityResourceInterface|null - */ - public function getResource(); } \ No newline at end of file diff --git a/activity/resource/interface.php b/activity/resource/interface.php deleted file mode 100644 index f1f984e..0000000 --- a/activity/resource/interface.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @link http://github.com/joomlatools/joomlatools-framework-activities for the canonical source repository - */ - -/** - * Activity Resource Interface. - * - * @author Arunas Mazeika - * @package Koowa\Component\Activities - */ - -interface ComActivitiesActivityResourceInterface extends KDatabaseRowInterface -{ - /** - * Resource package getter. - * - * @return string - */ - public function getPackage(); - - /** - * Resource name getter. - * - * @return string - */ - public function getName(); - - /** - * Resource ID getter. - * - * @return mixed - */ - public function getId(); - - /** - * Resource UUID getter. - * - * @return string - */ - public function getUuid(); -} \ No newline at end of file diff --git a/controller/behavior/resourceable.php b/controller/behavior/resourceable.php index 24e9eb7..a2ef93d 100644 --- a/controller/behavior/resourceable.php +++ b/controller/behavior/resourceable.php @@ -32,13 +32,13 @@ public function __construct(KObjectConfig $config) { parent::__construct($config); - $this->_actions = KObjectConfig::unbox($config->actions); + $this->_actions = KObjectConfig::unbox($config->actions); } protected function _initialize(KObjectConfig $config) { $config->append(array( - 'actions' => array('delete') + 'actions' => array('delete') )); parent::_initialize($config); @@ -50,19 +50,70 @@ protected function _afterAdd(KControllerContextInterface $context) if ($entity instanceof ComActivitiesActivityInterface && $entity->getActivityObject()) { - if ($resource = $entity->getResource()) + $resource = $this->_getResource($entity); + + $controller = $this->_getController(); + + if (!in_array($entity->action, $this->_actions)) { - if (!in_array($entity->action, $this->_actions)) + $data = $this->_getData($entity); + + if (!$resource->isNew()) { - if ($resource->isNew() || $resource->isModified()) { - $resource->save(); + // Update resource if title changed. + if ($resource->title != $entity->title) { + $controller->id($resource->id)->edit($data); } } - else if (!$resource->isNew()) $resource->delete(); + else $controller->add($data); } + else if (!$resource->isNew()) $controller->id($resource->id)->delete(); } } + /** + * Resource getter. + * + * @param KModelEntityInterface $entity The entity to get the resource from + * + * @return KModelEntityInterface|null The resource + */ + protected function _getResource($entity) + { + $model = $this->_getController()->getModel(); + + $model->reset()->getState()->setValues(array( + 'package' => $entity->package, + 'name' => $entity->name, + 'resource_id' => $entity->row + )); + + return $model->fetch(); + } + + /** + * Entity data getter + * + * @param KModelEntityInterface $entity The entity to get data from + * + * @return array The entity data + */ + protected function _getData(KModelEntityInterface $entity) + { + $data = array( + 'package' => $entity->package, + 'name' => $entity->name, + 'resource_id' => $entity->row, + 'title' => $entity->title + ); + + if ($uuid = $entity->getActivityObject()->getUuid()) { + $data['uuid'] = $uuid; + } + + return $data; + } + /** * Resource controller getter. * diff --git a/model/entity/activity.php b/model/entity/activity.php index aeae03f..3c99816 100644 --- a/model/entity/activity.php +++ b/model/entity/activity.php @@ -71,13 +71,6 @@ class ComActivitiesModelEntityActivity extends KModelEntityRow implements ComAct */ protected $_locale; - /** - * Resources model - * - * @var string|KObjectIdentifierInterface|KModelInterface - */ - protected $_resources_model; - /** * Constructor. * @@ -87,12 +80,11 @@ public function __construct(KObjectConfig $config) { parent::__construct($config); - $this->_format = $config->format; - $this->_objects = KObjectConfig::unbox($config->objects); - $this->_object_table = $config->object_table; - $this->_object_column = $config->object_column; - $this->_translator = $config->translator; - $this->_resources_model = $config->resources_model; + $this->_format = $config->format; + $this->_objects = KObjectConfig::unbox($config->objects); + $this->_object_table = $config->object_table; + $this->_object_column = $config->object_column; + $this->_translator = $config->translator; } /** @@ -107,12 +99,11 @@ protected function _initialize(KObjectConfig $config) $data = $config->data; $config->append(array( - 'format' => '{actor} {action} {object.type} title {object}', - 'object_table' => $data->package . '_' . KStringInflector::pluralize($data->name), - 'object_column' => $data->package . '_' . $data->name . '_id', - 'translator' => 'com:activities.activity.translator', - 'objects' => array('actor', 'action', 'object', 'target', 'generator', 'provider'), - 'resources_model' => 'com:activities.model.resources' + 'format' => '{actor} {action} {object.type} title {object}', + 'object_table' => $data->package . '_' . KStringInflector::pluralize($data->name), + 'object_column' => $data->package . '_' . $data->name . '_id', + 'translator' => 'com:activities.activity.translator', + 'objects' => array('actor', 'action', 'object', 'target', 'generator', 'provider') )); parent::_initialize($config); @@ -154,7 +145,6 @@ public function save() { $this->setStatus(KDatabase::STATUS_FAILED); $this->setStatusMessage($this->getObject('translator')->translate('Missing required data')); - return false; } } @@ -334,7 +324,6 @@ public function getPropertyFormat() public function setLocale($locale) { $this->_locale = $locale; - return $this; } @@ -360,7 +349,6 @@ public function getLocale() public function setTranslator(ComActivitiesActivityTranslatorInterface $translator) { $this->_translator = $translator; - return $this; } @@ -801,61 +789,4 @@ protected function _getRoute($url) return $this->getObject('lib:dispatcher.router.route', array('url' => array('query' => $url))); } - - /** - * Resource getter. - * - * @return ComActivitiesModelEntityResource - */ - public function getResource() - { - $model = $this->_getResourcesModel(); - - $resource = $model->package($this->package)->name($this->name)->resource_id($this->row)->fetch(); - - if ($resource->isNew()) - { - $data = array( - 'package' => $this->package, - 'name' => $this->name, - 'resource_id' => $this->row, - 'title' => $this->title - ); - - if ($uuid = $this->getActivityObject()->getUuid()) { - $data['uuid'] = $uuid; - } - - $resource = $model->create($data); - } - else - { - if ($this->title != $resource->title) { - $resource->title = $this->title; - } - } - - return $resource; - } - - /** - * Resource model getter. - * - * @return KModelInterface - */ - protected function _getResourcesModel() - { - if (!$this->_resources_model instanceof KModelInterface) - { - $model = $this->getObject($this->_resources_model); - - if (!$model instanceof KModelInterface) { - throw new RuntimeException(sprintf('%s is not a valid model identifier', (string) $this->_resources_model)); - } else { - $this->_resources_model = $model; - } - } - - return $this->_resources_model; - } } diff --git a/model/entity/resource.php b/model/entity/resource.php index 00a1c15..1f4f4a6 100644 --- a/model/entity/resource.php +++ b/model/entity/resource.php @@ -13,45 +13,6 @@ * @author Arunas Mazeika * @package Koowa\Component\Activities */ -class ComActivitiesModelEntityResource extends KModelEntityRow implements ComActivitiesActivityResourceInterface +class ComActivitiesModelEntityResource extends KModelEntityRow { - /** - * Resource package getter. - * - * @return string - */ - public function getPackage() - { - return $this->package; - } - - /** - * Resource name getter. - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Resource ID getter. - * - * @return mixed - */ - public function getId() - { - return $this->id; - } - - /** - * Resource UUID getter. - * - * @return string - */ - public function getUuid() - { - return $this->uuid; - } } \ No newline at end of file