PATH:
home
/
letacommog
/
letaweb
/
admin
/
api
<?php // base class for process common api request class BaseManager extends PController { /* * Permission constants */ const OWNER_PERMISSION = 'Owner'; const ADMINISTRATOR_PERMISSION = 'Administrator'; const SUPER_ADMINISTRATOR_PERMISSION = 'Super Administrator'; const AUTHOR_PERMISSION = 'Author'; const BILLING_PERMISSION = 'Billing'; public function filters() { return array( 'accessControl', ); } public function accesdsRules() { return array( array('allow', 'actions' => array('getTemplate'), 'roles' => array('Owner','Administrator','Super Administrator'), ), array('deny'// deny all users // 'users'=>array('*'), ), ); } public $owner; // Create api object public function __construct($id, $owner) { parent::__construct($id, null); $this->owner = $owner; } // return api name protected function getClassName() { $type = get_class($this); $type = str_replace('Manager', '', $type); $type = strtolower($type); return $type; } // Extract value from an array protected function getIndex($a, $i) { return isset($a[$i]) ? $a[$i] : ''; } // change Unicode character to ASCII character public function stripUnicode($str) { if (!$str) { return false; } $unicode = array( 'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ', 'd' => 'đ', 'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ', 'i' => 'í|ì|ỉ|ĩ|ị', 'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ', 'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự', 'y' => 'ý|ỳ|ỷ|ỹ|ỵ', ); foreach ($unicode as $nonUnicode => $uni) { $str = preg_replace("/($uni)/i", $nonUnicode, $str); } return $str; } protected function getDbConnection() { $module = new BModule(); $db = $module->getDbConnection(); return $db; } // function to get full url of a system file protected function _getFileName($file_name) { $base_url = Yii::app()->getBaseUrl(true); $file_name = str_replace(str_replace('www.', '', $base_url), '', str_replace('www.', '', $file_name)); $file_name = Yii::getPathOfAlias('application_root').'/'.$file_name; $qm_pos = strpos($file_name, '?'); if ($qm_pos > -1) { $file_name = substr($file_name, 0, $qm_pos); } if (preg_match('/http:/', $file_name)) { //echo "" . $file_name . ""; // trigger_error("EXTERNAL_FILE", E_USER_NOTICE); return $file_name; } return $file_name; } // function to load all skin preset protected function _getThemeCSS() { $css = Functions::parseCSS($this->renderPartial('themes', array(), true)); $skin = Functions::parseCSS($this->renderPartial('themes_skin', array(), true)); $rs = array('css' => $css,'skin' => $skin); return $rs; } public function actiongetCSS() { $hrefs = $_REQUEST['hrefs']; $this->_getCSS($hrefs); } public function actiongetCSSSkin() { $hrefs = $_REQUEST['hrefs']; $this->_getCSSSkin($hrefs); } protected function _getCSS($hrefs) { $file_names = explode(';', $hrefs); $data = array(); $i = 0; foreach ($file_names as $file_name) { if ($file_name == '') { continue; } $data[$file_name] = array(); $relative_path = $file_name; if ($i == 0) { $themes = $this->_getThemeCSS(); $data[$file_name]['css'] = $themes['css']; $data[$file_name]['skin'] = $themes['skin']; } else { $relative_path = $this->_getFileName($file_name); $file_arr = $this->getCSSContents($relative_path); $data[$file_name]['css'] = $file_arr; $relative_path = $relative_path.'_skin'; $file_arr = $this->getCSSContents($relative_path); $data[$file_name]['skin'] = $file_arr; } ++$i; } $this->ajaxSuccess($data); } protected function _getCSSSkin($hrefs) { $file_names = explode(';', $hrefs); $data = array(); foreach ($file_names as $file_name) { if ($file_name == '') { continue; } $relative_path = $this->_getFileName($file_name); $relative_path = $relative_path.'_skin'; $file_arr = $this->getCSSContents($relative_path); $data[$file_name] = $file_arr; } $this->ajaxSuccess($data); } // Read website color palette from css file public function actionloadPalette() { $file_name = 'common_skin'; $relative_path = Yii::app()->site->model->getFilePath('css', $file_name, '.css'); $data = array(); $data = $this->getCSSContents($relative_path); $this->ajaxSuccess($data); } // function to write all configuration value to file public function actionsync() { $this->_sync_configuration(); $this->ajaxSuccess(1); } protected function _sync_configuration() { $module = new BModule(); $db = $module->getDbConnection(); $command = $db->createCommand(); $rs = BConfigurations::model()->findAll(); $content = html_entity_decode('<?php '); foreach ($rs as $item) { $content .= "define('".$item['name']."','".$item['value']."');"; } Yii::app()->site->model->writeFile('', 'configurations', $content); } public function getCSSContents($relative_path) { return $this->readCSSContentFromFile($relative_path); } // send a success ajax message to browser public function ajaxSuccess($data) { Yii::app()->runComponent('api', $data); $rs = array('success' => true, 'data' => $data); echo json_encode($rs); } // send a error ajax message to browser public function ajaxError($msg = '') { $rs = array('success' => false, 'message' => $msg, 'type' => 1); echo json_encode($rs); return; } // function to get paging list data and send to browser public function actiongetPagedList() { if (isset($_REQUEST['name'])) { $name = $_REQUEST['name']; $tablename = $name; $m = 'actionget'.$tablename.'PagedList'; if (@method_exists($this, $m)) { $this->$m(); } else { $active_reocord_class = 'B'.$this->capitalizeTableName($tablename); if (class_exists($active_reocord_class)) { $start = $_REQUEST['start']; $columns = array(); if(isset($_REQUEST['columns'])) { $columns = $_REQUEST['columns']; } $orderStatement = array(); if(isset($_REQUEST['order'])) { $orders = $_REQUEST['order']; if(is_array($orders)) { foreach($orders as $order) { if(isset($order['column'])) { $column = $order['column']; $dir = isset($order['dir']) ? $order['dir'] : 'desc'; if(isset($columns[$column]) && isset($columns[$column]['data']) && strlen($columns[$column]['data']) ) { $orderStatement[] = $columns[$column]['data'].' '. $dir; } } } } } $orderStatement = implode(',', $orderStatement); $draw = $_REQUEST['draw']; $length = $_REQUEST['length']; //$_GET['pageSize'] = $length; // $_GET[$active_reocord_class.'_page'] = $page; $record = new $active_reocord_class(); if (isset($_REQUEST['filters'])) { $filters = $_REQUEST['filters']; if (is_array($filters)) { foreach ($filters as $key => $value) { if (property_exists($record, $key)) { $record->$key = $value; } } } } $dataProvider = $record->getPagedList($length,$start, $orderStatement); if(!is_array($dataProvider) && get_class($dataProvider) == 'ActiveDataProvider') { $list = $dataProvider->getData(true); $rows = $this->ar_toArray($list); $pagination = $dataProvider->getPagination(); $rs = array( 'draw'=>(int)$draw, 'data' => $rows, 'page' => $pagination->pageCount, 'recordsTotal' => (int)$dataProvider->getTotalItemCount(), 'recordsFiltered' => (int)$dataProvider->getTotalItemCount(), ); } else if(!is_array($dataProvider) && get_class($dataProvider) == 'CArrayDataProvider') { $list = $dataProvider->getData(true); $rows = ($list); $pagination = $dataProvider->getPagination(); $rs = array( 'draw'=>(int)$draw, 'data' => $rows, 'page' => $pagination->pageCount, 'recordsTotal' => (int)$dataProvider->getTotalItemCount(), 'recordsFiltered' => (int)$dataProvider->getTotalItemCount(), ); } else if(is_array($dataProvider) && isset($dataProvider['rows'])) { $rs = array( 'draw'=>(int)$draw, 'data' => ($dataProvider['rows']), 'page' => $dataProvider['pageCount'], 'recordsTotal' => $dataProvider['itemCount'], 'recordsFiltered' => $dataProvider['itemCount'], ); } else if(is_array($dataProvider)) { $rs = array( 'draw'=>(int)$draw, 'data' => ($dataProvider), 'page' => 1, 'recordsTotal' => sizeof($dataProvider), 'recordsFiltered' => sizeof($dataProvider), ); } echo json_encode($rs); return; } } $this->ajaxError(1); } else { $this->ajaxSuccess(array()); } } // function to get model form data and send to browser public function actiongetFormData() { if (isset($_REQUEST['name'])) { $name = $_REQUEST['name']; $tablename = $name; $active_reocord_class = 'B'.$this->capitalizeTableName($tablename); if (@class_exists($active_reocord_class)) { $record = new $active_reocord_class(); $id = $_REQUEST['id']; $record->id = $id; $dataProvider = $record->getFormData(); $this->ajaxSuccess($this->ar_toObject($dataProvider)); return; } } } protected function capitalizeTableName($tablename) { $tablename = explode('_', $tablename); $tables = array(); for ($i = 0;$i < sizeof($tablename);++$i) { $tables[] = ucfirst($tablename[$i]); } return implode('_', $tables); } // function to get list data and send to browser public function actiongetList() { if (isset($_REQUEST['name'])) { $name = $_REQUEST['name']; $tablename = $name; $m = 'actionget'.$tablename.'List'; if (method_exists($this, $m)) { $this->$m(); return; } else { $active_reocord_class = 'B'.$this->capitalizeTableName($tablename); if (@class_exists($active_reocord_class)) { $record = new $active_reocord_class(); $dataProvider = $record->getList(); $list = $dataProvider->getData(); $rs = $this->ar_toArray($list); $this->ajaxSuccess($rs); return; } } } $this->ajaxError(array()); } public function actiongetConfigurations() { $keys = $_REQUEST['keys']; $criteria = new CDbCriteria(); $criteria->addInCondition('name', $keys); $command = BConfigurations::model()->findAll($criteria); $rs = array(); for ($i = 0;$i < sizeof($command); ++$i) { $rs[ $command[$i]['name'] ] = $command[$i]['value']; } $this->ajaxSuccess($rs); } public function actiongetConfiguration() { $key = $_REQUEST['key']; $rs = BConfigurations::model()->find('name=:key', array(':key' => $key)); if (isset($rs)) { $this->ajaxSuccess($rs->value); } else { $this->ajaxSuccess('null'); } } public function _getConfiguration($key) { $rs = BConfigurations::model()->find('name=:key', array(':key' => $key)); if (isset($rs)) { return $rs->value; } return false; } protected function _updateConfiguration($key, $value) { $c = new BConfigurations(); $v = $c->find('`key`= :key', array(':key' => $key)); if (!isset($v)) { $db = $c->getDbConnection(); $maxId = $db->createCommand()->select('max(id)')->from('configurations')->queryScalar(); $command = $db->createCommand(); $command->insert('configurations', array('id' => $maxId + 1, 'key' => 'key', 'value' => 'key')); $v = new BConfigurations(); $v->id = $maxId + 1; $v->key = $key; $v->value = $value; } else { $v->key = $key; $v->value = $value; } $v->save(); } public function actionupdateConfiguration() { $c = new BConfigurations(); $key = $_REQUEST['key']; $value = $_REQUEST['value']; $this->_updateConfiguration($key, $value); $this->ajaxSuccess(1); } public function actionupdateConfigurations() { $data = $_REQUEST['data']; foreach ($data as $key => $value) { $c = new BConfigurations(); $c->setIsNewRecord(true); BConfigurations::model()->deleteAll('name=:key', array(':key' => $key)); $c->name = $key; if(is_array($value)) { $value = serialize($value); } $c->value = $value; $c->save(); } $this->_sync_configuration(); $this->ajaxSuccess(1); } protected function _updateConfigurations($data) { foreach ($data as $item) { $c = new BConfigurations(); $c->setIsNewRecord(true); $db = $c->getDbConnection(); $command = $db->createCommand(); $command->delete('configurations', 'key=:key', array(':key' => $item['name'])); $command->insert('configurations', array('key' => $item['name'], 'value' => $item['value'])); } return true; } // function to retrieve page color palette protected function actiongetColorPalette() { return BSkins::getColorPalette(); } // function to load css from file public function readCSSContentFromFile($relative_path) { if (!is_file($relative_path)) { try { $file = file_get_contents($relative_path); } catch (Exception $e) { } } $file = file_get_contents($relative_path); return Functions::parseCSS($file); } //get all color schemes public function actiongetColorScheme() { if (isset($_REQUEST['id'])) { $s = Yii::app()->db->createCommand() ->select('*') ->from('tbl_colorscheme') ->where('id=:id', array(':id' => $id)) ->queryRow(); } else { $s = Yii::app()->db->createCommand() ->select('*') ->from('tbl_colorscheme') ->queryAll(); } $this->ajaxSuccess($s); } // convert active record to php object protected function ar_toObject($ar) { $record = $ar->attributes; $relations = $ar->relations(); foreach ($relations as $name => $relation) { if (isset($ar->$name)) { $relationValue = $ar->$name; if (is_array($relationValue)) { $record[$name] = array(); foreach ($relationValue as $v) { $record[$name][] = $v->attributes; } } elseif (isset($relationValue->attributes)) { $record[$name] = $relationValue->attributes; } else $record[$name] = $relationValue; } } return $record; } // convert active record to php array protected function ar_toArray($ar_list) { $rs = array(); foreach ($ar_list as $l) { $rs[] = $this->ar_toObject($l); } return $rs; } public function getPlanCondition($name, $type = 'string') { $context = Context::getContext(); if (isset($context->planCondition) && isset($context->planCondition[$name])) { $value = $context->planCondition[$name]; settype($value, $type); return $value; } } //get plain html template public function actiongetHtml() { $name = ''; if (isset($_REQUEST['name'])) { $name = $_REQUEST['name']; } $c = $this->_getTemplate($name); echo $c; } public function actiongetCustomModuleHtml() { $name = $_REQUEST['name']; $data = ModuleList::model()->find('name=:name', array(':name'=>$name)); $html = ''; if(isset($data)) { // $backend= $data->backend; $html = $this->renderPartial('modules/___custom', array('name'=>$name,'title'=>$data->display_name), true); } echo $html; } // get HTML template public function actiongetTemplate() { $name = ''; if (isset($_REQUEST['name'])) { $name = $_REQUEST['name']; } $c = $this->_getTemplate($name); $this->ajaxSuccess(array('html' => $c)); } public function _getTemplate($name = '') { if (strlen($name) == 0) { $class = __CLASS__; $name = str_replace($class, 'Manager', $name); } return $this->renderPartial($name, array(), true); } // function to set view path public function getViewPath() { return YiiBase::getPathOfAlias('application.views.builder')/*.DIRECTORY_SEPARATOR.$this->getId()*/; } //function to check user authentication public function checkAccess($roles) { return Yii::app()->user->checkAccess($roles); } public function actiongetConfigurationList() { $configurations = BConfigurations::model()->findAll(); $result = array(); foreach($configurations as $configuration) { $value = @unserialize($configuration->value); if($value !== false) { $result[$configuration->name] = $value; } else $result[$configuration->name] = $configuration->value; } $this->ajaxSuccess($result); } }
[+]
..
[-] SettingsManager.php
[edit]
[-] BaseManager.php
[edit]
[+]
settings
[-] BlogManager.php
[edit]
[+]
blog
[-] AssetManager.php
[edit]
[+]
customize
[-] ContentManager.php
[edit]
[+]
content
[-] CustomizeManager.php
[edit]
[-] EcommerceManager.php
[edit]