Esempio n. 1
0
 public function createModuleHook(ModuleHookCreateEvent $event)
 {
     $moduleHook = new ModuleHookModel();
     // todo: test if classname and method exists
     $moduleHook->setModuleId($event->getModuleId())->setHookId($event->getHookId())->setActive(false)->setClassname($event->getClassname())->setMethod($event->getMethod())->setModuleActive($this->isModuleActive($event->getModuleId()))->setHookActive($this->isHookActive($event->getHookId()))->setPosition($this->getLastPositionInHook($event->getHookId()))->setTemplates($event->getTemplates())->save();
     // Be sure to delete this module hook from the ignored module hook table
     IgnoredModuleHookQuery::create()->filterByHookId($event->getHookId())->filterByModuleId($event->getModuleId())->delete();
     $event->setModuleHook($moduleHook);
 }
Esempio n. 2
0
 /**
  * Exclude object from result
  *
  * @param   ChildModuleHook $moduleHook Object to remove from the list of results
  *
  * @return ChildModuleHookQuery The current query, for fluid interface
  */
 public function prune($moduleHook = null)
 {
     if ($moduleHook) {
         $this->addUsingAlias(ModuleHookTableMap::ID, $moduleHook->getId(), Criteria::NOT_EQUAL);
     }
     return $this;
 }
Esempio n. 3
0
 /**
  * Filter the query by a related \Thelia\Model\ModuleHook object
  *
  * @param \Thelia\Model\ModuleHook|ObjectCollection $moduleHook  the related object to use as filter
  * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return ChildHookQuery The current query, for fluid interface
  */
 public function filterByModuleHook($moduleHook, $comparison = null)
 {
     if ($moduleHook instanceof \Thelia\Model\ModuleHook) {
         return $this->addUsingAlias(HookTableMap::ID, $moduleHook->getHookId(), $comparison);
     } elseif ($moduleHook instanceof ObjectCollection) {
         return $this->useModuleHookQuery()->filterByPrimaryKeys($moduleHook->getPrimaryKeys())->endUse();
     } else {
         throw new PropelException('filterByModuleHook() only accepts arguments of type \\Thelia\\Model\\ModuleHook or Collection');
     }
 }
Esempio n. 4
0
 /**
  * @params ModuleHookModel $hook
  * @depends testUpdate
  */
 public function testDelete(ModuleHookModel $moduleHook)
 {
     $event = new ModuleHookDeleteEvent($moduleHook->getId());
     $event->setDispatcher($this->dispatcher);
     $this->action->deleteModuleHook($event);
     $deletedModuleHook = $event->getModuleHook();
     $this->assertInstanceOf('Thelia\\Model\\ModuleHook', $deletedModuleHook);
     $this->assertTrue($deletedModuleHook->isDeleted());
 }
Esempio n. 5
0
 /**
  * Returns the object ID from the object
  *
  * @param ModuleHook $object
  * @return int
  */
 protected function getObjectId($object)
 {
     return $object->getId();
 }
 /**
  * Create a new hook if the hook definition is valid.
  *
  * @param string               $class  the namespace of the class
  * @param \Thelia\Model\Module $module the module
  * @param string               $id     the service (hook) id
  * @param array                $attributes  the hook attributes
  *
  * @throws \InvalidArgumentException
  */
 protected function registerHook($class, $module, $id, $attributes)
 {
     if (!isset($attributes['event'])) {
         throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "hook.event_listener" tags.', $id));
     }
     $active = isset($attributes['active']) ? intval($attributes['active']) : 1;
     $attributes['active'] = 1 === $active;
     $attributes['templates'] = isset($attributes['templates']) ? strval($attributes['templates']) : '';
     $attributes['type'] = isset($attributes['type']) ? $this->getHookType($attributes['type']) : TemplateDefinition::FRONT_OFFICE;
     if (null === ($hook = $this->getHook($attributes['event'], $attributes['type']))) {
         return;
     }
     $attributes = $this->getMethodName($attributes);
     // test if method exists
     $validMethod = true;
     if (!$this->isValidHookMethod($class, $attributes['method'], $hook->getBlock())) {
         $validMethod = false;
     }
     // test if hook is already registered in ModuleHook
     $moduleHook = ModuleHookQuery::create()->filterByModuleId($module->getId())->filterByHook($hook)->filterByMethod($attributes['method'])->findOne();
     if (null === $moduleHook) {
         if (!$validMethod) {
             Tlog::getInstance()->addAlert(sprintf("Module [%s] could not be registered hook [%s], method [%s] is not reachable.", $module->getCode(), $attributes['event'], $attributes['method']));
             return;
         }
         // Assign the module to the hook only if it has not been "deleted"
         $ignoreCount = IgnoredModuleHookQuery::create()->filterByHook($hook)->filterByModuleId($module->getId())->count();
         if (0 === $ignoreCount) {
             // hook for module doesn't exist, we add it with default registered values
             $moduleHook = new ModuleHook();
             $moduleHook->setHook($hook)->setModuleId($module->getId())->setClassname($id)->setMethod($attributes['method'])->setActive($active)->setHookActive(true)->setModuleActive(true)->setPosition(ModuleHook::MAX_POSITION);
             if (isset($attributes['templates'])) {
                 $moduleHook->setTemplates($attributes['templates']);
             }
             $moduleHook->save();
         }
     } else {
         if (!$validMethod) {
             Tlog::getInstance()->addAlert(sprintf("Module [%s] could not use hook [%s], method [%s] is not reachable anymore.", $module->getCode(), $attributes['event'], $attributes['method']));
             $moduleHook->setHookActive(false)->save();
         } else {
             //$moduleHook->setTemplates($attributes['templates']);
             // Update hook if id was changed in the definition
             if ($moduleHook->getClassname() != $id) {
                 $moduleHook->setClassname($id);
             }
             $moduleHook->save();
         }
     }
 }
Esempio n. 7
0
 public function createModuleHook(ModuleHookCreateEvent $event)
 {
     $moduleHook = new ModuleHookModel();
     $moduleHook->setModuleId($event->getModuleId())->setHookId($event->getHookId())->setActive(false)->setModuleActive($this->isModuleActive($event->getModuleId()))->setHookActive($this->isHookActive($event->getHookId()))->setPosition($this->getLastPositionInHook($event->getHookId()))->save();
     $event->setModuleHook($moduleHook);
 }
 /**
  * Create a new hook if the hook definition is valid.
  *
  * @param string               $class  the namespace of the class
  * @param \Thelia\Model\Module $module the module
  * @param string               $id     the service (hook) id
  * @param array                $event  the event attributes
  *
  * @throws \InvalidArgumentException
  */
 protected function registerHook($class, $module, $id, $event)
 {
     $active = isset($event['active']) ? intval($event['active']) : 1;
     $active = 1 === $active;
     if (!isset($event['event'])) {
         throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "hook.event_listener" tags.', $id));
     }
     $type = isset($event['type']) ? $this->getHookType($event['type']) : TemplateDefinition::FRONT_OFFICE;
     $hook = HookQuery::create()->filterByCode($event['event'])->filterByType($type)->findOne();
     if (null === $hook) {
         Tlog::getInstance()->addAlert(sprintf("Hook %s is unknown.", $event['event']));
         return;
     }
     if (!$hook->getActivate()) {
         Tlog::getInstance()->addAlert(sprintf("Hook %s is not activated.", $event['event']));
         return;
     }
     if (!isset($event['method'])) {
         $callback = function ($matches) {
             return strtoupper($matches[0]);
         };
         $event['method'] = 'on' . preg_replace_callback(array('/(?<=\\b)[a-z]/i', '/[^a-z0-9]/i'), $callback, $event['event']);
         $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
     }
     // test if method exists
     if (!$this->isValidHookMethod($class, $event['method'], $hook->getBlock())) {
         return;
     }
     // test if hook is already registered in ModuleHook
     $moduleHook = ModuleHookQuery::create()->filterByModuleId($module)->filterByHook($hook)->filterByMethod($event['method'])->findOne();
     if (null === $moduleHook) {
         // Assign the module to the hook only if it has not been "deleted"
         $ignoreCount = IgnoredModuleHookQuery::create()->filterByHook($hook)->filterByModuleId($module)->count();
         if (0 === $ignoreCount) {
             // hook for module doesn't exist, we add it with default registered values
             $moduleHook = new ModuleHook();
             $moduleHook->setHook($hook)->setModuleId($module)->setClassname($id)->setMethod($event['method'])->setActive($active)->setHookActive(true)->setModuleActive(true)->setPosition(ModuleHook::MAX_POSITION)->save();
         }
     } else {
         // Update hook if id was changed in the definition
         if ($moduleHook->getClassname() != $id) {
             $moduleHook->setClassname($id)->save();
         }
     }
 }