/** * Move the term to the given parent term, set position and change position of the terms on the same nesting level * @param AM_Model_Db_Term $oTerm * @param int $iTocTermParentId * @param type $iPosition */ public function moveTerm(AM_Model_Db_Term $oTerm, $iTocTermParentId, $iPosition) { $iTocTermParentId = 0 === $iTocTermParentId ? null : $iTocTermParentId; if ($oTerm->parent_term == $iTocTermParentId) { //We are moving term without the changing of parent term $aWhere = array('revision = ?' => $oTerm->revision); if ($oTerm->position < $iPosition) { //We are moving term down //Decrease the position of the term below the moving term $aWhere['position <= ?'] = $iPosition; $aWhere['position > ?'] = $oTerm->position; $sExpresion = 'position - 1'; } elseif ($oTerm->position > $iPosition) { //We are moving term up //Increase the position of the term below the new position of the moving term $aWhere['position >= ?'] = $iPosition; $aWhere['position < ?'] = $oTerm->position; $sExpresion = 'position + 1'; } else { return; } if (is_null($oTerm->parent_term)) { $aWhere['parent_term IS NULL'] = ''; } else { $aWhere['parent_term = ?'] = $oTerm->parent_term; } $this->update(array('position' => new Zend_Db_Expr($sExpresion)), $aWhere); } else { //Moving term to the other parent term //Decreasing the position of the term below the moving term $aWhere = array('revision = ?' => $oTerm->revision); $aWhere['position > ?'] = $oTerm->position; $sExpresion = 'position - 1'; if (is_null($oTerm->parent_term)) { $aWhere['parent_term IS NULL'] = ''; } else { $aWhere['parent_term = ?'] = $oTerm->parent_term; } $this->update(array('position' => new Zend_Db_Expr($sExpresion)), $aWhere); //Increase the position below the moved term in new parent term branch $aWhere = array('revision = ?' => $oTerm->revision); $aWhere['position >= ?'] = $iPosition; $sExpresion = 'position + 1'; if (is_null($iTocTermParentId)) { $aWhere['parent_term IS NULL'] = ''; } else { $aWhere['parent_term = ?'] = $iTocTermParentId; } $this->update(array('position' => new Zend_Db_Expr($sExpresion)), $aWhere); } $oTerm->parent_term = $iTocTermParentId; $oTerm->position = $iPosition; $oTerm->save(); }
/** * Creates toc term for current vocabulary * @param string $sTocItemName * @param AM_Model_Db_Revision $oRevision * @param int | null $iParentId * @return AM_Model_Db_Term */ public function createTocTerm($sTocItemName, AM_Model_Db_Revision $oRevision, $iParentId = null, $iPosition = 0) { $sTocItemName = trim(AM_Tools::filter_xss($sTocItemName)); $iParentId = 0 == $iParentId ? null : $iParentId; $oTocTerm = new AM_Model_Db_Term(); $oTocTerm->title = $sTocItemName; $oTocTerm->vocabulary = $this->id; $oTocTerm->revision = $oRevision->id; $oTocTerm->parent_term = $iParentId; $oTocTerm->position = $iPosition; $oTocTerm->updated = new Zend_Db_Expr('NOW()'); $oTocTerm->save(); return $oTocTerm; }