/** * Получить настройку по псевдониму или ключевому полю * * @param $key * @return CSetting */ public static function getSetting($key) { if (is_string($key)) { $key = strtoupper($key); } $cacheKey = CACHE_APPLICATION_SETTINGS . '_' . $key; if (!CApp::getApp()->cache->hasCache($cacheKey)) { $found = false; if (is_string($key)) { foreach (CActiveRecordProvider::getWithCondition(TABLE_SETTINGS, "UPPER(alias) = '" . $key . "'")->getItems() as $item) { $found = true; $setting = new CSetting($item); CApp::getApp()->cache->set(CACHE_APPLICATION_SETTINGS . '_' . $setting->getId(), $setting); CApp::getApp()->cache->set($cacheKey, $setting); } } elseif (is_numeric($key)) { $item = CActiveRecordProvider::getById(TABLE_SETTINGS, $key); if (!is_null($item)) { $found = true; $setting = new CSetting($item); CApp::getApp()->cache->set(CACHE_APPLICATION_SETTINGS . '_' . $setting->alias, $setting); CApp::getApp()->cache->set($cacheKey, $setting); } } if (!$found) { CApp::getApp()->cache->set($cacheKey, null); } } return CApp::getApp()->cache->get($cacheKey); }
/** * Магический метод получения любых классов * * @param $name * @param array $params * @throws Exception */ public static function __callStatic($name, $params = array()) { /** * Получаем имя класса из имени функции */ $className = "C" . CUtils::strRight($name, "get"); if (!class_exists($className)) { throw new Exception("В приложении не объявлен класс " . $className); } /** * @var CActiveModel $simpleClass */ $simpleClass = new $className(); $table = $simpleClass->getRecord()->getTable(); $id = $params[0]; /** * Попробуем сначала получить из кэша */ $keySeek = $table . "_" . $id; if (CApp::getApp()->cache->hasCache($keySeek)) { return CApp::getApp()->cache->get($keySeek); } $ar = CActiveRecordProvider::getById($table, $id); if (!is_null($ar)) { $obj = new $className($ar); CApp::getApp()->cache->set($keySeek, $obj, 60); return $obj; } }
public function actionGetObject($id) { $cache_id = $this->taxonomy . "_item_" . $id; if (is_null(CApp::getApp()->cache->get($cache_id))) { $taxonomy = CTaxonomyManager::getTaxonomy($this->taxonomy); $term = $taxonomy->getTerm($id); CApp::getApp()->cache->set($cache_id, $term, 30); } return CApp::getApp()->cache->get($cache_id); }
/** * Сериализовать бин на диск * * @param CStatefullBean $bean */ public function serializeBean(CStatefullBean $bean) { if (is_a(CApp::getApp()->getApp(), "CCacheDummy")) { if (file_exists($this->cacheDir . $bean->getBeanId())) { unlink($this->cacheDir . $bean->getBeanId()); } file_put_contents($this->cacheDir . $bean->getBeanId(), serialize($bean)); } else { CApp::getApp()->cache->set("bean_" . $bean->getBeanId(), $bean); } }
/** * @return CArrayList|null */ private static function getCacheSettings() { if (is_null(self::$_cacheSettings)) { self::$_cacheSettings = new CArrayList(); if (CSettingsManager::getSettingValue("preload_settings")) { // будет с поддержкой кеша if (is_null(CApp::getApp()->cache->get(CACHE_APPLICATION_SETTINGS))) { foreach (CActiveRecordProvider::getAllFromTable(TABLE_SETTINGS)->getItems() as $item) { $setting = new CSetting($item); self::getCacheSettings()->add($setting->getId(), $setting); self::getCacheSettings()->add(strtoupper($setting->alias), $setting); } CApp::getApp()->cache->set(CACHE_APPLICATION_SETTINGS, self::$_cacheSettings, 3600); } else { self::$_cacheSettings = CApp::getApp()->cache->get(CACHE_APPLICATION_SETTINGS); } } } return self::$_cacheSettings; }
public function getReportData() { $result = array(); // получаем данные о статистике // за промежуток времени $condition = "time_stamp BETWEEN '" . date("Y-m-d", strtotime($this->start)) . "' AND '" . date("Y-m-d", strtotime($this->end)) . "'"; $condition .= " and (s.q_string like '%action=search%'"; $condition .= " or s.q_string like '%action=getGlobalSearchSubform%'"; $condition .= " or s.q_string like '%action=LookupGetCreationDialog%'"; $condition .= " or s.q_string like '%action=GlobalSearch%'"; $condition .= " or s.q_string like '%action=LookupGetDialog%'"; $condition .= " or s.q_string like '%action=LookupGetItem%'"; $condition .= " or s.q_string like '%action=LookupTypeAhead%' )"; $query = new CQuery(CApp::getApp()->getDbLogConnection()); $select = "month(s.time_stamp) as t_stamp_m, year(s.time_stamp) as t_stamp_y, count(id) as cnt"; $select .= ", concat(month(s.time_stamp), '.', year(s.time_stamp)) as t_stamp"; $query->select($select)->from("stats as s")->condition($condition)->group("t_stamp_m")->order("t_stamp_y desc, t_stamp_m desc"); $result = $query->execute()->getItems(); $res = array(); foreach ($result as $row) { $data = array("t_stamp" => $row["t_stamp"], "cnt" => $row["cnt"], "total" => 0); $res[$row["t_stamp"]] = $data; } $condition = "time_stamp BETWEEN '" . date("Y-m-d", strtotime($this->start)) . "' AND '" . date("Y-m-d", strtotime($this->end)) . "'"; $query = new CQuery(CApp::getApp()->getDbLogConnection()); $select = "month(s.time_stamp) as t_stamp_m, year(s.time_stamp) as t_stamp_y, count(id) as cnt"; $select .= ", concat(month(s.time_stamp), '.', year(s.time_stamp)) as t_stamp"; $query->select($select)->from("stats as s")->condition($condition)->group("t_stamp_m")->order("t_stamp_y desc, t_stamp_m desc"); $result = $query->execute()->getItems(); foreach ($result as $row) { if (array_key_exists($row["t_stamp"], $res)) { $res[$row["t_stamp"]]["total"] = $row["cnt"]; } } return $res; }
/** * @param $key * @return CCoreModelFieldTranslation */ public static function getCoreModelFieldTranslation($key) { if (!CApp::getApp()->cache->hasCache("core_model_field_translation_" . $key)) { $ar = null; if (is_numeric($key)) { $ar = CActiveRecordProvider::getById(TABLE_CORE_MODEL_FIELD_TRANSLATIONS, $key); } if (!is_null($ar)) { $t = new CCoreModelFieldTranslation($ar); CApp::getApp()->cache->set("core_model_field_translation_" . $key, $t, 300); } } return CApp::getApp()->cache->get("core_model_field_translation_" . $key); }
public function execute($contextObject) { /** * @var $bean CStatefullBean */ $bean = CApp::getApp()->beans->getStatefullBean(CRequest::getString("beanId")); $persons = new CArrayList(); foreach ($bean->getItem("id")->getItems() as $person_id) { $person = CStaffManager::getPerson($person_id); if (!is_null($person)) { $persons->add($person->getId(), $person); } } /** * Отфильтруем нужные планы */ $targetPlans = new CArrayList(); /** * @var $person CPerson */ foreach ($persons->getItems() as $person) { foreach ($person->getIndPlansByYears($bean->getItem("year_id"))->getItems() as $year_id => $plans) { foreach ($plans->getItems() as $plan) { if (in_array($plan->type, $bean->getItem("types")->getItems())) { $targetPlans->add($plan->getId(), $plan); } } } } $month = $bean->getItem("month"); $month = $month->getFirstItem(); $result = array(); /** * @var $plan CIndPlanPersonLoad */ foreach ($targetPlans->getItems() as $plan) { $row = array(); $row[0] = ""; $row[1] = ""; $row[2] = ""; $row[3] = ""; $row[4] = ""; // план на семестр if (!array_key_exists(5, $row)) { $row[5] = 0; } $preparedData = array(); $table = $plan->getStudyLoadTable()->getTable(); foreach ($table as $r) { if ($plan->isSeparateContract()) { // если есть бюджет-контракт, то суммируем их $preparedRow = array(); $preparedRow[0] = $r[0]; for ($i = 1; $i <= 17; $i++) { $preparedRow[$i] = $r[$i * 2] + $r[$i * 2 - 1]; } $preparedData[] = $preparedRow; } else { // нет разделения на бюджет-контракт, копируем $preparedData[] = $r; } } if (in_array($month, array(2, 3, 4, 5, 6))) { foreach ($preparedData as $preparedRow) { $row[5] += $preparedRow[1]; } } else { foreach ($preparedData as $preparedRow) { $row[5] += $preparedRow[8]; } } $rows = array(6 => 0, 7 => 1, 8 => 2, 9 => -1, 10 => 4, 11 => -1, 12 => 14, 13 => 3, 14 => 5, 15 => 6, 16 => 7, 17 => 8, 18 => 9, 19 => 10, 20 => 15, 21 => 16); foreach ($rows as $target => $source) { if (!array_key_exists($target, $row)) { $row[$target] = 0; } if ($source != -1) { $row[$target] += $preparedData[$source][$month]; } } if (!array_key_exists(22, $row)) { $row[22] = 0; } for ($i = 6; $i <= 21; $i++) { $row[22] += $row[$i]; } $result[] = $row; } $sum = array(); $sum[0] = "Итог"; for ($i = 1; $i <= 4; $i++) { $sum[$i] = ""; } for ($i = 5; $i <= 22; $i++) { if (!array_key_exists($i, $sum)) { $sum[$i] = 0; } foreach ($result as $item) { $sum[$i] += $item[$i]; } } $total = array($sum); return $total; }
/** * Группа пользователей * * @static * @param $key * @return CUserGroup */ public static function getUserGroup($key) { if (!self::getCacheUserGroups()->hasElement($key)) { $cache_id = "staff_user_group_" . $key; if (is_null(CApp::getApp()->cache->get($cache_id))) { $ar = CActiveRecordProvider::getById(TABLE_USER_GROUPS, $key); $group = null; if (!is_null($ar)) { $group = new CUserGroup($ar); CApp::getApp()->cache->set($cache_id, $group, 3600); } } $group = CApp::getApp()->cache->get($cache_id); self::getCacheUserGroups()->add($group->getId(), $group); } return self::getCacheUserGroups()->getItem($key); }
/** * @return Smarty */ protected function getSmarty() { if (is_null($this->_smarty)) { // подключаем Smarty $config = CApp::getApp()->getConfig(); $smartyConfig = $config["smarty"]; $this->_smarty = new Smarty(); $this->_smarty->caching = $smartyConfig["cacheEnabled"]; $this->_smarty->setTemplateDir(SMARTY_TEMPLATES); $this->_smarty->setCompileDir(SMARTY_COMPILE); $this->_smarty->setCacheDir(SMARTY_CACHE); // постоянно нужная ерунда типа управлялки меню, // нет другого места, откуда ее можно хорошо подцепить $this->addCSSInclude("_core/core.css"); } return $this->_smarty; }
public function execute($contextObject) { /** * @var $bean CStatefullBean */ $bean = CApp::getApp()->beans->getStatefullBean(CRequest::getString("beanId")); $persons = new CArrayList(); foreach ($bean->getItem("id")->getItems() as $person_id) { $person = CStaffManager::getPerson($person_id); if (!is_null($person)) { $persons->add($person->getId(), $person); } } /** * Отфильтруем нужные планы */ $targetPlans = new CArrayList(); /** * @var $person CPerson */ foreach ($persons->getItems() as $person) { foreach ($person->getIndPlansByYears($bean->getItem("year_id"))->getItems() as $year_id => $plans) { foreach ($plans->getItems() as $plan) { if (in_array($plan->type, $bean->getItem("types")->getItems())) { $targetPlans->add($plan->getId(), $plan); } } } } $month = $bean->getItem("month"); $month = $month->getFirstItem(); $result = array(); /** * @var $plan CIndPlanPersonLoad */ foreach ($targetPlans->getItems() as $plan) { $row = array(); if (array_key_exists($plan->person_id, $result)) { $row = $result[$plan->person_id]; } $row[0] = "АСУ"; $row[1] = count($result); $row[2] = ""; if (!is_null($plan->person)) { if (!is_null($plan->person->getPost())) { $row[2] = $plan->person->getPost()->name_short; } } $row[3] = ""; if (!is_null($plan->person)) { if (!is_null($plan->person->degree)) { $row[3] = $plan->person->degree->comment; } } $row[4] = ""; if (!is_null($plan->person)) { $row[4] = $plan->person->fio_short; } // план на семестр бюджет if (!array_key_exists(5, $row)) { $row[5] = 0; } // план на семестр контракт if (!array_key_exists(6, $row)) { $row[6] = 0; } // итого бюджет if (!array_key_exists(23, $row)) { $row[23] = 0; } // итого контракт if (!array_key_exists(24, $row)) { $row[24] = 0; } $preparedData = array(); $table = $plan->getStudyLoadTable()->getTable(); foreach ($table as $r) { if ($plan->isSeparateContract()) { // если есть бюджет-контракт, то суммируем их $preparedRow = array(); $preparedRow[0] = $r[0]; for ($i = 1; $i <= 17; $i++) { $preparedRow[$i] = $r[$i * 2] + $r[$i * 2 - 1]; } // не разделяем бюджет-контракт плана на осенний семестр $preparedRow[18] = $r[1]; $preparedRow[19] = $r[2]; // не разделяем бюджет-контракт плана на весенний семестр $preparedRow[20] = $r[15]; $preparedRow[21] = $r[16]; // не разделяем бюджет-контракт итога за осенний семестр $preparedRow[20] = $r[13]; $preparedRow[21] = $r[14]; // не разделяем бюджет-контракт итога за весенний семестр $preparedRow[22] = $r[29]; $preparedRow[23] = $r[30]; $preparedData[] = $preparedRow; } else { // нет разделения на бюджет-контракт, копируем $preparedData[] = $r; } } if ($plan->isSeparateContract()) { if (in_array($month, array(2, 3, 4, 5, 6))) { foreach ($preparedData as $preparedRow) { $row[5] += $preparedRow[18]; $row[6] += $preparedRow[19]; $row[23] += $preparedRow[20]; $row[24] += $preparedRow[21]; } } else { foreach ($preparedData as $preparedRow) { $row[5] += $preparedRow[20]; $row[6] += $preparedRow[21]; $row[23] += $preparedRow[22]; $row[24] += $preparedRow[23]; } } $rows = array(7 => 0, 8 => 1, 9 => 2, 10 => -1, 11 => 4, 12 => -1, 13 => 14, 14 => 3, 15 => 5, 16 => 6, 17 => 7, 18 => 8, 19 => 9, 20 => 10, 21 => 15, 22 => 16); } else { if (in_array($month, array(2, 3, 4, 5, 6))) { foreach ($preparedData as $preparedRow) { $row[5] += $preparedRow[1]; } } else { foreach ($preparedData as $preparedRow) { $row[5] += $preparedRow[8]; } } $rows = array(6 => 0, 7 => 1, 8 => 2, 9 => -1, 10 => 4, 11 => -1, 12 => 14, 13 => 3, 14 => 5, 15 => 6, 16 => 7, 17 => 8, 18 => 9, 19 => 10, 20 => 15, 21 => 16); } foreach ($rows as $target => $source) { if (!array_key_exists($target, $row)) { $row[$target] = 0; } if (!array_key_exists(22, $row)) { $row[22] = 0; } if ($source != -1) { $row[$target] += $preparedData[$source][$month]; $row[22] += $preparedData[$source][$month]; } } if (!$plan->isSeparateContract()) { if (!array_key_exists(22, $row)) { $row[22] = 0; } for ($i = 6; $i <= 21; $i++) { $row[22] += $row[$i]; } } $result[$plan->person_id] = $row; } return $result; }
/** * Волшебный геттер * @param type $name */ public function __get($name) { // проверка на наличие маппинга свойства на поле БД $mapping = $this->fieldsMapping(); if (array_key_exists($name, $mapping)) { $name = $mapping[$name]; } // проверяем, не обычное ли это поле if (array_key_exists($name, $this->getRecord()->getItems())) { // обходной маневр для конвертации mysql-дат в отечественные $properties = $this->fieldsProperty(); if (array_key_exists($name, $properties)) { $property = $properties[$name]; if (array_key_exists("type", $property)) { if ($property["type"] == FIELD_MYSQL_DATE) { $value = $this->getRecord()->getItemValue($name); if (strpos($value, "0000-00-00") !== false) { return ""; } return date($property["format"], strtotime($value)); } } } return $this->getRecord()->getItemValue($name); } // поле необычное. $relations = $this->relations(); if (array_key_exists($name, $relations)) { $relation = $relations[$name]; // разрешим не указывать if (!array_key_exists("storageProperty", $relation)) { $relation["storageProperty"] = "_" . $name; } if ($relation['relationPower'] == RELATION_HAS_ONE) { $private = $relation['storageProperty']; if (is_null($this->{$private})) { $key_field = $relation['storageField']; $key_value = $this->{$key_field}; $useManager = true; if (array_key_exists("targetClass", $relation)) { $targetClass = $relation["targetClass"]; $useManager = false; } else { $managerClass = $relation['managerClass']; $managerGetter = $relation['managerGetObject']; } if ($useManager) { $obj = $managerClass::$managerGetter($key_value); } else { $targetClass = "get" . mb_substr($targetClass, 1); $obj = CBaseManager::$targetClass($key_value); } $this->{$private} = $obj; } return $this->{$private}; } elseif ($relation['relationPower'] == RELATION_HAS_MANY) { $private = $relation['storageProperty']; /** * Проверим, вдруг разрешено использование кэш * и данные кэше уже есть */ $useCache = false; if (array_key_exists("useCache", $relation)) { $useCache = $relation["useCache"]; } if ($useCache) { $cacheKey = get_class($this) . "_property_" . $name . "_" . $this->getId(); if (CApp::getApp()->cache->hasCache($cacheKey)) { $valueFromCache = CApp::getApp()->cache->get($cacheKey); $this->{$private} = $valueFromCache; } } if (is_null($this->{$private})) { $table = $relation['storageTable']; $condition = $relation['storageCondition']; $useManager = true; if (array_key_exists("targetClass", $relation)) { $targetClass = $relation["targetClass"]; $useManager = false; } else { $managerClass = $relation['managerClass']; $managerGetter = $relation['managerGetObject']; } $managerOrder = null; if (array_key_exists("managerOrder", $relation)) { $managerOrder = $relation['managerOrder']; } $this->{$private} = new CArrayList(); foreach (CActiveRecordProvider::getWithCondition($table, $condition, $managerOrder)->getItems() as $item) { if ($useManager) { $obj = $managerClass::$managerGetter($item->getId()); } else { $obj = new $targetClass($item); } if (!is_null($obj)) { if (is_object($obj)) { $this->{$private}->add($obj->getId(), $obj); } else { $this->{$private}->add($this->{$private}->getCount(), $obj); } } } if ($useCache) { CApp::getApp()->cache->set($cacheKey, $this->{$private}, 60); } } return $this->{$private}; } elseif ($relation['relationPower'] == RELATION_COMPUTED) { $private = $relation['storageProperty']; if (is_null($this->{$private})) { $function = $relation['relationFunction']; $this->{$private} = $this->{$function}(); } return $this->{$private}; } elseif ($relation['relationPower'] == RELATION_MANY_TO_MANY) { $private = $relation['storageProperty']; /** * Проверим, вдруг разрешено использование кэш * и данные кэше уже есть */ $useCache = false; if (array_key_exists("useCache", $relation)) { $useCache = $relation["useCache"]; } if ($useCache) { $cacheKey = get_class($this) . "_property_" . $name . "_" . $this->getId(); if (CApp::getApp()->cache->hasCache($cacheKey)) { $valueFromCache = CApp::getApp()->cache->get($cacheKey); $this->{$private} = $valueFromCache; } } if (is_null($this->{$private})) { $this->{$private} = new CArrayList(); $joinTable = $relation["joinTable"]; $rightKey = $relation["rightKey"]; $leftCondition = $relation["leftCondition"]; $useManager = true; if (array_key_exists("targetClass", $relation)) { $useManager = false; } else { $managerClass = $relation["managerClass"]; $managerGetter = $relation["managerGetObject"]; } foreach (CActiveRecordProvider::getWithCondition($joinTable, $leftCondition)->getItems() as $item) { $items = $item->getItems(); if ($useManager) { $obj = $managerClass::$managerGetter($items[$rightKey]); } else { $method = "get" . mb_substr($relation["targetClass"], 1); $obj = CBaseManager::$method($items[$rightKey]); } if (!is_null($obj)) { $this->{$private}->add($obj->id, $obj); } } if ($useCache) { CApp::getApp()->cache->set($cacheKey, $this->{$private}, 60); } } return $this->{$private}; } } return parent::__get($name); }
/** * Лист ролей, которыми должен обладать пользователь, чтобы * видеть данный пункт меню (как минимум, одной) * * @return CArrayList */ public function getRoles() { if (is_null($this->_roles)) { if (!CApp::getApp()->cache->hasCache("menu_item_access_" . $this->getId())) { $this->_roles = new CArrayList(); foreach (CActiveRecordProvider::getWithCondition(TABLE_MENU_ITEMS_ACCESS, "item_id = " . $this->getId())->getItems() as $item) { $role = CStaffManager::getUserRole($item->getItemValue("role_id")); if (!is_null($role)) { $this->_roles->add($role->getId(), $role); } } CApp::getApp()->cache->set("menu_item_access_" . $this->getId(), $this->_roles, 300); } $this->_roles = CApp::getApp()->cache->get("menu_item_access_" . $this->getId()); } return $this->_roles; }
/** * Отрисовать представление, результаты будут переданы указанному * классу и его методу * * @param $view * @param $targetClass * @param $targetMethod */ public function renderView($view, $targetClass = "", $targetMethod = "") { $this->setData("targetClass", $targetClass); $this->setData("targetMethod", $targetMethod); CApp::getApp()->beans->serializeBean(self::getStatefullBean()); $this->setData("beanId", self::getStatefullBean()->getBeanId()); parent::renderView($view); }
/** * @return CStatefullFormBean */ protected function getStatefullFormBean() { if (is_null($this->_statefullBean)) { $this->_statefullBean = new CStatefullFormBean(); if (CRequest::getString(FORM_BEAN) != "") { $this->_statefullBean = CApp::getApp()->beans->getStatefullBean(CRequest::getString(FORM_BEAN)); } } return $this->_statefullBean; }