Esempio n. 1
0
 /**
  * Change a product template
  *
  * @param \Thelia\Core\Event\Template\TemplateUpdateEvent $event
  * @param $eventName
  * @param EventDispatcherInterface $dispatcher
  */
 public function update(TemplateUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
 {
     if (null !== ($template = TemplateQuery::create()->findPk($event->getTemplateId()))) {
         $template->setDispatcher($dispatcher)->setLocale($event->getLocale())->setName($event->getTemplateName())->save();
         $event->setTemplate($template);
     }
 }
Esempio n. 2
0
 protected function getUpdateEvent($formData)
 {
     $changeEvent = new TemplateUpdateEvent($formData['id']);
     // Create and dispatch the change event
     $changeEvent->setLocale($formData["locale"])->setTemplateName($formData['name']);
     // Add feature and attributes list
     return $changeEvent;
 }
Esempio n. 3
0
 public function import($startRecord = 0)
 {
     $count = 0;
     $errors = 0;
     $hdl = $this->t1db->query(sprintf("select * from rubrique order by parent asc limit %d, %d", intval($startRecord), $this->getChunkSize()));
     /*
             $hdl = $this->t1db
                 ->query(
                     sprintf(
                         "select * from rubrique order by parent asc limit %d, %d",
                         intval($startRecord),
                         $this->getChunkSize()
                     )
                 );
     */
     $image_import = new CategoryImageImport($this->dispatcher, $this->t1db);
     $document_import = new CategoryDocumentImport($this->dispatcher, $this->t1db);
     while ($hdl && ($rubrique = $this->t1db->fetch_object($hdl))) {
         echo "r={$rubrique->id}";
         $count++;
         try {
             $this->cat_corresp->getT2($rubrique->id);
             Tlog::getInstance()->warning("Category ID={$rubrique->id} already imported.");
             continue;
         } catch (ImportException $ex) {
             // Okay, the category was not imported.
         }
         try {
             $event = new CategoryCreateEvent();
             $idx = 0;
             $descs = $this->t1db->query_list("select * from rubriquedesc where rubrique = ? order by lang asc", array($rubrique->id));
             foreach ($descs as $objdesc) {
                 $lang = $this->getT2Lang($objdesc->lang);
                 // A title is required to create the rewritten URL
                 if (empty($objdesc->titre)) {
                     $objdesc->titre = sprintf("Untitled-%d-%s", $objdesc->id, $lang->getCode());
                 }
                 $parent = $rubrique->parent > 0 ? $rubrique->parent + 1000000 : 0;
                 if ($idx == 0) {
                     $event->setLocale($lang->getLocale())->setTitle($objdesc->titre)->setParent($parent)->setVisible($rubrique->ligne == 1 ? true : false);
                     $this->dispatcher->dispatch(TheliaEvents::CATEGORY_CREATE, $event);
                     $category_id = $event->getCategory()->getId();
                     // Update position
                     // ---------------
                     $update_position_event = new UpdatePositionEvent($category_id, UpdatePositionEvent::POSITION_ABSOLUTE, $rubrique->classement);
                     $this->dispatcher->dispatch(TheliaEvents::CATEGORY_UPDATE_POSITION, $update_position_event);
                     Tlog::getInstance()->info("Created category {$category_id} from {$objdesc->titre} ({$rubrique->id})");
                     $this->cat_corresp->addEntry($rubrique->id, $category_id);
                     // Create a product template if this categorie has declinaisons and/or caracteristiques
                     // ------------------------------------------------------------------------------------
                     $tpl_update = null;
                     $feature_list = $this->t1db->query_list("select caracteristique from rubcaracteristique where rubrique={$rubrique->id}");
                     $attribute_list = $this->t1db->query_list("select declinaison from rubdeclinaison where rubrique={$rubrique->id}");
                     if (!empty($attribute_list) || !empty($feature_list)) {
                         $tpl_create = new TemplateCreateEvent();
                         $tpl_create->setLocale($lang->getLocale())->setTemplateName($objdesc->titre . " (generated by import)");
                         $this->dispatcher->dispatch(TheliaEvents::TEMPLATE_CREATE, $tpl_create);
                         $tpl_id = $tpl_create->getTemplate()->getId();
                         $tpl_update = new TemplateUpdateEvent($tpl_id);
                         foreach ($attribute_list as $attr) {
                             try {
                                 $attribute_template = new AttributeTemplate();
                                 $id = $this->attr_corresp->getT2($attr->declinaison);
                                 $attribute_template->setAttributeId($id)->setTemplateId($tpl_id)->save();
                             } catch (\Exception $ex) {
                                 Tlog::getInstance()->addError("Failed to create template attribute for T1 caractéristique {$attr->declinaison} : ", $ex->getMessage());
                                 $errors++;
                             }
                         }
                         foreach ($feature_list as $feat) {
                             try {
                                 $feature_template = new FeatureTemplate();
                                 $id = $this->feat_corresp->getT2($feat->caracteristique);
                                 $feature_template->setFeatureId($id)->setTemplateId($tpl_id)->save();
                             } catch (\Exception $ex) {
                                 Tlog::getInstance()->addError("Failed to create template feature for T1 declinaison {$feat->caracteristique} : ", $ex->getMessage());
                                 $errors++;
                             }
                         }
                         $this->tpl_corresp->addEntry($rubrique->id, $tpl_id);
                     }
                     // Import related content
                     // ----------------------
                     $contents = $this->t1db->query_list("select * from contenuassoc where objet=? and type=0 order by classement", array($rubrique->id));
                     // type: 1 = produit, 0=rubrique
                     foreach ($contents as $content) {
                         try {
                             $content_event = new CategoryAddContentEvent($event->getCategory(), $this->content_corresp->getT2($content->contenu));
                             $this->dispatcher->dispatch(TheliaEvents::CATEGORY_ADD_CONTENT, $content_event);
                         } catch (\Exception $ex) {
                             Tlog::getInstance()->addError("Failed to create associated content {$content->contenu} for category {$category_id}: ", $ex->getMessage());
                             $errors++;
                         }
                     }
                     // Import images and documents
                     // ---------------------------
                     $image_import->importMedia($rubrique->id, $category_id);
                     $document_import->importMedia($rubrique->id, $category_id);
                 }
                 // Update the newly created category
                 $update_event = new CategoryUpdateEvent($category_id);
                 $update_event->setTitle($objdesc->titre)->setParent($parent)->setLocale($lang->getLocale())->setVisible($rubrique->ligne == 1 ? true : false)->setChapo($objdesc->chapo)->setDescription($objdesc->description)->setPostscriptum($objdesc->postscriptum);
                 $this->dispatcher->dispatch(TheliaEvents::CATEGORY_UPDATE, $update_event);
                 // Create a product template name in this language
                 // -----------------------------------------------
                 if ($tpl_update !== null) {
                     $tpl_update->setLocale($lang->getLocale())->setTemplateName($objdesc->titre . " (generated by import)");
                     $this->dispatcher->dispatch(TheliaEvents::TEMPLATE_UPDATE, $tpl_update);
                 }
                 // Update the rewritten URL
                 $this->updateRewrittenUrl($event->getCategory(), $lang->getLocale(), $objdesc->lang, "rubrique", "%id_rubrique={$rubrique->id}");
                 $idx++;
             }
         } catch (\Exception $ex) {
             Tlog::getInstance()->addError("Failed to import category ID={$rubrique->id}: ", $ex->getMessage());
             $errors++;
         }
     }
     return new ImportChunkResult($count, $errors);
 }