* @copyright Copyright(c) 2010 C-UNIT SQUARE Co.,Ltd. All Rights Reserved. * @license C-UNIT SQUARE Co.,Ltd. ------------------------------------------------------------------------*/ /** * モデル抽象クラスを呼び出す */ require_once('AbstructModel.php'); /** * Translation */ require_once('TranslateCls.php'); /** * class connect ftp */ require_once('FtpCls.php'); /** * コンテンツのステータス */ define('CMS_STATUS_CREATED', 0); // 作成済み define('CMS_STATUS_PUBLIC', 1); // 公開中 define('CMS_STATUS_BACK', 2); // 差し戻し define('CMS_STATUS_EDITED', 3); // 編集済み define('CMS_STATUS_APPROVAL', 4); // 承認中 define('CMS_STATUS_EDITING', 5); // 編集中 define('CMS_STATUS_PRIVATE', 6); // 未公開 /** * 単純承認か多段階承認か */ define('CMS_FLOW_MODE_NORMAL', 0); define('CMS_FLOW_MODE_MULTI', 1); class CmsObjectCls extends AbstructModel { /** * @var string モデルクラス名のフォーマット */ protected $_name_format = '{$model}Cls'; /** * @var string 設定ファイルのセクションキー、データベース情報を指す */ protected $_db_key = 'db_cms'; /** * @var array 状態定義リスト */ protected $_status_list; /** * @var object CSVのヘッダーと連想配列のキーを関連付ける定義を格納した連想配列(連想配列キー=>CSVヘッダー) */ protected $_csvHeader = array(); /** * @var object 同値チェックするキーと検索条件の連想配列 */ protected $_objAlreadyKey = array( 'name' => '', 'username' => '', ); /** * @var array プレビューに使うデータ */ protected $_objPreviewData = array(); /** * @var boolean 表画面作成処理中かどうか */ static protected $_bFrontCreate = false; /** * @var boolean プレビュー画面作成処理中かどうか */ static protected $_bPreviewCreate = false; /** * @var boolean 記号チェックを行うかどうか */ protected $_use_check_nochar = true; /** * @var object translation */ public $_objTranslation; /** * @var object Date Format */ public $_objDateFormat = 'Y/m/d'; /**---------------------------------------------------------------------- * 初期化 ------------------------------------------------------------------------*/ public function init() { parent::init(); /* * C-Unit 2011-10-31 Start * #5593 * Load languale for translation */ $objTranslate = new TranslateCls; $this->_objTranslation = $objTranslate->_objTranslation; /* * C-Unit 2011-10-31 End */ /* * C-Unit 2011-10-31 Start * #5593 * Load date format for translation */ $objLanguage = new Zend_Session_Namespace('language'); $this->_objDateFormat = $objLanguage->strDateFormat; // Smartyテンプレートで扱うステータス $objStatus = array( 'created' => CMS_STATUS_CREATED, 'public' => CMS_STATUS_PUBLIC, 'back' => CMS_STATUS_BACK, 'edited' => CMS_STATUS_EDITED, 'approval' => CMS_STATUS_APPROVAL, 'editing' => CMS_STATUS_EDITING, 'private' => CMS_STATUS_PRIVATE, ); $this->_status_list = $objStatus; // Smartyテンプレートで扱うステータス表示名を登録 $arrName = parse_ini_file($this->_config['config_file']['cms_status']); foreach ($objStatus as $key => $val) { $this->_status_list[] = $arrName[$key]; } } /**---------------------------------------------------------------------- * 表画面作成フラグを取得する * @return boolean 表画面作成フラグ ------------------------------------------------------------------------*/ static public function is_front() { return self::$_bFrontCreate; } /**---------------------------------------------------------------------- * 表画面作成フラグを設定する * @param boolean $flag 表画面作成フラグ ------------------------------------------------------------------------*/ static public function set_front_flag($flag) { self::$_bFrontCreate = $flag; } /**---------------------------------------------------------------------- * プレビュー画面作成フラグを取得する * @return boolean 表画面作成フラグ ------------------------------------------------------------------------*/ static public function is_preview() { return self::$_bPreviewCreate; } /**---------------------------------------------------------------------- * プレビュー画面作成フラグを設定する * @param boolean $flag 表画面作成フラグ ------------------------------------------------------------------------*/ static public function set_preview_flag($flag) { self::$_bPreviewCreate = $flag; } /**---------------------------------------------------------------------- * 承認情報を設定する * @param string $id 設定対象オブジェクトの識別番号 * @param string $id_approval 承認情報の識別番号 * @return number 変更したレコードの数 ------------------------------------------------------------------------*/ public function set_approval($id, $id_approval) { $intCount = $this->_db->update($this->_table, array('id_approval' => $id_approval), '`id`="' . $id . '"'); unset(self::$_get_item[$id]); return $intCount; } /**---------------------------------------------------------------------- * 更新履歴をセットする * @param array $model 更新履歴として保存する内容 ------------------------------------------------------------------------*/ public function set_history($model) { $objData = $this->get($this->_last_id); $objDataName = $objData; unset($objData['name']); unset($objData['url']); unset($objData['id_contents_parent']); if (sizeof($objDataName) >0 ) { if (!(3 == count($model) && isset($model['status']) && isset($model['id_account']))) { $objHistoryData = array( 'name' => '', 'data' => serialize($objData), 'cms_type' => $this->_name, 'head_id' => $objData['id'], 'head_name' => $objDataName['name'], 'head_status' => $objData['status'], 'head_date_create' => $objData['date_create'], 'head_date_update' => $objData['date_update'], 'head_id_account' => $objData['id_account'], ); $objHistoryName = array( 'head_name' => $objDataName['name'], ); $objHistory = $this->createObject('history'); $objHistory->set_where($objHistoryName, '`head_id`="' . $objData['id'] . '"'); $objHistory->set($objHistoryData); } } } /**---------------------------------------------------------------------- * 更新履歴をセットする * @param string $id - head id ------------------------------------------------------------------------*/ public function get_history($id) { $objHistory = $this->createObject('history'); $arrHistory = $objHistory->get_where('`head_id`="' . $id . '"'); return $arrHistory; } /**---------------------------------------------------------------------- * 編集中時間を設定する * @param object $params 要求を格納した連想配列 * @return number 変更したレコードの数 ------------------------------------------------------------------------*/ public function set_edit_time($params) { $accountEdit = ''; // init variable account edit if (!isset($params['time']) || 0 != $params['time']) { $edit_time = $this->_config['cms']['edit_time']; $edit_time = mktime(date('H'), date('i'), date('s') + $edit_time, date('m'), date('d'), date('Y')); $edit_time = date('Y-m-d H:i:s', $edit_time); /* * C-Unit 2011-10-13 strart * #5302 * Add user edit last */ $accountEdit = $this->_auth->get_login_user('id'); // get id account edit } else { $accountEdit = ''; // set account id is bank $edit_time = ''; } /* * C-Unit 2011-10-13 strart * #5302,#5300,#5301 * Add user edit last */ $arrParamsEdit= array();// init array switch($this->_name) { case 'contents':// table cms_contents case 'item':// table cms_item case 'inquiry': // table cms_inquiry case 'template':// table cms_template case 'block': // table cms_block case 'layout': // table cms_layout case 'css': // table cms_css case 'menu':// table cms_menu case 'account':// table cms_account $arrParamsEdit = array('edit_time' => $edit_time, 'id_account_edit' => $accountEdit ); break; default: $arrParamsEdit = array('edit_time' => $edit_time ); break; } //$intCount = $this->_db->update($this->_table, array('edit_time' => $edit_time), '`id`="' . $params['id'] . '"'); $intCount = $this->_db->update($this->_table, $arrParamsEdit, '`id`="' . $params['id'] . '"'); /* * C-Unit 2011-10-11 and */ $session = new Zend_Session_Namespace('edit_data'); if (!isset($session->id_list)) { $session->id_list = array(); } if ('' != $edit_time) { // 編集中CMSオブジェクトとして登録 $session->id_list[$params['id']] = true; } else { unset($session->id_list[$params['id']]); } return $intCount; } /**---------------------------------------------------------------------- * CMS状態を設定する * @param string $id 設定対象オブジェクトの識別番号 * @param number $status CMS状態 ------------------------------------------------------------------------*/ public function set_status($id, $status) { $this->set(array( 'id' => $id, 'status' => $status, )); } /**---------------------------------------------------------------------- * データベースの指定テーブルから全レコードを取得する * @return array 全レコードのリスト ------------------------------------------------------------------------*/ public function getAll() { $list = parent::getAll(); $list = $this->_org_filter($list, false); return $list; } /**---------------------------------------------------------------------- * データベースを検索する * @param string $query where 以降のSQL文 * @param string $sort 検索結果を表示する際の並び変えに使用される項目 * @return array 検索結果レコードのリスト ------------------------------------------------------------------------*/ public function search($query, $bOrgFilter = false, $sort = 'date_update' , $strCSS = '', $strLimit = '') { $list = parent::search($query, $sort, $strLimit); $list = $this->_org_filter($list, $bOrgFilter, $strCSS); return $list; } /**---------------------------------------------------------------------- * オリジナルIDを対象にしたフィルタを適用する * @param array $list 対象となるコンテンツの配列 * @param string $bUse true:オリジナルを含める false:オリジナルを除外する(CSS以外の場合に使用) * @param string $strCSS 対象となるコンテンツがCSSかどうか * @return array フィルタリングされたコンテンツの配列 ------------------------------------------------------------------------*/ public function _org_filter($list, $bUse , $strCSS ='' ) { // オリジナルIDの一覧を取得 $org_id = ''; foreach ($list as $i => $objData) { if (isset($objData['id_org']) && '-' != $objData['id_org']) { $org_id .= ',' . $objData['id']; } } $result_list = array(); foreach ($list as $i => $objData) { if($strCSS != 'css') { // Check is list show in page of user if($bUse == true) { $result_list[] = $objData; } else { if ('-'!= $objData['id_org']) { $result_list[] = $objData; } } } else { $result_list[] = $objData; } } return $result_list; } /**---------------------------------------------------------------------- * 取得したデータを加工する * @param object $data 加工するデータを格納した連想配列 * @return object 加工したデータを格納した連想配列 ------------------------------------------------------------------------*/ protected function _get_process($data) { $data = parent::_get_process($data); if (0 != count($data)) { if (isset($data['edit_time']) && '' != $data['edit_time']) { $data['status'] = CMS_STATUS_EDITING; } } return $data; } /**---------------------------------------------------------------------- * レコードを1件登録する * @param object $model 登録するレコードを格納した連想配列 * @return integer 処理結果コード ------------------------------------------------------------------------*/ public function set($model) { $this->_log->start('set'); $this->_log->debug($model); $objDataOrg = null; $bolFTP = false; /* * C-Unit 2011-10-17 strart * #5397 * Add user id update */ switch($this->_name) { case 'admin': // admin /* * C-Unit 2011-10-31 strart * #5397 * create file getcontents, upload site_template using FTP */ if($this->_config['system']['ftp_transfer'] == '1'){ $bolFTP = true; } break; /* * C-Unit 2011-10-31 end */ /* case 'contents': // contents case 'item': // item case 'inquiry': // inquiry case 'template':// template case 'block': // block case 'layout': // layout case 'css': // css case 'menu':// menu case 'account':// account $model['id_account_update'] = $this->_auth->get_login_user('id'); // set id_account_update break; */ } /* * C-Unit 2011-10-17 end */ if (isset($model['id']) && '' != $model['id']) { $objDataOrg = $this->_get_main($model['id']); // Remove the field id_html because it's an auto-increment field (PRIMARY KEY) unset($objDataOrg['id_html']); if (0 != count($objDataOrg)) { // ステータス更新 /* * C-Unit 2011-12-07 * #6332 * check approvel public */ if (2 == count($model) && isset($model['status']) && $model['status'] == 1) { if (CMS_STATUS_PUBLIC == $model['status']) { // 待避オリジナル削除 parent::del($objDataOrg['id_org']); // メインデータの id_org を空にする $this->_db->update($this->_table, array('id_org' => ''), '`id`="' . $model['id'] . '"'); } } // ステータス更新ではない else { // 公開中 if (CMS_STATUS_PUBLIC == $objDataOrg['status']) { // 待避オリジナル作成 $objDataOrg['id'] = uniqid32(); $objDataOrg['id_org'] = '-'; $this->_db->insert($this->_table, $objDataOrg); // 待避オリジナルを指す $model['id_org'] = $objDataOrg['id']; } } } } // ステータス指定が無い場合は自動設定 if (!isset($model['status'])) { $model['status'] = CMS_STATUS_EDITED; } // What'sNew 自動保存データ構築 { if (isset($model['to_news'])) { $to_news = $model['to_news']; unset($model['to_news']); } else { $to_news = 0; } if (isset($model['to_news_contents'])) { $to_news_contents = $model['to_news_contents']; unset($model['to_news_contents']); } else { $to_news_contents = array(); } /** * C-Unit 2011-10-27 Start * * Change date format to SQL format: 'Y/m/d' */ $objLanguage = new Zend_Session_Namespace('language'); $objSite = new Util_SiteCls($this->_config); if (isset($model['date_entry'])) { $model['date_entry'] = $objSite->convertToSqlDate($model['date_entry'], $objLanguage->strDateFormat); } if (isset($model['range_start'])) { $model['range_start'] = $objSite->convertToSqlDate($model['range_start'], $objLanguage->strDateFormat); } if (isset($model['range_end'])) { $model['range_end'] = $objSite->convertToSqlDate($model['range_end'], $objLanguage->strDateFormat); } /** * C-Unit 2011-10-27 End */ } // 保存者のIDを記録 /* * C-Unit 2011-10-17 strart * #5397 * if update */ //if (2 == count($model) && isset($model['status']) && (null != $objDataOrg && isset($objDataOrg['id_account']))) { if (isset($model['status']) && (null != $objDataOrg && isset($objDataOrg['id_account']))) { $model['id_account'] = $objDataOrg['id_account']; $is_status_update = true; } else { $model['id_account'] = $this->_auth->get_login_user('id'); $is_status_update = false; } // データ更新の場合は編集中時間を空にする if (isset($model['id']) && '' != $model['id']) { $model['edit_time'] = ''; } // 通常登録 $result = parent::set($model); if (!Message::isError($result)) { // 登録したデータ $objData = $this->get($this->_last_id); // What'sNew 自動保存 if (1 == $to_news) { $this->to_news($objData, $to_news_contents); } } /* * C-Unit 2011-10-31 strart * #5397 * create file getcontents, upload site_template using FTP */ if($bolFTP == true){ $this->_uploadNewFileFTP(); } $this->_log->end(); return $result; } /**---------------------------------------------------------------------- * レコードを1件削除する * @param string $id 対象レコードの主キー * @return integer 処理結果コード ------------------------------------------------------------------------*/ public function del($id) { $this->_log->start('del'); $objData = $this->query_fetch('select * from `' . $this->_table . '` where `id`="' . $id . '"'); // 待避オリジナルを持っているか? if ('' != $objData['id_org'] && '-' != $objData['id_org']) { // 待避オリジナルを消す $result = $this->del($objData['id_org']); } $result = parent::del($id); if (!Message::isError($result)) { // 履歴も削除 $objHistory = $this->createObject('history'); $objHistory->del_where('`head_id`="' . $id . '"'); } $this->_log->end(); return $result; } /**---------------------------------------------------------------------- * データを検証する * @param object $model 処理対象とするモデルデータ * @param string $validate_name 処理対象とするモデルデータ * @return array 検証結果のエラーを格納した配列 ------------------------------------------------------------------------*/ public function check($model, $validate_name = null) { $this->_log->start('check'); /** * C-Unit 2011-12-01 Del * #5826,#5784 * Move to check function in NewCls.php */ //if(isset($model['name']) == true){ //$model['name'] = trim(preg_replace ('/<[^>]*>(?: |\s)*/', '', $model['name']), chr(0xC2) . chr(0xA0)); //$model['name'] = str_replace( ' ', '', $model['name']); // trim whitespace 2 byte //} /** * C-Unit 2011-12-01 End */ $arrError = parent::check($model, $validate_name); /** * C-Unit 2011-11-01 * Move check_nochar function to Util_SiteCls */ $objSiteMgr = new Util_SiteCls($this->_config); // 名前には記号を使わせない if (isset($model['name']) && isset($this->_arrParamName['name'])) { if (true == $this->_use_check_nochar) { $arrError = $objSiteMgr->check_nochar($model, $arrError, array('name' => $this->_arrParamName['name'])); // " を使っていたらこの場で終了 if (false !== strpos($model['name'], '"')) { $this->_log->end(); return $arrError; } } } // Check existing username if (isset($model['username']) && isset($this->_arrParamName['username'])) { if (true == $this->_use_check_nochar) { $arrError = $objSiteMgr->check_nochar($model, $arrError, array('username' => $this->_arrParamName['username'])); // " を使っていたらこの場で終了 if (false !== strpos($model['username'], '"')) { $this->_log->end(); return $arrError; } } unset($this->_objAlreadyKey['name']); } $arrError = $this->check_already($model, $arrError); $this->_log->end(); return $arrError; } /**---------------------------------------------------------------------- * 同値チェック ------------------------------------------------------------------------*/ public function check_already($model, $arrError) { $this->_log->start('check_already'); $arrAlreadyError = array(); foreach ($this->_objAlreadyKey as $key => $condition) { if (isset($model[$key])) { $arrAlreadyError[$key]['already'] = array(); $where = ''; if (isset($model['id']) && '' != $model['id']) { $where .= '(`id`!="' . $model['id'] . '") and '; } $where .= '(`' . $key . '`="' . $model[$key] . '")'; if ('' != $condition) { $where .= ' and (' . $condition . ')'; } // オリジナルIDがハイフンではないものをチェック対象とする $where .= ' and (`id_org`!="-")'; // 同名を取得 $arrDataList = $this->search($where); // 見つかったか? if (0 != count($arrDataList) && isset($this->_arrParamName[$key])) { // 同名エラー /** * C-Unit 2011-10-31 * Translate the text 'は既に使用されています。' */ $arrAlreadyError[$key]['already'][] = $this->_arrParamName[$key] . '[' . $model[$key] . ']' . $this->_objTranslation->_('CmsObjectCls_msg0'); } } } // 返却用に追加 foreach ($arrAlreadyError as $key => $val) { foreach ($arrAlreadyError[$key]['already'] as $i => $val) { if ('' != $val) { $arrError[$key]['already'] = $arrAlreadyError[$key]['already']; break; } } } $this->_log->end(); return $arrError; } /**---------------------------------------------------------------------- * プレビューデータを取得する ------------------------------------------------------------------------*/ public function preview($model) { $session = new Zend_Session_Namespace('preview'); $session->model = $model; return ''; } /**---------------------------------------------------------------------- * What'sNew を連動して登録する ------------------------------------------------------------------------*/ public function to_news($model, $to_news_contents = array()) { $this->_log->start('to_news'); if ('news' == $this->_name) { $this->_log->end(); return RETURN_NORMAL; } // 基本登録データ取得 $to_news_param = parse_ini_file($this->_config['config_file']['to_news'], true); // デフォルトデータ $news_model = $to_news_param['default']; // 個別データ登録 foreach ($to_news_param[$this->_name] as $key => $val) { $news_model[$key] = $val; } // 公開サイトでは、What'sNewに自動生成ページの更新情報が掲載される場合、リンクが付く if ($this->_name == 'item'){ $news_model['data'] = '{$id}'; } // 登録データで更新 foreach ($news_model as $key => $val) { foreach ($model as $owner_key => $owner_val) { if ($owner_key != 'category') { $val = str_replace('{$' . $owner_key . '}', $owner_val, $val); } } $news_model[$key] = $val; } // サイトリスト $news_model['id_site_list'] = $to_news_contents; $objNews = $this->createObject('news'); $result = $objNews->set($news_model); $this->_log->end(); return $result; } /**---------------------------------------------------------------------- * 状態定義リストを取得する * @return array 状態定義リスト ------------------------------------------------------------------------*/ public function get_status_list() { $this->_log->start('get_status_list'); $this->_log->end(); return $this->_status_list; } /**---------------------------------------------------------------------- * 複製対象のレコードを更新する * @param object $model 複製するレコードを格納した連想配列 * @param object $uniq_list 一意な値とするキーとフォーマットの組を格納した連想配列 * 指定するとテーブル内で一意な値となるように複製する * @return object 更新後の複製レコードを格納した連想配列 ------------------------------------------------------------------------*/ protected function _cpy_update($model, $uniq_list = array()) { $this->_log->start('_cpy_update'); $uniq_list[] = 'name'; $model = $this->_copy_uniq_model($model, $uniq_list); $model['status'] = CMS_STATUS_EDITED; $this->_log->end(); return $model; } /**---------------------------------------------------------------------- * Get list record data matching with list of input ids in the current table model * @param string the id list string, such as with id1='id_value01', id2='id_value02' * => the ids will be id_value01,id_value02 * @return array the list records matching with list of input ids ------------------------------------------------------------------------*/ public function getListRecords($ids) { $this->_log->start('get'); // Process the id list string $arrIds = explode(',', $ids); $strIds = '\'' . implode('\',\'',$arrIds) . '\''; $strSql = 'select * from ' . $this->_table . ' where `id` IN (' . $strIds . ')'; $arrItem = $this->query_fetch_all($strSql); $this->_log->end(); return $arrItem; } /**---------------------------------------------------------------------- * Get page_auth_public status by account id * @param string $strAccountId * @return string return page_auth_public status ------------------------------------------------------------------------*/ public function getApproveAuth($strAccountId) { $this->_log->start('get'); // Get page_auth by account id $arrPageAuth = $this->query_fetch_all('select page_auth from `' . $this->_config['db_cms']['prefix'] . 'account` where `id`="' . $strAccountId . '"'); // decode page_auth value $arrApprovals = unserialize($arrPageAuth[0]['page_auth']); $this->_log->end(); // return page_auth_public status. return $arrApprovals["page_auth_public"]; } /**---------------------------------------------------------------------- * Get last approver id * @return string return approver id ------------------------------------------------------------------------*/ public function getLastApprove() { $this->_log->start('get'); // Get approver id $arrPageAuth = $this->query_fetch_all("select id from `" . $this->_config['db_cms']['prefix'] . "account` where `page_auth` LIKE '%page_auth_public\";s:3:\"all%'"); $this->_log->end(); // return approver id return $arrPageAuth[0]["id"]; } /**---------------------------------------------------------------------- * upload file connect FTP * @param null ------------------------------------------------------------------------*/ protected function _uploadNewFileFTP() { $objFtp = new FtpCls(); // create file getcontents.php $userSession = new Zend_Session_Namespace('userinfo'); if ($userSession->user_id != '') { $userId = $userSession->user_id; } else { $userId = ''; } $objPath = new PathMgr($this->_config); $path_system_user_root = $objPath->get('tpl', 'system_user_root') . '/'; $file = $this->_config['path']['root_system'].'/getcontents.php'; $strPhp = file_get_contents($file); $strPhp = str_replace('@user_id', $userId, $strPhp); $strPhp = str_replace('@path_system_user_root', $path_system_user_root, $strPhp); if($objFtp->createFile('', 'getcontents.php',$strPhp) == false){ return false; } // upload site_template $objFtp->copyDir($this->_config['path']['root_system'] . '/' . $this->_config['path']['site_template'], ''); } }