コード例 #1
0
 public function postActivation(ConnectionInterface $con = null)
 {
     $database = new Database($con->getWrappedConnection());
     $database->insertSql(null, [__DIR__ . '/Config/thelia.sql']);
     // Add order-invoice.before-discount hook if not already defined
     if (null === HookQuery::create()->findOneByCode('order-invoice.before-discount')) {
         try {
             $hookEvent = new HookCreateEvent();
             $hookEvent->setCode('order-invoice.before-discount')->setType(TemplateDefinition::FRONT_OFFICE)->setNative(false)->setActive(true)->setLocale('en_US')->setTitle("Before discount code form block");
             $this->getDispatcher()->dispatch(TheliaEvents::HOOK_CREATE, $hookEvent);
             if ($hookEvent->hasHook()) {
                 // Assign module to this hook
                 $moduleHookEvent = new ModuleHookCreateEvent();
                 $moduleHookEvent->setModuleId($this->getModuleId())->setHookId($hookEvent->getHook()->getId())->setClassname('creditaccount.order_invoice.hook')->setMethod('orderInvoiceForm');
                 // Activate module hook
                 $this->getDispatcher()->dispatch(TheliaEvents::MODULE_HOOK_CREATE, $moduleHookEvent);
                 if ($moduleHookEvent->hasModuleHook()) {
                     $event = new ModuleHookToggleActivationEvent($moduleHookEvent->getModuleHook());
                     $this->getDispatcher()->dispatch(TheliaEvents::MODULE_HOOK_TOGGLE_ACTIVATION, $event);
                 }
             }
         } catch (\Exception $ex) {
             throw new TheliaProcessException(Translator::getInstance()->trans("Failed to put module in 'order-invoice.before-discount' hook (%err)", ['%err' => $ex->getMessage()]), $ex);
         }
     }
 }
コード例 #2
0
 public function setUp()
 {
     $stubContainer = $this->getMockBuilder('\\Symfony\\Component\\DependencyInjection\\ContainerInterface')->disableOriginalConstructor()->getMock();
     $this->action = new ModuleHook($stubContainer, $this->getMockEventDispatcher());
     $this->module = ModuleQuery::create()->findOneByActivate(1);
     $this->hook = HookQuery::create()->findOneByActivate(true);
 }
コード例 #3
0
ファイル: ModuleHook.php プロジェクト: alex63530/thelia
 protected function isHookActive($hook_id)
 {
     if (null !== ($hook = HookQuery::create()->findPk($hook_id))) {
         return $hook->getActivate();
     }
     return false;
 }
コード例 #4
0
ファイル: Hook.php プロジェクト: vigourouxjulien/thelia
 public function toggleActivation(HookToggleActivationEvent $event, $eventName, EventDispatcherInterface $dispatcher)
 {
     if (null !== ($hook = HookQuery::create()->findPk($event->getHookId()))) {
         $hook->setActivate(!$hook->getActivate())->save();
         $event->setHook($hook);
         $this->cacheClear($dispatcher);
     }
 }
コード例 #5
0
 protected function getHookChoices()
 {
     $choices = array();
     $hooks = HookQuery::create()->filterByActivate(true, Criteria::EQUAL)->joinWithI18n($this->translator->getLocale())->orderBy('HookI18n.title', Criteria::ASC)->find();
     /** @var Hook $hook */
     foreach ($hooks as $hook) {
         $choices[$hook->getId()] = $hook->getTitle() . ' (code ' . $hook->getCode() . ')';
     }
     return $choices;
 }
コード例 #6
0
 protected function getHookChoices()
 {
     $choices = array();
     $hooks = HookQuery::create()->filterByActivate(true, Criteria::EQUAL)->find();
     /** @var Hook $hook */
     foreach ($hooks as $hook) {
         $choices[$hook->getId()] = $hook->getTitle();
     }
     return $choices;
 }
コード例 #7
0
 public function checkCodeUnicity($code, ExecutionContextInterface $context)
 {
     $type = $context->getRoot()->getData()['type'];
     $query = HookQuery::create()->filterByCode($code)->filterByType($type);
     if ($this->form->has('id')) {
         $query->filterById($this->form->getRoot()->getData()['id'], Criteria::NOT_EQUAL);
     }
     if ($query->count() > 0) {
         $context->addViolation(Translator::getInstance()->trans("A Hook with code %name already exists. Please choose another code.", array('%name' => $code)));
     }
 }
コード例 #8
0
ファイル: ModuleHook.php プロジェクト: margery/thelia
 /**
  * Get the associated ChildHook object
  *
  * @param      ConnectionInterface $con Optional Connection object.
  * @return                 ChildHook The associated ChildHook object.
  * @throws PropelException
  */
 public function getHook(ConnectionInterface $con = null)
 {
     if ($this->aHook === null && $this->hook_id !== null) {
         $this->aHook = ChildHookQuery::create()->findPk($this->hook_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aHook->addModuleHooks($this);
            */
     }
     return $this->aHook;
 }
コード例 #9
0
ファイル: Hook.php プロジェクト: margery/thelia
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      ConnectionInterface $con
  * @return void
  * @throws PropelException
  * @see Hook::setDeleted()
  * @see Hook::isDeleted()
  */
 public function delete(ConnectionInterface $con = null)
 {
     if ($this->isDeleted()) {
         throw new PropelException("This object has already been deleted.");
     }
     if ($con === null) {
         $con = Propel::getServiceContainer()->getWriteConnection(HookTableMap::DATABASE_NAME);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = ChildHookQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             $con->commit();
             $this->setDeleted(true);
         } else {
             $con->commit();
         }
     } catch (Exception $e) {
         $con->rollBack();
         throw $e;
     }
 }
コード例 #10
0
ファイル: HookTest.php プロジェクト: margery/thelia
 public function testSearchByHookId()
 {
     $hook = HookQuery::create()->findOne();
     $this->baseTestSearchById($hook->getId());
 }
コード例 #11
0
 /**
  * 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();
         }
     }
 }
コード例 #12
0
ファイル: HookController.php プロジェクト: badelas/thelia
 /**
  * Load an existing object from the database
  */
 protected function getExistingObject()
 {
     $hook = HookQuery::create()->findPk($this->getRequest()->get('hook_id', 0));
     if (null !== $hook) {
         $hook->setLocale($this->getCurrentEditionLocale());
     }
     return $hook;
 }
コード例 #13
0
ファイル: HookTableMap.php プロジェクト: margery/thelia
 /**
  * Performs an INSERT on the database, given a Hook or Criteria object.
  *
  * @param mixed               $criteria Criteria or Hook object containing data that is used to create the INSERT statement.
  * @param ConnectionInterface $con the ConnectionInterface connection to use
  * @return mixed           The new primary key.
  * @throws PropelException Any exceptions caught during processing will be
  *         rethrown wrapped into a PropelException.
  */
 public static function doInsert($criteria, ConnectionInterface $con = null)
 {
     if (null === $con) {
         $con = Propel::getServiceContainer()->getWriteConnection(HookTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from Hook object
     }
     if ($criteria->containsKey(HookTableMap::ID) && $criteria->keyContainsValue(HookTableMap::ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . HookTableMap::ID . ')');
     }
     // Set the correct dbName
     $query = HookQuery::create()->mergeWith($criteria);
     try {
         // use transaction because $criteria could contain info
         // for more than one table (I guess, conceivably)
         $con->beginTransaction();
         $pk = $query->doInsert($con);
         $con->commit();
     } catch (PropelException $e) {
         $con->rollBack();
         throw $e;
     }
     return $pk;
 }
コード例 #14
0
ファイル: Module.php プロジェクト: zorn-v/thelia
 /**
  * Gets the number of ChildHook objects related by a many-to-many relationship
  * to the current object by way of the ignored_module_hook cross-reference table.
  *
  * @param      Criteria $criteria Optional query object to filter the query
  * @param      boolean $distinct Set to true to force count distinct
  * @param      ConnectionInterface $con Optional connection object
  *
  * @return int the number of related ChildHook objects
  */
 public function countHooks($criteria = null, $distinct = false, ConnectionInterface $con = null)
 {
     if (null === $this->collHooks || null !== $criteria) {
         if ($this->isNew() && null === $this->collHooks) {
             return 0;
         } else {
             $query = ChildHookQuery::create(null, $criteria);
             if ($distinct) {
                 $query->distinct();
             }
             return $query->filterByModule($this)->count($con);
         }
     } else {
         return count($this->collHooks);
     }
 }
コード例 #15
0
 /**
  * G<et a hook for a hook name (code) and a hook type. The hook should exists and be activated.
  *
  * @param string $hookName
  * @param int $hookType
  *
  * @return Hook|null
  */
 protected function getHook($hookName, $hookType)
 {
     $hook = HookQuery::create()->filterByCode($hookName)->filterByType($hookType)->findOne();
     if (null === $hook) {
         $this->logAlertMessage(sprintf("Hook %s is unknown.", $hookName));
         return null;
     }
     if (!$hook->getActivate()) {
         $this->logAlertMessage(sprintf("Hook %s is not activated.", $hookName), true);
     }
     return $hook;
 }
コード例 #16
0
ファイル: BaseModule.php プロジェクト: alex63530/thelia
 protected function createOrUpdateHook(array $hook, EventDispatcherInterface $dispatcher, $defaultLocale)
 {
     $hookModel = HookQuery::create()->filterByCode($hook["code"])->findOne();
     if ($hookModel === null) {
         $event = new HookCreateAllEvent();
     } else {
         $event = new HookUpdateEvent($hookModel->getId());
     }
     /**
      * Get used I18n variables
      */
     $locale = $defaultLocale;
     list($titles, $descriptions, $chapos) = $this->getHookI18nInfo($hook, $defaultLocale);
     /**
      * If the default locale exists
      * extract it to save it in create action
      *
      * otherwise take the first
      */
     if (isset($titles[$defaultLocale])) {
         $title = $titles[$defaultLocale];
         unset($titles[$defaultLocale]);
     } else {
         reset($titles);
         $locale = key($titles);
         $title = array_shift($titles);
     }
     $description = $this->arrayKeyPop($locale, $descriptions);
     $chapo = $this->arrayKeyPop($locale, $chapos);
     /**
      * Set data
      */
     $event->setBlock(isset($hook["block"]) && (bool) $hook["block"])->setLocale($locale)->setTitle($title)->setDescription($description)->setChapo($chapo)->setType($hook["type"])->setCode($hook["code"])->setNative(false)->setByModule(true)->setActive(isset($hook["active"]) && (bool) $hook["active"]);
     /**
      * Dispatch the event
      */
     $dispatcher->dispatch($hookModel === null ? TheliaEvents::HOOK_CREATE_ALL : TheliaEvents::HOOK_UPDATE, $event);
     return [$event->getHook(), $this->formatHookDataForI18n($titles, $descriptions, $chapos)];
 }
コード例 #17
0
ファイル: Hook.php プロジェクト: NandoKstroNet/thelia
 /**
  * {@inheritdoc}
  */
 public function buildModelCriteria()
 {
     $search = HookQuery::create();
     /* manage translations */
     $this->configureI18nProcessing($search, ['TITLE', 'CHAPO', 'DESCRIPTION']);
     $id = $this->getId();
     if (null !== $id) {
         $search->filterById($id, Criteria::IN);
     }
     $code = $this->getCode();
     if (null !== $code) {
         $search->filterByCode($code, Criteria::IN);
     }
     $hookType = $this->getHook_type();
     if (null !== $hookType) {
         $search->filterByType($hookType, Criteria::IN);
     }
     $exclude = $this->getExclude();
     if (!is_null($exclude)) {
         $search->filterById($exclude, Criteria::NOT_IN);
     }
     $active = $this->getActive();
     if ($active !== Type\BooleanOrBothType::ANY) {
         $search->filterByActivate($active ? 1 : 0, Criteria::EQUAL);
     }
     $orders = $this->getOrder();
     foreach ($orders as $order) {
         switch ($order) {
             case "id":
                 $search->orderById(Criteria::ASC);
                 break;
             case "id_reverse":
                 $search->orderById(Criteria::DESC);
                 break;
             case "alpha":
                 $search->addAscendingOrderByColumn('i18n_TITLE');
                 break;
             case "alpha_reverse":
                 $search->addDescendingOrderByColumn('i18n_TITLE');
                 break;
             case "code":
                 $search->orderByCode(Criteria::ASC);
                 break;
             case "code_reverse":
                 $search->orderByCode(Criteria::DESC);
                 break;
             case "manual":
                 $search->orderByPosition(Criteria::ASC);
                 break;
             case "manual_reverse":
                 $search->orderByPosition(Criteria::DESC);
                 break;
             case "enabled":
                 $search->orderByActivate(Criteria::ASC);
                 break;
             case "enabled_reverse":
                 $search->orderByActivate(Criteria::DESC);
                 break;
             case "native":
                 $search->orderByNative(Criteria::ASC);
                 break;
             case "native_reverse":
                 $search->orderByNative(Criteria::DESC);
                 break;
         }
     }
     return $search;
 }
コード例 #18
0
 /**
  * G<et a hook for a hook name (code) and a hook type. The hook should exists and be activated.
  *
  * @param string $hookName
  * @param int $hookType
  *
  * @return Hook|null
  */
 protected function getHook($hookName, $hookType)
 {
     $hook = HookQuery::create()->filterByCode($hookName)->filterByType($hookType)->findOne();
     if (null === $hook) {
         Tlog::getInstance()->addAlert(sprintf("Hook %s is unknown.", $hookName));
         return null;
     }
     if (!$hook->getActivate()) {
         Tlog::getInstance()->addAlert(sprintf("Hook %s is not activated.", $hookName));
         return null;
     }
     return $hook;
 }