Пример #1
0
 /**
  * Saves value in data object.
  *
  * Called by the render when doing $render->getValues()
  * Uses the group parameter to decide where to store data.
  *
  * @param Zikula_Form_View $view  Reference to Form render object.
  * @param array            &$data Data object.
  *
  * @return void
  */
 public function saveValue(Zikula_Form_View $view, &$data)
 {
     if ($this->enableDBUtil && $this->dataBased) {
         if ($this->group == null) {
             $data['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue();
         } else {
             if (!array_key_exists($this->group, $data)) {
                 $data[$this->group] = array();
             }
             $data[$this->group]['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue();
         }
     } else {
         if ($this->enableDoctrine && $this->dataBased) {
             if ($this->group == null) {
                 $data['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField);
             } else {
                 if (!array_key_exists($this->group, $data)) {
                     $data[$this->group] = array();
                 }
                 $data[$this->group]['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField);
             }
         } else {
             if ($this->doctrine2) {
                 $entity = $view->get_template_vars($this->group);
                 // load category from db
                 $em = ServiceUtil::getService('doctrine.entitymanager');
                 $collection = $em->getClassMetadata(get_class($entity))->getFieldValue($entity, $this->dataField);
                 if (!$collection) {
                     $collection = new \Doctrine\Common\Collections\ArrayCollection();
                     $em->getClassMetadata(get_class($entity))->setFieldValue($entity, $this->dataField, $collection);
                 }
                 if (is_array($this->getSelectedValue())) {
                     $selectedValues = $this->getSelectedValue();
                 } else {
                     $selectedValues[] = $this->getSelectedValue();
                 }
                 $selectedValues = array_combine($selectedValues, $selectedValues);
                 foreach ($collection->getKeys() as $key) {
                     $entityCategory = $collection->get($key);
                     if ($entityCategory->getCategoryRegistryId() == $this->registryId) {
                         $categoryId = $entityCategory->getCategory()->getId();
                         if (isset($selectedValues[$categoryId])) {
                             unset($selectedValues[$categoryId]);
                         } else {
                             $collection->remove($key);
                         }
                     }
                 }
                 // we do NOT flush here, as the calling module is responsible for that (Guite)
                 //$em->flush();
                 foreach ($selectedValues as $selectedValue) {
                     $category = $em->find('Zikula_Doctrine2_Entity_Category', $selectedValue);
                     $class = $em->getClassMetadata(get_class($entity))->getAssociationTargetClass($this->dataField);
                     $collection->add(new $class($this->registryId, $category, $entity));
                 }
             } else {
                 parent::saveValue($view, $data);
             }
         }
     }
 }
Пример #2
0
 /**
  * Saves value in data object.
  *
  * Called by the render when doing $render->getValues()
  * Uses the group parameter to decide where to store data.
  *
  * @param Zikula_Form_View $view  Reference to Form render object.
  * @param array            &$data Data object.
  *
  * @return void
  */
 function saveValue(Zikula_Form_View $view, &$data)
 {
     if ($this->enableDBUtil && $this->dataBased) {
         if ($this->group == null) {
             $data['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue();
         } else {
             if (!array_key_exists($this->group, $data)) {
                 $data[$this->group] = array();
             }
             $data[$this->group]['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue();
         }
     } else {
         if ($this->enableDoctrine && $this->dataBased) {
             if ($this->group == null) {
                 $data['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField);
             } else {
                 if (!array_key_exists($this->group, $data)) {
                     $data[$this->group] = array();
                 }
                 $data[$this->group]['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField);
             }
         } else {
             if ($this->doctrine2) {
                 $entity = $view->get_template_vars($this->group);
                 // load category from db
                 $em = ServiceUtil::get('doctrine')->getEntityManager();
                 $category = $em->find('Zikula_Doctrine2_Entity_Category', $this->getSelectedValue());
                 $collection = $em->getClassMetadata(get_class($entity))->getFieldValue($entity, $this->dataField);
                 if (!$collection) {
                     $collection = new \Doctrine\Common\Collections\ArrayCollection();
                     $em->getClassMetadata(get_class($entity))->setFieldValue($entity, $this->dataField, $collection);
                 }
                 if ($collection->containsKey($this->registryId)) {
                     $collection->get($this->registryId)->setCategory($category);
                 } else {
                     $class = $em->getClassMetadata(get_class($entity))->getAssociationTargetClass($this->dataField);
                     $collection->set($this->registryId, new $class($this->registryId, $category, $entity));
                 }
             } else {
                 parent::saveValue($view, $data);
             }
         }
     }
 }
Пример #3
0
 /**
  * Pre-process relationship identifiers.
  *
  * @param Zikula_Form_View $view    Reference to Zikula_Form_View object.
  * @param array            &$params Parameters passed from the Smarty plugin function.
  *
  * @return void
  */
 protected function preprocessIdentifiers(Zikula_Form_View $view, &$params)
 {
     $entityData = isset($params['linkingItem']) ? $params['linkingItem'] : $view->get_template_vars('linkingItem');
     $alias = $this->id;
     $itemIds = array();
     $many = $this->selectionMode == 'multiple';
     if (isset($entityData[$alias])) {
         $relatedItems = $entityData[$alias];
         if (is_array($relatedItems) || is_object($relatedItems)) {
             if ($many) {
                 foreach ($relatedItems as $relatedItem) {
                     $itemIds[] = $this->createItemIdentifier($relatedItem);
                 }
             } else {
                 $itemIds[] = $this->createItemIdentifier($relatedItems);
             }
         }
         $this->preselectedItems = $relatedItems;
     }
     if (count($itemIds) > 0) {
         if ($this->selectionMode != 'multiple') {
             $entityData[$alias] = ModUtil::apiFunc($this->name, 'selection', 'getEntity', array('ot' => $alias, 'id' => $itemIds[0]));
         } else {
             $entityData[$alias] = ModUtil::apiFunc($this->name, 'selection', 'getEntities', array('ot' => $alias, 'idList' => $itemIds));
         }
     }
     $view->assign('linkingItem', $entityData);
 }