_suMode = (bool) $flag; return $this; } public function setIdCategory($id) { $this->_idCategory = (int) $id; return $this; } public function getAdminSectionEmails() { if (null === $this->_adminSectionEmails) { $select = $this->db->select(); $select->from($this->pairAdmin, array('email')); $select->where('Admin.email IS NOT NULL'); $this->_adminSectionEmails = $this->db->fetchCol($select); $this->_adminSectionEmails = array_unique($this->_adminSectionEmails); sort($this->_adminSectionEmails); } return $this->_adminSectionEmails; } protected function _getFromDb($key = null, $changed = false) { $mainField = $changed ? 'changed' : 'value'; $select = $this->getListSelect() ->reset(Zend_Db_Select::COLUMNS) ->columns(array('name', $mainField, 'fieldType', 'options'), $this->_tableAlias); if (null != $key) { $select->where("{$this->_tableAlias}.name = ?", $key); $row = $this->_db->fetchRow($select); if (!empty($row)) { $this->_arrayParseTag2Url($row); if ('select' == $row['fieldType'] && !$changed) { $this->_prepareSelectValue($row['value'], $row['options']); } return array($row['name'] => $row[$mainField]); } } else { $rows = $this->_db->fetchAll($select); if (!empty($rows)) { $this->_prepareList($rows); $values = array(); foreach ($rows as $row) { if ('select' == $row['fieldType']) { $this->_prepareSelectValue($row['value'], $row['options']); } $values[$row['name']] = $row[$mainField]; } return $values; } } return false; } public function getData($field = false) { if (null === $this->_data && $data = $this->_getFromDb()) { $this->_data = $data; } return Qs_Array::get($this->_data, $field); } public function getChangedData($field = false) { if (null === $this->_changed && $changed = $this->_getFromDb(null, true)) { $this->_changed = $changed; } return Qs_Array::get($this->_changed, $field); } protected function _prepareSelectValue(&$value, $options) { if (!empty($options)) { $options = $this->parseSelectOptions($options); if ($options['multiple']) { $value = explode("\n", $value); array_walk($value, array($this, '_trimOptions')); } } return $this; } public function parseSelectOptions($options) { $options = trim($options); $options = explode("\n", $options); list($size, $multiple) = explode('|', $options[0]); unset($options[0]); array_walk($options, array($this, '_trimOptions')); $options = array_combine($options, $options); $size = (int)$size; $size = (!$size) ? 1 : $size; $multiple = (int)$multiple; return array('size' => $size, 'multiple' => (bool)$multiple, 'options' => $options); } protected function _trimOptions(&$value, $key) { $value = trim($value); } protected function _order(Zend_Db_Select $select = null) { return parent::_order($select)->order("{$this->_tableAlias}.sorter"); } protected function _join(Zend_Db_Select $select = null) { $select = parent::_join($select); $select->join( array("{$this->_tableAlias}Category" => $this->_getTableName("{$this->_tableAlias}Category")), "{$this->_tableAlias}Category.id = {$this->_tableAlias}.idCategory", '' ); return $select; } protected function _where(Zend_Db_Select $select = null) { if ($this->_idCategory) { $where = "{$this->_tableAlias}.idCategory = ?"; if (!$this->_suMode) { $where .= " AND {$this->_tableAlias}.system = 'n'" . " AND {$this->_tableAlias}Category.system = 'n'"; } return parent::_where($select)->where($where, $this->_idCategory); } return parent::_where($select); } public function update($data = null) { if (null === $data) { $data = $this->_data; } $fields = $this->_getFromDb(); if (is_array($fields)) { foreach (array_keys($fields) as $field) { if (array_key_exists($field, $data)) { if (is_array($data[$field])) { $data[$field] = implode("\n", $data[$field]); } $data[$field] = $this->_baseUrl2Tag($data[$field]); $this->table->updateByKey(array('value' => $data[$field]), $field); } } } self::_initSettings(); return $this; } public function getCategories($force = false) { if ($force || $this->_suMode) { self::$_categoriesList = array(); } if (empty(self::$_categoriesList)) { $select = $this->_db->select(); $select->from(array("{$this->_tableAlias}Category" => "{$this->_tableName}Category"), array('id', 'name')); if (!$this->_suMode) { $select->where("{$this->_tableAlias}Category.system = 'n'"); } $select->order("{$this->_tableAlias}Category.sorter"); self::$_categoriesList = $this->_db->fetchPairs($select); } return self::$_categoriesList; } public static function getAdminEmails() { return self::get('adminEmail', 'parseEmails'); } public static function getEmailForm($fieldName) { $result = self::get($fieldName); if (empty($result)) { $result = self::get('siteFrom'); } return $result; } public static function getFormEmails($fieldName) { $emails = self::getAdminEmails(); $formEmails = self::get($fieldName, 'parseEmails'); if (is_array($formEmails) && !empty($formEmails)) { $emails = array_merge($emails, $formEmails); $emails = array_unique($emails); } return $emails; } public static function get($name, $action = null) { self::_initSettings(); if (array_key_exists($name, self::$_settings)) { $value = self::$_settings[$name]; if (null !== $action && is_callable("App_Settings_Obj::{$action}")) { $value = App_Settings_Obj::$action($value); } return self::tag2BaseUrl($value); } return false; } public static function getChanged($name) { self::_initSettings(); if (array_key_exists($name, self::$_settingsChanged)) { return self::$_settingsChanged[$name]; } return false; } public static function parseEmails($value) { $emails = array(); $value = split("[ ;,\n\r\t]", $value); if (!empty($value)) { $validator = new Zend_Validate_EmailAddress(); foreach ($value as $email) { $email = trim($email); if ($validator->isValid($email)) { $emails[] = $email; } } } $emails = array_unique($emails); return $emails; } protected static function _initSettings() { if (null === self::$_settings) { self::$_settings = self::getInstance()->getData(); } if (null === self::$_settingsChanged) { self::$_settingsChanged = self::getInstance()->getChangedData(); } } public function getFieldOptions($name) { $select = $this->db->select(); $select->from($this->pairSettingsOptions); $select->where('name = ' . $this->db->quote($name)); $list = $this->db->fetchAll($select); $options = array(); foreach ($list as $option) { switch ($option['type']) { case 'bool': $value = (bool) $option['value']; break; case 'int': $value = (int) $option['value']; break; case 'float': $value = (float) $option['value']; break; case 'double': $value = (double) $option['value']; break; default: $value = $option['value']; break; } $options[$option['variable']] = $value; } $options = Qs_Array::expand($options); return $options; } }