/** * Save term * * @param \Terms $term * @param $error * @return bool */ public function _save(\Terms $term, &$error) { $current_status = $term->getStatus(); $term->hydrate($this->post('term', 'ARRAY', array())); $parentId = $this->post('term_parent', 'INT', 0); if ($parentId == 0) { $parent = \Terms::retrieveRoot($term->taxonomy); } else { $parent = \Terms::retrieveById($parentId); if (!$parent) { $error['parent'] = array(t('Term parent not found with id:' . $parentId)); return false; } } $is_new = $term->isNew(); $term->beginTransaction(); try { if ($is_new) { // create new $term->setParentId($parent->getId()); $term->setStatus($parent->getStatus()); $term->insertAsLastChildOf($parent); //dispatch event create new $this->dispatch('onCreateNewCatalog', new CMSBackendEvent($this, ['term' => $term])); $this->_saveProperty($term, $error); } else { // save information $currentParent = $term->getParent(); if ($parent->getId() == $term->getId()) { //something f****d $error['parent'] = t('Could not move to child of itself!'); } if ($currentParent->getId() != $parent->getId()) { //moved category $term->moveToLastChildOf($parent); $term->setParentId($parent->getId()); $term->setStatus($parent->getStatus()); $this->_saveProperty($term, $error); if (!$term->save()) { //save information first $this->dispatch('onUpdateTerm', new CMSBackendEvent($this, ['term' => $term])); if (!$term->isValid()) { $failures = $term->getValidationFailures(); foreach ($failures as $failure) { $error[$failure->getColumn()] = $failure->getMessage(); } } } } else { //simply save information if ($term->save()) { $this->_saveProperty($term, $error); //dispatch event update $this->dispatch('onUpdateTerm', new CMSBackendEvent($this, ['term' => $term])); } else { if (!$term->isValid()) { $failures = $term->getValidationFailures(); foreach ($failures as $failure) { $error[$failure->getColumn()] = $failure->getMessage(); } } } if (!empty($error)) { $term->rollBack(); return false; } } //end simply save information if ($current_status != $term->getStatus() && $term->getStatus() == \Terms::STATUS_INACTIVE) { //change status to INACTIVE $term->changeDescendantsStatus(\Terms::STATUS_INACTIVE); } } if ($term->isValid()) { $term->commit(); return $term; } $term->rollBack(); } catch (\Exception $e) { $term->rollBack(); throw new $e(); } $failures = $term->getValidationFailures(); foreach ($failures as $failure) { $error[$failure->getColumn()] = $failure->getMessage(); } return false; }