Ejemplo n.º 1
0
 /**
  * Saves the current item.
  * If id > 0 the item is updated, otherwise it is inserted into the DB.
  * Disabled fields are removed and field names mapped to table row names
  * according to table configuration.
  * @param string $tablename 
  * @return Returns TRUE on success or FALSE on failure.
  */
 protected function _save($tablename)
 {
     if ($tablename == '' || !array_key_exists($tablename, $this->tables)) {
         return FALSE;
     }
     $tabledata = $this->tables[$tablename];
     if (empty($tabledata['config']['dirty'])) {
         return TRUE;
     }
     $current_time = time();
     $current_author = $this->itemcfg['current_author'];
     // check and assign serialized fields
     foreach ($tabledata['serialized'] as $assigned_field => $subfieldvalues) {
         if (!empty($subfieldvalues)) {
             $tabledata['fields'][$assigned_field] = $this->_serialize($subfieldvalues);
         } else {
             $tabledata['fields'][$assigned_field] = '';
         }
     }
     // UPDATE
     if ($tabledata['fields']['id'] > 0) {
         $tabledata['fields']['lastmodified'] = $current_time;
         $tabledata['fields']['lastmodified_author'] = $current_author;
         $record = array();
         // map fields to rows and remove disabled fields
         foreach ($tabledata['fields'] as $field => $value) {
             if ($this->_isFieldDisabled($field, $tabledata['name']) == FALSE) {
                 $mappedfield = $this->mapFieldToRow($field, $tabledata['name']);
                 $fk_value = $this->_getForeignkeyValue($field, $tabledata['name']);
                 $record[$mappedfield] = $fk_value === FALSE ? $value : $fk_value;
             }
         }
         $this->db->AutoExecute($tabledata['name'], $record, 'UPDATE', $this->mapFieldToRow('id', $tabledata['name']) . " = '" . $tabledata['fields']['id'] . "'");
     } else {
         $tabledata['fields']['created'] = $current_time;
         $tabledata['fields']['lastmodified'] = $current_time;
         $tabledata['fields']['created_author'] = $current_author;
         $tabledata['fields']['lastmodified_author'] = $current_author;
         $tabledata['fields']['ip'] = $_SERVER['REMOTE_ADDR'];
         $record = array();
         // map fields to rows and remove disabled fields
         foreach ($tabledata['fields'] as $field => $value) {
             // id not needed
             if ($field != 'id' && $this->_isFieldDisabled($field, $tabledata['name']) == FALSE) {
                 $mappedfield = $this->mapFieldToRow($field, $tabledata['name']);
                 $fk_value = $this->_getForeignkeyValue($field, $tabledata['name']);
                 $record[$mappedfield] = $fk_value === FALSE ? $value : $fk_value;
             }
         }
         $this->db->AutoExecute($tabledata['name'], $record, 'INSERT');
         $this->tables[$tablename]['fields']['id'] = $this->db->Insert_ID();
     }
     unset($record);
     // reset dirty
     $this->tables[$tablename]['config']['dirty'] = array();
     return TRUE;
 }