Esempio n. 1
0
 public function store($updateNulls = false)
 {
     $k = $this->_tbl_key;
     if ($this->_debug) {
         echo "\n" . get_class($this) . "::store\n";
         $this->_logtable(true, false);
     }
     if (empty($this->{$k})) {
         if ($this->_location_id >= 0) {
             if (!$this->_lock()) {
                 return false;
             }
             if ($this->_location_id == 0) {
                 $query = $this->_db->getQuery(true);
                 $query->select($this->_tbl_key . ', parent_id, level, lft, rgt');
                 $query->from($this->_tbl);
                 $query->where('parent_id = 0');
                 $query->order('lft DESC');
                 $this->_db->setQuery($query, 0, 1);
                 $reference = $this->_db->loadObject();
                 if ($this->_db->getErrorNum()) {
                     $e = new MException(MText::sprintf('MLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), $this->_db->getErrorMsg()));
                     $this->setError($e);
                     $this->_unlock();
                     return false;
                 }
                 if ($this->_debug) {
                     $this->_logtable(false);
                 }
             } else {
                 if (!($reference = $this->_getNode($this->_location_id))) {
                     $this->_unlock();
                     return false;
                 }
             }
             if (!($repositionData = $this->_getTreeRepositionData($reference, 2, $this->_location))) {
                 $this->_unlock();
                 return false;
             }
             $query = $this->_db->getQuery(true);
             $query->update($this->_tbl);
             $query->set('lft = lft + 2');
             $query->where($repositionData->left_where);
             $this->_runQuery($query, 'MLIB_DATABASE_ERROR_STORE_FAILED');
             $query = $this->_db->getQuery(true);
             $query->update($this->_tbl);
             $query->set('rgt = rgt + 2');
             $query->where($repositionData->right_where);
             $this->_runQuery($query, 'MLIB_DATABASE_ERROR_STORE_FAILED');
             $this->parent_id = $repositionData->new_parent_id;
             $this->level = $repositionData->new_level;
             $this->lft = $repositionData->new_lft;
             $this->rgt = $repositionData->new_rgt;
         } else {
             $e = new MException(MText::_('MLIB_DATABASE_ERROR_INVALID_PARENT_ID'));
             $this->setError($e);
             return false;
         }
     } else {
         if ($this->_location_id > 0) {
             if (!$this->moveByReference($this->_location_id, $this->_location, $this->{$k})) {
                 return false;
             }
         }
         if (!$this->_lock()) {
             return false;
         }
     }
     if (!parent::store($updateNulls)) {
         $this->_unlock();
         return false;
     }
     if ($this->_debug) {
         $this->_logtable();
     }
     $this->_unlock();
     return true;
 }