/**
  * Moves the category.
  *
  * @author Alexander Girin
  * @param integer $newParentCatId the new category id,
  *        to which the category is moved
  * @param integer $cid the moved category id
  * @return
  */
 function moveCategory($newParentCatId, $cid)
 {
     global $application;
     $NewParentCatInfo = $this->fetchCategoryInfo($newParentCatId);
     $CatInfo = $this->fetchCategoryInfo($cid);
     $catobj = new CCategoryInfo($cid);
     $CurrentParentCatInfo = $catobj->getCategoryTagValue('parentid');
     if ($NewParentCatInfo == FALSE || $CatInfo == FALSE) {
         return;
     }
     $tables = $this->getTables();
     $c = $tables['categories']['columns'];
     # categories is a database table
     $deltaLevel = -($CatInfo['level'] - 1) + $NewParentCatInfo['level'];
     if ($NewParentCatInfo['left'] < $CatInfo['left'] && $NewParentCatInfo['right'] > $CatInfo['right'] && $NewParentCatInfo['level'] < $CatInfo['level'] - 1) {
         $query = new DB_Update('categories');
         $query->addUpdateExpression($c['level'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['level'] . sprintf('%+d', $deltaLevel), $c['level']));
         $query->addUpdateExpression($c['right'], $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . ($CatInfo['right'] + 1) . ' ' . DB_AND . ' ' . ($NewParentCatInfo['right'] - 1), $c['right'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['right'] . '+' . (($NewParentCatInfo['right'] - $CatInfo['right'] - $CatInfo['level'] + $NewParentCatInfo['level']) / 2 * 2 + $CatInfo['level'] - $NewParentCatInfo['level'] - 1), $c['right'])));
         $query->addUpdateExpression($c['left'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . ($CatInfo['right'] + 1) . ' ' . DB_AND . ' ' . ($NewParentCatInfo['right'] - 1), $c['left'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['left'] . '+' . (($NewParentCatInfo['right'] - $CatInfo['right'] - $CatInfo['level'] + $NewParentCatInfo['level']) / 2 * 2 + $CatInfo['level'] - $NewParentCatInfo['level'] - 1), $c['left'])));
         $query->WhereField($c['left'], DB_BETWEEN, "'" . ($NewParentCatInfo['left'] + 1) . "'" . ' ' . DB_AND . " '" . ($NewParentCatInfo['right'] - 1) . "'");
         $application->db->getDB_Result($query);
     } elseif ($NewParentCatInfo['left'] < $CatInfo['left']) {
         $query = new DB_Update('categories');
         $query->addUpdateExpression($c['level'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['level'] . sprintf('%+d', $deltaLevel), $c['level']));
         $query->addUpdateExpression($c['left'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $NewParentCatInfo['right'] . ' ' . DB_AND . ' ' . ($CatInfo['left'] - 1), $c['left'] . '+' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['left'] . '-' . ($CatInfo['left'] - $NewParentCatInfo['right']), $c['left'])));
         $query->addUpdateExpression($c['right'], $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . $NewParentCatInfo['right'] . ' ' . DB_AND . ' ' . $CatInfo['left'], $c['right'] . '+' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['right'] . '-' . ($CatInfo['left'] - $NewParentCatInfo['right']), $c['right'])));
         $query->WhereField($c['left'], DB_BETWEEN, "'" . $NewParentCatInfo['left'] . "'" . ' ' . DB_AND . " '" . $CatInfo['right'] . "'");
         $query->WhereOR();
         $query->WhereField($c['right'], DB_BETWEEN, "'" . $NewParentCatInfo['left'] . "'" . ' ' . DB_AND . " '" . $CatInfo['right'] . "'");
         $application->db->getDB_Result($query);
     } else {
         $query = new DB_Update('categories');
         $query->addUpdateExpression($c['level'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['level'] . sprintf('%+d', $deltaLevel), $c['level']));
         $query->addUpdateExpression($c['left'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['right'] . ' ' . DB_AND . ' ' . $NewParentCatInfo['right'], $c['left'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['left'] . '+' . ($NewParentCatInfo['right'] - $CatInfo['right'] - 1), $c['left'])));
         $query->addUpdateExpression($c['right'], $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . ($CatInfo['right'] + 1) . ' ' . DB_AND . ' ' . ($NewParentCatInfo['right'] - 1), $c['right'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['right'] . '+' . ($NewParentCatInfo['right'] - $CatInfo['right'] - 1), $c['right'])));
         $query->WhereField($c['left'], DB_BETWEEN, "'" . $CatInfo['left'] . "'" . ' ' . DB_AND . " '" . $NewParentCatInfo['right'] . "'");
         $query->WhereOR();
         $query->WhereField($c['right'], DB_BETWEEN, "'" . $CatInfo['left'] . "'" . ' ' . DB_AND . " '" . $NewParentCatInfo['right'] . "'");
         $application->db->getDB_Result($query);
     }
     modApiFunc('EventsManager', 'throwEvent', 'CategoryMoved', array('OLD_PARENT_CATEGORY_ID' => $CurrentParentCatInfo, 'NEW_PARENT_CATEGORY_ID' => $newParentCatId, 'CATEGORY_ID_LIST' => $cid));
 }