/** */ public function save() { /* initial org settings */ /* @var SPdb $db */ $db = SPFactory::db(); $this->nid = $this->createAlias(); $this->approved = Sobi::Can($this->type(), 'publish', 'own'); $db->transaction(); parent::save(); $properties = get_class_vars(__CLASS__); /* get database columns and their ordering */ $cols = $db->getColumns($this->_dbTable); $values = array(); /* and sort the properties in the same order */ foreach ($cols as $col) { $values[$col] = array_key_exists($col, $properties) ? $this->{$col} : ''; } Sobi::Trigger($this->name(), ucfirst(__FUNCTION__), array(&$values)); /* try to save */ try { $db->insertUpdate($this->_dbTable, $values); } catch (SPException $x) { $db->rollback(); Sobi::Error($this->name(), SPLang::e('CANNOT_SAVE_CATEGORY_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* insert relation */ try { $db->delete('spdb_relations', array('id' => $this->id, 'oType' => 'category')); if (!$this->position) { $db->select('MAX( position ) + 1', 'spdb_relations', array('pid' => $this->parent, 'oType' => 'category')); $this->position = (int) $db->loadResult(); if (!$this->position) { $this->position = 1; } } $db->insertUpdate('spdb_relations', array('id' => $this->id, 'pid' => $this->parent, 'oType' => 'category', 'position' => $this->position, 'validSince' => $this->validSince, 'validUntil' => $this->validUntil)); } catch (SPException $x) { $db->rollback(); Sobi::Error($this->name(), SPLang::e('CANNOT_SAVE_CATEGORY_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* if there was no errors, commit the database changes */ $db->commit(); SPFactory::cache()->purgeSectionVars()->deleteObj('category', $this->id)->deleteObj('category', $this->parent); /* trigger plugins */ Sobi::Trigger('afterSave', $this->name(), array(&$this)); }
/** * (non-PHPdoc) * @see Site/lib/models/SPDBObject#save() */ public function save($request = 'post') { $this->loadFields(Sobi::Section(), true); // Thu, Feb 19, 2015 12:12:47 - it should be actually "beforeSave" Sobi::Trigger($this->name(), 'Before' . ucfirst(__FUNCTION__), array($this->id)); /* save the base object data */ /* @var SPdb $db */ $db = SPFactory::db(); $db->transaction(); if (!$this->nid || SPRequest::task() == 'entry.clone') { $this->nid = SPRequest::string($this->nameField, null, false, $request); $this->nid = $this->createAlias(); $this->name = $this->nid; } if (Sobi::Cfg('entry.publish_limit', 0) && !defined('SOBI_ADM_PATH')) { SPRequest::set('entry_createdTime', 0, $request); SPRequest::set('entry_validSince', 0, $request); SPRequest::set('entry_validUntil', 0, $request); $this->validUntil = gmdate('Y-m-d H:i:s', time() + Sobi::Cfg('entry.publish_limit', 0) * 24 * 3600); } $preState = Sobi::Reg('object_previous_state'); parent::save($request); $nameField = $this->nameField(); /* get the fields for this section */ foreach ($this->fields as $field) { /* @var $field SPField */ try { if ($field->enabled('form', $preState['new'])) { $field->saveData($this, $request); } else { $field->finaliseSave($this, $request); } if ($field->get('id') == $nameField) { /* get the entry name */ $this->name = $field->getRaw(); /* save the nid (name id) of the field where the entry name is saved */ $this->nameField = $field->get('nid'); } } catch (SPException $x) { if (SPRequest::task() != 'entry.clone') { $db->rollback(); throw new SPException(SPLang::e('CANNOT_SAVE_FIELS_DATA', $x->getMessage())); } else { Sobi::Error($this->name(), SPLang::e('CANNOT_SAVE_FIELS_DATA', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } } } $values = array(); /* get categories */ $cats = Sobi::Reg('request_categories'); if (!count($cats)) { $cats = SPRequest::arr('entry_parent', SPFactory::registry()->get('request_categories', array()), $request); } /* by default it should be comma separated string */ if (!count($cats)) { $cats = SPRequest::string('entry_parent', null, $request); if (strlen($cats) && strpos($cats, ',')) { $cats = explode(',', $cats); foreach ($cats as $i => $cat) { $c = (int) trim($cat); if ($c) { $cats[$i] = $c; } else { unset($cats[$i]); } } } elseif (strlen($cats)) { $cats = array((int) $cats); } } if (is_array($cats) && count($cats)) { foreach ($cats as $i => $v) { if (!$v) { unset($cats[$i]); } } } if (is_array($cats) && count($cats)) { /* get the ordering in these categories */ try { $db->select('pid, MAX(position)', 'spdb_relations', array('pid' => $cats, 'oType' => 'entry'), null, 0, 0, false, 'pid'); $cPos = $db->loadAssocList('pid'); $currPos = $db->select(array('pid', 'position'), 'spdb_relations', array('id' => $this->id, 'oType' => 'entry'))->loadAssocList('pid'); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* set the right position */ foreach ($cats as $i => $cat) { $copy = 0; if (!$this->approved) { $copy = isset($this->categories[$cats[$i]]) ? 0 : 1; } else { $db->delete('spdb_relations', array('id' => $this->id, 'oType' => 'entry')); } if (isset($currPos[$cat])) { $pos = $currPos[$cat]['position']; } else { $pos = isset($cPos[$cat]) ? $cPos[$cat]['MAX(position)'] : 0; $pos++; } $values[] = array('id' => $this->id, 'pid' => $cats[$i], 'oType' => 'entry', 'position' => $pos, 'validSince' => $this->validSince, 'validUntil' => $this->validUntil, 'copy' => $copy); } try { $db->insertArray('spdb_relations', $values, true); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } } elseif (!count($this->categories)) { throw new SPException(SPLang::e('MISSING_CAT')); } /* trigger possible state changes */ if ($preState['approved'] != $this->approved) { if ($this->approved) { $this->approveFields(true); // it's being done by the method above - removing //Sobi::Trigger( $this->name(), 'AfterApprove', array( $this->id, $this->approved ) ); } } if ($preState['state'] != $this->state) { Sobi::Trigger($this->name(), 'AfterChangeState', array($this->id, $this->state)); } SPFactory::cache()->purgeSectionVars(); SPFactory::cache()->deleteObj('entry', $this->id); if (count($cats)) { foreach ($cats as $cat) { SPFactory::cache()->deleteObj('category', $cat); } } Sobi::Trigger($this->name(), 'After' . ucfirst($preState['new'] ? __FUNCTION__ : 'Update'), array(&$this)); }
/** */ public function save($update = false, $init = true) { /* @var SPdb $db */ $db =& SPFactory::db(); /* check nid */ if (!$update) { $c = 1; while ($c) { /* section name id has to be unique */ try { $db->select('COUNT(nid)', 'spdb_object', array('oType' => 'section', 'nid' => $this->nid)); $c = $db->loadResult(); if ($c > 0) { $this->nid = $this->nid . '_' . rand(0, 1000); } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } } } SPFactory::registry()->set('current_section', $this->id); $db->transaction(); parent::save(); /* case adding new section, define the default title field */ if (!$update && $init) { $field = SPFactory::Model('field', true); $fid = $field->saveNew(array('name' => 'Name', 'nid' => 'field_name', 'showIn' => 'both', 'fieldType' => 'inbox', 'enabled' => 1, 'required' => 1, 'editable' => 1, 'section' => $this->id, 'inSearch' => 1, 'searchMethod' => 'general', 'isFree' => 1, 'editLimit' => -1, 'withLabel' => 1)); $field = SPFactory::Model('field', true); $field->saveNew(array('name' => 'Category', 'nid' => 'field_category', 'showIn' => 'hidden', 'fieldType' => 'category', 'enabled' => 1, 'required' => 1, 'editable' => 1, 'section' => $this->id, 'inSearch' => 1, 'searchMethod' => 'select', 'isFree' => 1, 'editLimit' => -1, 'withLabel' => 1, 'method' => 'select', 'isPrimary' => true)); SPFactory::config()->saveCfg('entry.name_field', $fid)->saveCfg('list.entries_ordering', 'field_name')->saveCfg('template.icon_fonts_arr', array('font-awesome-3-local')); SPFactory::Controller('acl', true)->addNewRule($this->get('name'), array($this->id), array('section.access.valid', 'category.access.valid', 'entry.access.valid', 'entry.add.own', 'section.search.*'), array('visitor', 'registered'), 'Default permissions for the section "' . $this->get('name') . '"'); } /* insert relation */ try { $db->insertUpdate('spdb_relations', array('id' => $this->id, 'pid' => 0, 'oType' => 'section', 'position' => 1, 'validSince' => $this->validSince, 'validUntil' => $this->validUntil)); } catch (SPException $x) { $db->rollback(); Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* if there was no errors, commit the database changes */ $db->commit(); // if( !$update ) { // SPFactory::mainframe()->msg( Sobi::Txt( 'SEC.CREATED' ) ); // } SPFactory::cache()->cleanSection(); /* trigger plugins */ Sobi::Trigger('afterSave', $this->name(), array(&$this)); }