/** */ protected function parseOrdering() { $order = Sobi::GetUserState('sections.order', 'order', 'name.asc'); $ord = $order; $dir = 'asc'; if (strstr($order, '.')) { $ord = explode('.', $ord); $dir = $ord[1]; $ord = $ord[0]; } if ($ord == 'position') { $ord = 'name'; } if ($ord == 'name') { /* @var SPdb $db */ $db =& SPFactory::db(); try { $db->select('id', 'spdb_language', array('oType' => 'section', 'sKey' => 'name', 'language' => Sobi::Lang()), 'sValue.' . $dir); $fields = $db->loadResultArray(); if (!count($fields) && Sobi::Lang() != Sobi::DefLang()) { $db->select('id', 'spdb_language', array('oType' => 'section', 'sKey' => 'name', 'language' => Sobi::DefLang()), 'sValue.' . $dir); $fields = $db->loadResultArray(); } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::WARNING, 500, __LINE__, __FILE__); return false; } if (count($fields)) { $fields = implode(',', $fields); $ord = "field( id, {$fields} )"; } else { $ord = 'id.' . $dir; } } else { $ord = isset($dir) && strlen($dir) ? $ord . '.' . $dir : $ord; } SPFactory::user()->setUserState('sections.order', $ord); return $ord; }
/** * Gets the data for a field and save it in the database * @param SPEntry $entry * @param string $request * @return bool */ public function saveData(&$entry, $request = 'POST') { if (!$this->enabled) { return false; } /* @var SPdb $db */ $db = SPFactory::db(); $save = $this->verify($entry, $request); $time = SPRequest::now(); $IP = SPRequest::ip('REMOTE_ADDR', 0, 'SERVER'); $uid = Sobi::My('id'); /* collect the needed params */ $params = array(); $params['publishUp'] = $entry->get('publishUp'); $params['publishDown'] = $entry->get('publishDown'); $params['fid'] = $this->fid; $params['sid'] = $entry->get('id'); $params['section'] = Sobi::Reg('current_section'); $params['lang'] = Sobi::Lang(); $params['enabled'] = $entry->get('state'); $params['baseData'] = $db->escape(SPConfig::serialize($save)); $params['approved'] = $entry->get('approved'); $params['confirmed'] = $entry->get('confirmed'); /* if it is the first version, it is new entry */ if ($entry->get('version') == 1) { $params['createdTime'] = $time; $params['createdBy'] = $uid; $params['createdIP'] = $IP; } $params['updatedTime'] = $time; $params['updatedBy'] = $uid; $params['updatedIP'] = $IP; $params['copy'] = !$entry->get('approved'); if (Sobi::My('id') == $entry->get('owner')) { --$this->editLimit; } $params['editLimit'] = $this->editLimit; /* save it */ try { /* Notices: * If it was new entry - insert * If it was an edit and the field wasn't filled before - insert * If it was an edit and the field was filled before - update * " ... " and changes are not autopublish it should be insert of the copy .... but * " ... " if a copy already exist it is update again * */ $db->insertUpdate('spdb_field_data', $params); } catch (SPException $x) { Sobi::Error(__CLASS__, SPLang::e('CANNOT_SAVE_DATA', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } /* if it wasn't edited in the default language, we have to try to insert it also for def lang */ if (Sobi::Lang() != Sobi::DefLang()) { $params['lang'] = Sobi::DefLang(); try { $db->insert('spdb_field_data', $params, true, true); } catch (SPException $x) { Sobi::Error(__CLASS__, SPLang::e('CANNOT_SAVE_DATA', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } } }
public function save(&$attr) { static $lang = null; static $defLang = null; if (!$lang) { $lang = Sobi::Lang(); $defLang = Sobi::DefLang(); } $file = SPRequest::file('spfieldsopts', 'tmp_name'); if ($file) { $data = parse_ini_file($file, true); } elseif (is_string($attr['options'])) { $data = parse_ini_string($attr['options'], true); } else { $data = null; } $options = $this->parseOptsFile($data); if (!count($options) && count($attr['options'])) { $p = 0; $hold = array(); foreach ($attr['options'] as $o) { if (is_numeric($o['id'])) { $o['id'] = $this->nid . '_' . $o['id']; } if (isset($o['id'])) { $i = 0; $oid = $o['id']; while (isset($hold[$oid])) { $oid = $o['id'] . '_' . ++$i; } $options[] = array('id' => $oid, 'name' => $o['name'], 'parent' => null, 'position' => ++$p); $hold[$oid] = $oid; } } } if (count($options)) { unset($attr['options']); $optionsArr = array(); $labelsArr = array(); $defLabelsArr = array(); $optsIds = array(); foreach ($options as $i => $option) { /* check for doubles */ foreach ($options as $pos => $opt) { if ($i == $pos) { continue; } if ($option['id'] == $opt['id']) { $option['id'] = $option['id'] . '_' . substr((string) microtime(), 2, 8) . rand(1, 100); SPFactory::message()->warning('FIELD_WARN_DUPLICATE_OPT_ID'); } } $optionsArr[] = array('fid' => $this->id, 'optValue' => $option['id'], 'optPos' => $option['position'], 'optParent' => $option['parent']); $defLabelsArr[] = array('sKey' => $option['id'], 'sValue' => $option['name'], 'language' => $defLang, 'oType' => 'field_option', 'fid' => $this->id); $labelsArr[] = array('sKey' => $option['id'], 'sValue' => $option['name'], 'language' => $lang, 'oType' => 'field_option', 'fid' => $this->id); $optsIds[] = $option['id']; } /* @var SPdb $db */ $db =& SPFactory::db(); /* try to delete the existing labels */ try { $db->delete('spdb_field_option', array('fid' => $this->id)); $db->delete('spdb_language', array('oType' => 'field_option', 'fid' => $this->id, '!sKey' => $optsIds)); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_DELETE_SELECTED_OPTIONS', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* insert new values */ try { $db->insertArray('spdb_field_option', $optionsArr); $db->insertArray('spdb_language', $labelsArr, true); if ($defLang != $lang) { $db->insertArray('spdb_language', $defLabelsArr, false, true); } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_STORE_FIELD_OPTIONS_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } } if (!isset($attr['params'])) { $attr['params'] = array(); } $myAttr = $this->getAttr(); $properties = array(); if (count($myAttr)) { foreach ($myAttr as $property) { $properties[$property] = isset($attr[$property]) ? $attr[$property] : null; } } $attr['params'] = $properties; $this->sets['field.options'] = SPFactory::Instance('types.array')->toINIString($data); }
/** * Static function to create the right SQL-Query if a entries list should be sorted by this field * @param string $tables - table or tables join * @param array $conditions - array with conditions * @param string $oPrefix * @param string $eOrder * @param string $eDir * @return void */ public static function sortBy(&$tables, &$conditions, &$oPrefix, &$eOrder, $eDir) { /* @var SPdb $db */ $db =& SPFactory::db(); $tables = $db->join(array(array('table' => 'spdb_field_option_selected', 'as' => 'sdata', 'key' => 'fid'), array('table' => 'spdb_object', 'as' => 'spo', 'key' => array('sdata.sid', 'spo.id')), array('table' => 'spdb_field_data', 'as' => 'fdata', 'key' => array('fdata.fid', 'sdata.fid')), array('table' => 'spdb_field', 'as' => 'fdef', 'key' => array('fdef.fid', 'sdata.fid')), array('table' => 'spdb_language', 'as' => 'ldata', 'key' => array('sdata.optValue', 'ldata.sKey')), array('table' => 'spdb_relations', 'as' => 'sprl', 'key' => array('spo.id', 'sprl.id')))); $oPrefix = 'spo.'; $conditions['spo.oType'] = 'entry'; if (!isset($conditions['sprl.pid'])) { $conditions['sprl.pid'] = SPRequest::sid(); } $conditions['ldata.oType'] = 'field_option'; $conditions['fdef.nid'] = $eOrder; $eOrder = 'sValue.' . $eDir . ", field( language, '" . Sobi::Lang(false) . "', '" . Sobi::DefLang() . "' )"; return true; }
public function save(&$attr) { static $lang = null; static $defLang = null; if (!$lang) { $lang = Sobi::Lang(); $defLang = Sobi::DefLang(); } $file = SPRequest::file('spfieldsopts', 'tmp_name'); if ($file) { $data = parse_ini_file($file, true); } elseif (is_string($attr['options'])) { $data = parse_ini_string($attr['options'], true); } else { $data = null; } $options = $this->parseOptsFile($data); if (!count($options) && count($attr['options'])) { $p = 0; $hold = array(); foreach ($attr['options'] as $o) { if (is_numeric($o['id'])) { $o['id'] = $this->nid . '_' . $o['id']; } if (isset($o['id'])) { $i = 0; $oid = $o['id']; while (isset($hold[$oid])) { $oid = $o['id'] . '_' . ++$i; } $options[] = array('id' => $oid, 'name' => $o['name'], 'parent' => null, 'position' => ++$p); $hold[$oid] = $oid; } } } if (count($options)) { unset($attr['options']); $optionsArr = array(); $labelsArr = array(); $optsIds = array(); $defLabelsArr = array(); $duplicates = false; foreach ($options as $i => $option) { /* check for doubles */ foreach ($options as $pos => $opt) { if ($i == $pos) { continue; } if ($option['id'] == $opt['id']) { $option['id'] = $option['id'] . '_' . substr((string) microtime(), 2, 8) . rand(1, 100); $duplicates = true; } } $optionsArr[] = array('fid' => $this->id, 'optValue' => $option['id'], 'optPos' => $option['position'], 'optParent' => $option['parent']); $defLabelsArr[] = array('sKey' => $option['id'], 'sValue' => $option['name'], 'language' => $defLang, 'oType' => 'field_option', 'fid' => $this->id); $labelsArr[] = array('sKey' => $option['id'], 'sValue' => $option['name'], 'language' => $lang, 'oType' => 'field_option', 'fid' => $this->id); $optsIds[] = $option['id']; } if ($duplicates) { SPFactory::message()->warning('FIELD_WARN_DUPLICATE_OPT_ID'); } $db = SPFactory::db(); /* try to delete the existing labels */ try { $db->delete('spdb_field_option', array('fid' => $this->id)); $db->delete('spdb_language', array('oType' => 'field_option', 'fid' => $this->id, '!sKey' => $optsIds)); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_STORE_FIELD_OPTIONS_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* insert new values */ try { $db->insertArray('spdb_field_option', $optionsArr); $db->insertArray('spdb_language', $labelsArr, true); if ($defLang != $lang) { $db->insertArray('spdb_language', $defLabelsArr, false, true); } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_DELETE_SELECTED_OPTIONS', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } } if (!isset($attr['params'])) { $attr['params'] = array(); } $myAttr = $this->getAttr(); $properties = array(); if (count($myAttr)) { foreach ($myAttr as $property) { $properties[$property] = isset($attr[$property]) ? $attr[$property] : null; } } $this->sets['field.options'] = SPFactory::Instance('types.array')->toINIString($data); /** handle upload of new definition file */ $XMLFile = SPRequest::file('select-list-dependency', 'tmp_name'); if ($XMLFile && file_exists($XMLFile)) { $XMLFileName = SPRequest::file('select-list-dependency', 'name'); if (SPFs::getExt($XMLFileName) == 'zip') { $arch = SPFactory::Instance('base.fs.archive'); $name = str_replace('.zip', null, $XMLFileName); $path = SPLoader::dirPath('tmp.install.' . $name, 'front', false); $c = 0; while (SPFs::exists($path)) { $path = SPLoader::dirPath('tmp.install.' . $name . '_' . ++$c, 'front', false); } $arch->upload($XMLFile, $path . '/' . $XMLFileName); $arch->extract($path); $files = scandir($path); if (count($files)) { foreach ($files as $defFile) { switch (SPFs::getExt($defFile)) { case 'xml': $properties['dependencyDefinition'] = $defFile; SPFs::move($path . '/' . $defFile, SOBI_PATH . '/etc/fields/select-list/' . $defFile); break; case 'ini': $defLang = explode('.', $defFile); $defLang = $defLang[0]; if (file_exists(SOBI_ROOT . '/language/' . $defLang)) { SPFs::move($path . '/' . $defFile, SOBI_ROOT . '/language/' . $defLang . '/' . $defFile); } break; } } } } elseif (SPFs::getExt($XMLFileName) == 'xml') { if (SPFs::upload($XMLFile, SOBI_PATH . '/etc/fields/select-list/' . $XMLFileName)) { $properties['dependencyDefinition'] = $XMLFileName; } } } /** if we use it - let's transform the XML file */ if ($properties['dependency'] && $properties['dependencyDefinition']) { $this->parseDependencyDefinition($properties['dependencyDefinition']); } $attr['params'] = $properties; }
/** * Adding new field * Save base data and redirect to the edit function when the field type has been chosed * @return integer */ public function saveNew($attr) { $db = SPFactory::db(); /* cast all needed data and clean - it is possible just in admin panel but "strzeżonego pan Bóg strzeże" ;-) */ $base = array(); $base['section'] = isset($attr['section']) && $attr['section'] ? $attr['section'] : SPRequest::sid(); if (isset($attr['name'])) { $base['name'] = $db->escape($attr['name']); } if (isset($attr['description'])) { $base['description'] = $db->escape($attr['description']); } else { $base['description'] = null; } if (isset($attr['suffix'])) { $base['suffix'] = $db->escape($attr['suffix']); } else { $base['suffix'] = null; } if (isset($attr['nid'])) { $base['nid'] = $this->nid($db->escape(preg_replace('/[^[:alnum:]\\-\\_]/', null, $attr['nid'])), true); } if (isset($attr['cssClass'])) { $base['cssClass'] = $db->escape(preg_replace('/[^[:alnum:]\\-\\_ ]/', null, $attr['cssClass'])); } if (isset($attr['notice'])) { $base['notice'] = $db->escape($attr['notice']); } if (isset($attr['showIn'])) { $base['showIn'] = $db->escape(preg_replace('/[^[:alnum:]\\.\\-\\_]/', null, $attr['showIn'])); } if (isset($attr['fieldType'])) { $base['fieldType'] = preg_replace('/[^[:alnum:]\\.\\-\\_]/', null, $attr['fieldType']); } if (isset($attr['type'])) { $base['fieldType'] = preg_replace('/[^[:alnum:]\\.\\-\\_]/', null, $attr['type']); } if (isset($attr['description'])) { $base['description'] = $db->escape($attr['description']); } if (isset($attr['enabled'])) { $base['enabled'] = (int) $attr['enabled']; } if (isset($attr['required'])) { $base['required'] = (int) $attr['required']; } if (isset($attr['adminField'])) { $base['adminField'] = (int) $attr['adminField']; } if (isset($attr['adminField']) && $attr['adminField']) { $attr['required'] = false; } if (isset($attr['editable'])) { $base['editable'] = (int) $attr['editable']; } if (isset($attr['editLimit'])) { $base['editLimit'] = (int) $attr['editLimit']; $base['editLimit'] = $base['editLimit'] > 0 ? $base['editLimit'] : -1; } if (isset($attr['isFree'])) { $base['isFree'] = (int) $attr['isFree']; } if (isset($attr['withLabel'])) { $base['withLabel'] = (int) $attr['withLabel']; } if (isset($attr['inSearch'])) { $base['inSearch'] = (int) $attr['inSearch']; } if (isset($attr['admList'])) { $base['admList'] = (int) $attr['admList']; } if (isset($attr['fee'])) { $base['fee'] = (double) $attr['fee']; } if (isset($attr['section'])) { $base['section'] = (int) $attr['section']; } $base['version'] = 1; /* determine the right position */ try { $db->select('MAX( position )', 'spdb_field', array('section' => SPRequest::sid())); $base['position'] = (int) $db->loadResult() + 1; if (!$base['position']) { $base['position'] = 1; } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_GET_FIELD_POSITION_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* get database columns and their ordering */ $cols = $db->getColumns('spdb_field'); $values = array(); /* and sort the properties in the same order */ foreach ($cols as $col) { $values[$col] = array_key_exists($col, $base) ? $base[$col] : ''; } /* save new field */ try { $db->insert('spdb_field', $values); $this->fid = $db->insertid(); } catch (SPException $x) { Sobi::Error($this->name(), $x->getMessage(), SPC::ERROR, 500, __LINE__, __FILE__); } /* save language depend properties */ $labels = array(); $defLabels = array(); $labels[] = array('sKey' => 'name', 'sValue' => $base['name'], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid); $labels[] = array('sKey' => 'description', 'sValue' => $base['description'], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid); $labels[] = array('sKey' => 'suffix', 'sValue' => $base['suffix'], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid); if (Sobi::Lang() != Sobi::DefLang()) { $defLabels[] = array('sKey' => 'name', 'sValue' => $base['name'], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid); $defLabels[] = array('sKey' => 'suffix', 'sValue' => $base['suffix'], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid); $defLabels[] = array('sKey' => 'description', 'sValue' => $base['description'], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid); } if (count($labels)) { try { if (Sobi::Lang() != Sobi::DefLang()) { $db->insertArray('spdb_language', $defLabels, false, true); } $db->insertArray('spdb_language', $labels, true); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_SAVE_FIELD_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } } SPFactory::cache()->cleanSection(); return $this->fid; }
/** * Translating language depend attributes of objects * * @param array $sids - array with ids of objects to translate * @param array $fields - (optional) array (or string) with properties names to translate. If not given, translates all * @param string $type - (optional) type of object (section, category, entry). If not given, translates all * @param string $lang - (optional) specific language. If not given, use currently set language * @param string $ident * @return array */ public static function translateObject($sids, $fields = array(), $type = null, $lang = null, $ident = 'id') { /** @todo multiple attr does not work because the id is the object id */ $fields = is_array($fields) ? $fields : (strlen($fields) ? array($fields) : null); $lang = $lang ? $lang : Sobi::Lang(false); // we don't need to specify the language as we want to have all of them and then order it right // when an object name has been entered in a particular language but this language isn't used later // we won't have any label for this certain object // Wed, Dec 18, 2013 09:57:04 //$params = array( 'id' => $sids, 'language' => array( $lang, Sobi::DefLang(), 'en-GB' ) ); $params = array($ident => $sids); $result = array(); if ($type) { $params['oType'] = $type; } if (in_array('alias', $fields)) { $fields[] = 'nid'; } if ($fields && count($fields)) { $params['sKey'] = $fields; } try { $labels = SPFactory::db()->select($ident . ' AS id, sKey AS label, sValue AS value, language', 'spdb_language', $params, "FIELD( language, '{$lang}', '" . Sobi::DefLang() . "' )")->loadAssocList(); if (count($labels)) { $aliases = array(); if (in_array('alias', $fields)) { $aliases = SPFactory::db()->select(array('nid', 'id'), 'spdb_object', array('id' => $sids))->loadAssocList('id'); } foreach ($labels as $label) { if ($label['label'] == 'nid') { $result[$label['id']]['alias'] = $label['value']; } else { if (!isset($result[$label['id']]) || $label['language'] == Sobi::Lang()) { $result[$label['id']] = $label; } } if (in_array('nid', $fields)) { if (!isset($result[$label['id']]['alias'])) { $result[$label['id']]['alias'] = isset($aliases[$label['id']]) ? $aliases[$label['id']]['nid'] : null; } } } } } catch (SPError $x) { Sobi::Error('language', SPLang::e('CANNOT_TRANSLATE_OBJECT', $x->getMessage()), SPC::WARNING, 500, __LINE__, __CLASS__); } return $result; }
/** * @param string $subject * @param string $col * @param string $def * @param int $lim * @param int $lStart * @param $sids * @return string */ protected function parseOrdering($subject, $col, $def, &$lim, &$lStart, &$sids) { $ord = Sobi::GetUserState($subject . '.order', $col, Sobi::Cfg('admin.' . $subject . '-order', $def)); $ord = str_replace(array('e_s', 'c_s'), null, $ord); if (strstr($ord, '.')) { $ord = explode('.', $ord); $dir = $ord[1]; $ord = $ord[0]; } else { $dir = 'asc'; } if ($ord == 'order' || $ord == 'position') { $subject = $subject == 'categories' ? 'category' : 'entry'; /* @var SPdb $db */ $db = SPFactory::db(); $db->select('id', 'spdb_relations', array('oType' => $subject, 'pid' => $this->_model->get('id')), 'position.' . $dir, $lim, $lStart); $fields = $db->loadResultArray(); if (count($fields)) { $sids = $fields; $fields = implode(',', $fields); $ord = "field( id, {$fields} )"; $lStart = 0; $lim = 0; } else { $ord = 'id.' . $dir; } } elseif ($ord == 'name') { $subject = $subject == 'categories' ? 'category' : 'entry'; /* @var SPdb $db */ $db =& SPFactory::db(); $db->select('id', 'spdb_language', array('oType' => $subject, 'sKey' => 'name', 'language' => Sobi::Lang()), 'sValue.' . $dir); $fields = $db->loadResultArray(); if (!count($fields) && Sobi::Lang() != Sobi::DefLang()) { $db->select('id', 'spdb_language', array('oType' => $subject, 'sKey' => 'name', 'language' => Sobi::DefLang()), 'sValue.' . $dir); $fields = $db->loadResultArray(); } if (count($fields)) { $fields = implode(',', $fields); $ord = "field( id, {$fields} )"; } else { $ord = 'id.' . $dir; } } elseif (strstr($ord, 'field_')) { $db = SPFactory::db(); static $field = null; if (!$field) { try { $db->select('fieldType', 'spdb_field', array('nid' => $ord, 'section' => Sobi::Section())); $fType = $db->loadResult(); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_DETERMINE_FIELD_TYPE', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } if ($fType) { $field = SPLoader::loadClass('opt.fields.' . $fType); } } /* * * @TODO The whole sort by custom field method in admin panel has to be re-implemented - * We could use the same field 'sortBy' method for backend and frontend. * The current method could be very inefficient !!! */ if ($field && method_exists($field, 'sortByAdm')) { $fields = call_user_func_array(array($field, 'sortByAdm'), array(&$ord, &$dir)); } else { $join = array(array('table' => 'spdb_field', 'as' => 'def', 'key' => 'fid'), array('table' => 'spdb_field_data', 'as' => 'fdata', 'key' => 'fid')); $db->select('sid', $db->join($join), array('def.nid' => $ord, 'lang' => Sobi::Lang()), 'baseData.' . $dir); $fields = $db->loadResultArray(); } if (count($fields)) { $fields = implode(',', $fields); $ord = "field( id, {$fields} )"; } else { $ord = 'id.' . $dir; } } elseif ($ord == 'state') { $ord = $ord . '.' . $dir . ', validSince.' . $dir . ', validUntil.' . $dir; } else { $ord = $ord . '.' . $dir; } return $ord; }
/** */ private function loadTables() { try { $lang = Sobi::Lang(false); $labels = SPFactory::db()->select(array('sValue', 'sKey'), 'spdb_language', array('fid' => $this->id, 'sKey' => $this->_translatable, 'oType' => 'field'), "FIELD( language, '{$lang}', '%' ) ASC")->loadAssocList('sKey'); if (!count($labels)) { // last fallback $labels = SPFactory::db()->select(array('sValue', 'sKey'), 'spdb_language', array('fid' => $this->id, 'sKey' => $this->_translatable, 'language' => 'en-GB', 'oType' => 'field'))->loadAssocList('sKey'); } if (Sobi::Lang(false) != Sobi::DefLang()) { $labels2 = SPFactory::db()->select(array('sValue', 'sKey'), 'spdb_language', array('fid' => $this->id, 'sKey' => $this->_translatable, 'language' => Sobi::DefLang(), 'oType' => 'field'))->loadAssocList('sKey'); $labels = array_merge($labels2, $labels); } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } if (count($labels)) { Sobi::Trigger('Field', ucfirst(__FUNCTION__), array(&$labels)); foreach ($labels as $k => $v) { $this->_set($k, $v['sValue']); } } $this->priority = $this->priority ? $this->priority : 5; /* if field is an admin filed - it cannot be required */ if (($this->adminField || !$this->editable || !$this->enabled) && !defined('SOBIPRO_ADM')) { $this->required = false; } }
/** * Gets the data for a field and save it in the database * @param SPEntry $entry * @param string $request * @return bool */ public function saveData(&$entry, $request = 'POST') { if (!$this->enabled) { return false; } $data = $this->verify($entry, $request); $time = SPRequest::now(); $IP = SPRequest::ip('REMOTE_ADDR', 0, 'SERVER'); $uid = Sobi::My('id'); /* if we are here, we can save these data */ /* @var SPdb $db */ $db =& SPFactory::db(); if ($this->allowHtml) { /* filter data */ if (count($this->allowedAttributes)) { SPRequest::setAttributesAllowed($this->allowedAttributes); } if (count($this->allowedTags)) { SPRequest::setTagsAllowed($this->allowedTags); } $data = SPRequest::string($this->nid, null, $this->allowHtml, $request); SPRequest::resetFilter(); if (!$this->editor && $this->maxLength && strlen($data) > $this->maxLength) { $data = substr($data, 0, $this->maxLength); } } else { $data = strip_tags($data); } /* collect the needed params */ $params = array(); $params['publishUp'] = $entry->get('publishUp'); $params['publishDown'] = $entry->get('publishDown'); $params['fid'] = $this->fid; $params['sid'] = $entry->get('id'); $params['section'] = Sobi::Reg('current_section'); $params['lang'] = Sobi::Lang(); $params['enabled'] = $entry->get('state'); $params['params'] = null; $params['options'] = null; $params['baseData'] = $data; $params['approved'] = $entry->get('approved'); $params['confirmed'] = $entry->get('confirmed'); /* if it is the first version, it is new entry */ if ($entry->get('version') == 1) { $params['createdTime'] = $time; $params['createdBy'] = $uid; $params['createdIP'] = $IP; } $params['updatedTime'] = $time; $params['updatedBy'] = $uid; $params['updatedIP'] = $IP; $params['copy'] = !$entry->get('approved'); if (Sobi::My('id') == $entry->get('owner')) { --$this->editLimit; } $params['editLimit'] = $this->editLimit; /* save it */ try { $db->insertUpdate('spdb_field_data', $params); } catch (SPException $x) { Sobi::Error(__CLASS__, SPLang::e('CANNOT_SAVE_DATA', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } /* if it wasn't edited in the default language, we have to try to insert it also for def lang */ if (Sobi::Lang() != Sobi::DefLang()) { $params['lang'] = Sobi::DefLang(); try { $db->insert('spdb_field_data', $params, true, true); } catch (SPException $x) { Sobi::Error(__CLASS__, SPLang::e('CANNOT_SAVE_DATA', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } } }
/** * @param $cOrder * @param int $cLim * @internal param string $eOrder * @internal param int $eLimit * @internal param int $eLimStart * @return array */ public function getCats($cOrder, $cLim = 0) { $categories = array(); $cOrder = trim($cOrder); $cLim = $cLim > 0 ? $cLim : 0; if ($this->_model->getChilds('category')) { /* var SPDb $db */ $db = SPFactory::db(); $oPrefix = null; /* load needed definitions */ SPLoader::loadClass('models.dbobject'); $conditions = array(); switch ($cOrder) { case 'name.asc': case 'name.desc': $table = $db->join(array(array('table' => 'spdb_language', 'as' => 'splang', 'key' => 'id'), array('table' => 'spdb_object', 'as' => 'spo', 'key' => 'id'))); $oPrefix = 'spo.'; $conditions['spo.oType'] = 'category'; $conditions['splang.sKey'] = 'name'; $conditions['splang.language'] = array(Sobi::Lang(false), Sobi::DefLang(), 'en-GB'); if (strstr($cOrder, '.')) { $cOrder = explode('.', $cOrder); $cOrder = 'sValue.' . $cOrder[1]; } break; case 'position.asc': case 'position.desc': $table = $db->join(array(array('table' => 'spdb_relations', 'as' => 'sprl', 'key' => 'id'), array('table' => 'spdb_object', 'as' => 'spo', 'key' => 'id'))); $conditions['spo.oType'] = 'category'; $oPrefix = 'spo.'; break; case 'counter.asc': case 'counter.desc': $table = $db->join(array(array('table' => 'spdb_counter', 'as' => 'spcounter', 'key' => 'sid'), array('table' => 'spdb_object', 'as' => 'spo', 'key' => 'id'))); $oPrefix = 'spo.'; $conditions['spo.oType'] = 'category'; if (strstr($cOrder, '.')) { $cOrder = explode('.', $cOrder); $cOrder = 'spcounter.counter.' . $cOrder[1]; } break; default: $table = 'spdb_object'; break; } /* check user permissions for the visibility */ if (Sobi::My('id')) { if (!Sobi::Can('category.access.*')) { if (Sobi::Can('category.access.unapproved_own')) { $conditions[] = $db->argsOr(array('approved' => '1', 'owner' => Sobi::My('id'))); } else { $conditions[$oPrefix . 'approved'] = '1'; } } if (!Sobi::Can('category.access.unpublished')) { if (Sobi::Can('category.access.unpublished_own')) { $conditions[] = $db->argsOr(array('state' => '1', 'owner' => Sobi::My('id'))); } else { $conditions[$oPrefix . 'state'] = '1'; } } if (!Sobi::Can('category.access.*')) { if (Sobi::Can('category.access.expired_own')) { $conditions[] = $db->argsOr(array('@VALID' => $db->valid($oPrefix . 'validUntil', $oPrefix . 'validSince'), 'owner' => Sobi::My('id'))); } else { $conditions['state'] = '1'; $conditions['@VALID'] = $db->valid($oPrefix . 'validUntil', $oPrefix . 'validSince'); } } } else { $conditions = array_merge($conditions, array($oPrefix . 'state' => '1', $oPrefix . 'approved' => '1', '@VALID' => $db->valid($oPrefix . 'validUntil', $oPrefix . 'validSince'))); } $conditions[$oPrefix . 'id'] = $this->_model->getChilds('category'); try { $results = $db->select($oPrefix . 'id', $table, $conditions, $cOrder, $cLim, 0, true)->loadResultArray(); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } Sobi::Trigger($this->name(), 'AfterGetCategories', array(&$results)); if ($results && count($results)) { foreach ($results as $i => $cid) { $categories[$i] = $cid; // new $cClass(); //$categories[ $i ]->init( $cid ); } } } return $categories; }
/** * @param string $col * @param string $def * @return string */ protected function parseOrdering($col, $def) { $order = Sobi::GetUserState('fields.order', $col, Sobi::Cfg('admin.fields-order', $def)); $ord = $order; $dir = 'asc'; /** legacy - why the hell I called it order?! */ $ord = str_replace('order', 'position', $ord); if (strstr($ord, '.')) { $ord = explode('.', $ord); $dir = $ord[1]; $ord = $ord[0]; } $ord = $ord == 'state' ? 'enabled' : $ord; // $ord = ( $ord == 'position' ) ? 'position' : $ord; if ($ord == 'name') { /* @var SPdb $db */ $db = SPFactory::db(); $fields = $db->select('fid', 'spdb_language', array('oType' => 'field', 'sKey' => 'name', 'language' => Sobi::Lang()), 'sValue.' . $dir)->loadResultArray(); if (!count($fields) && Sobi::Lang() != Sobi::DefLang()) { $fields = $db->select('id', 'spdb_language', array('oType' => 'field', 'sKey' => 'name', 'language' => Sobi::DefLang()), 'sValue.' . $dir)->loadResultArray(); } if (count($fields)) { $fields = implode(',', $fields); $ord = "field( fid, {$fields} )"; } else { $ord = 'fid.' . $dir; } } else { $ord = $ord . '.' . $dir; } Sobi::setUserState('fields.order', $order); return $ord; }
/** * Shows the field in the search form * @param bool $return return or display directly * @return string */ public function searchForm($return = false) { if ($this->searchMethod == 'general') { return false; } if ($this->searchMethod == 'range') { return $this->rangeSearch($this->searchRangeValues, $this->freeRange); } $fdata = array(); try { $data = SPFactory::db()->dselect(array('baseData', 'sid', 'lang'), 'spdb_field_data', array('fid' => $this->fid, 'copy' => '0', 'enabled' => 1), 'field( lang, \'' . Sobi::Lang() . '\'), baseData', 0, 0, 'baseData')->loadAssocList(); $languages = array(); $output = array(); $lang = Sobi::Lang(false); $defLang = Sobi::DefLang(); if (count($data)) { foreach ($data as $row) { $languages[$row['lang']][$row['sid']] = $row['baseData']; } } if (isset($languages[$lang])) { foreach ($languages[$lang] as $sid => $fieldData) { $output[$sid] = $fieldData; } unset($languages[$lang]); } if (isset($languages[$defLang])) { foreach ($languages[$defLang] as $sid => $fieldData) { if (!isset($output[$sid])) { $output[$sid] = $fieldData; } } unset($languages[$defLang]); } if (count($languages)) { foreach ($languages as $language => $langData) { foreach ($langData as $sid => $fieldData) { if (!isset($output[$sid])) { $output[$sid] = $fieldData; } } unset($languages[$language]); } } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_GET_FIELDS_DATA_DB_ERR', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } $data = (array) $output; if (count($data)) { $fdata[''] = Sobi::Txt('FD.INBOX_SEARCH_SELECT', array('name' => $this->name)); foreach ($data as $i => $d) { if (strlen($d)) { $fdata[strip_tags($d)] = strip_tags($d); } } } return SPHtml_Input::select($this->nid, $fdata, $this->_selected, false, array('class' => $this->cssClass . ' ' . Sobi::Cfg('search.form_list_def_css', 'SPSearchSelect'), 'size' => '1', 'id' => $this->nid)); }
/** * Gets the data for a field and save it in the database * @param SPEntry $entry * @param string $request * @throws SPException * @return bool */ public function saveData(&$entry, $request = 'POST') { if (!$this->enabled) { return false; } if ($this->method == 'fixed') { $fixed = $this->fixedCid; $fixed = explode(',', $fixed); $data = array(); if (count($fixed)) { foreach ($fixed as $cid) { $data[] = trim($cid); } } if (!count($data)) { throw new SPException(SPLang::e('FIELD_CC_FIXED_CID_NOT_SELECTED', $this->name)); } } else { $data = $this->verify($entry, $request); } $time = SPRequest::now(); $IP = SPRequest::ip('REMOTE_ADDR', 0, 'SERVER'); $uid = Sobi::My('id'); /* if we are here, we can save these data */ /* @var SPdb $db */ $db = SPFactory::db(); /* collect the needed params */ $params = array(); $params['publishUp'] = $entry->get('publishUp'); $params['publishDown'] = $entry->get('publishDown'); $params['fid'] = $this->fid; $params['sid'] = $entry->get('id'); $params['section'] = Sobi::Reg('current_section'); $params['lang'] = Sobi::Lang(); $params['enabled'] = $entry->get('state'); $params['params'] = null; $params['options'] = null; $params['baseData'] = SPConfig::serialize($data); $params['approved'] = $entry->get('approved'); $params['confirmed'] = $entry->get('confirmed'); /* if it is the first version, it is new entry */ if ($entry->get('version') == 1) { $params['createdTime'] = $time; $params['createdBy'] = $uid; $params['createdIP'] = $IP; } $params['updatedTime'] = $time; $params['updatedBy'] = $uid; $params['updatedIP'] = $IP; $params['copy'] = !$entry->get('approved'); if (Sobi::My('id') == $entry->get('owner')) { --$this->editLimit; } $params['editLimit'] = $this->editLimit; /* save it */ try { /* Notices: * If it was new entry - insert * If it was an edit and the field wasn't filled before - insert * If it was an edit and the field was filled before - update * " ... " and changes are not autopublish it should be insert of the copy .... but * " ... " if a copy already exist it is update again * */ $db->insertUpdate('spdb_field_data', $params); } catch (SPException $x) { Sobi::Error(__CLASS__, SPLang::e('CANNOT_SAVE_DATA', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } /* if it wasn't edited in the default language, we have to try to insert it also for def lang */ if (Sobi::Lang() != Sobi::DefLang()) { $params['lang'] = Sobi::DefLang(); try { $db->insert('spdb_field_data', $params, true, true); } catch (SPException $x) { Sobi::Error(__CLASS__, SPLang::e('CANNOT_SAVE_DATA', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } } /** Last important thing - join selected categories */ $cats = SPFactory::registry()->get('request_categories', array()); $cats = array_unique(array_merge($cats, $data)); SPFactory::registry()->set('request_categories', $cats); if ($this->method == 'select' && $this->isPrimary) { $db->update('spdb_object', array('parent' => $data[0]), array('id' => $params['sid'])); } }
protected function checkTranslation() { $lang = SPRequest::cmd('sp-language', false, 'get'); if ($lang && $lang != Sobi::DefLang()) { $languages = SPFactory::CmsHelper()->availableLanguages(); SPFactory::message()->info(Sobi::Txt('INFO_DIFFERENT_LANGUAGE', $this->_type, $languages[$lang]['name']), false); } }
/** */ protected function translate() { $attributes = array_merge($this->translatable(), self::$translatable); Sobi::Trigger($this->name(), ucfirst(__FUNCTION__) . 'Start', array(&$attributes)); $db =& SPFactory::db(); try { $labels = $db->select('sValue, sKey', 'spdb_language', array('id' => $this->id, 'sKey' => $attributes, 'language' => Sobi::Lang(), 'oType' => $this->type()))->loadAssocList('sKey'); /* get labels in the default lang first */ if (Sobi::Lang(false) != Sobi::DefLang()) { $dlabels = $db->select('sValue, sKey', 'spdb_language', array('id' => $this->id, 'sKey' => $attributes, 'language' => Sobi::DefLang(), 'oType' => $this->type()))->loadAssocList('sKey'); if (count($dlabels)) { foreach ($dlabels as $k => $v) { if (!isset($labels[$k]) || !$labels[$k]) { $labels[$k] = $v; } } } } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('DB_REPORTS_ERR', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } if (count($labels)) { foreach ($labels as $k => $v) { $this->_set($k, $v['sValue']); } } Sobi::Trigger($this->name(), ucfirst(__FUNCTION__), array(&$labels)); }