7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
9 * @package Magic3 Framework
10 * @author 平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11 * @copyright Copyright 2006-2012 Magic3 Project.
12 * @license http://www.gnu.org/copyleft/gpl.html GPL License
13 * @version SVN: $Id: admin_s_css_addWidgetContainer.php 4549 2012-01-02 02:10:58Z fishbone $
14 * @link http://www.magic3.org
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
18 class admin_s_css_addWidgetContainer extends BaseAdminWidgetContainer
20 private $serialNo; // 選択中の項目のシリアル番号
21 private $serialArray = array(); // 表示中のシリアル番号
23 private $configId; // 定義ID
24 private $paramObj; // パラメータ保存用オブジェクト
26 private $cssFiles; // CSSファイル
27 private $cssDir; // CSSファイル読み込みディレクトリ
28 const DEFAULT_NAME_HEAD = '名称未設定'; // デフォルトの設定名
29 const DEFAULT_CSS = "input.button {\n border:2px outset #FF3366;\n background-color:#FF3366;\n}\n";
30 const CSS_DIR = '/resource/css'; // CSSファイル格納ディレクトリ
31 const FIELD_HEAD = 'item_file_'; // CSSファイル選択項目名ヘッダ
36 function __construct()
39 parent::__construct();
44 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
45 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
47 * @param RequestManager $request HTTPリクエスト処理クラス
48 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
49 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
51 function _setTemplate($request, &$param)
53 $task = $request->trimValueOf('task');
54 if ($task == 'list'){ // 一覧画面
55 return 'admin_list.tmpl.html';
57 return 'admin.tmpl.html';
63 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
65 * @param RequestManager $request HTTPリクエスト処理クラス
66 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
69 function _assign($request, &$param)
71 $task = $request->trimValueOf('task');
72 if ($task == 'list'){ // 一覧画面
73 return $this->createList($request);
75 return $this->createDetail($request);
81 * @param RequestManager $request HTTPリクエスト処理クラス
84 function createDetail($request)
86 // ページ定義IDとページ定義のレコードシリアル番号を取得
87 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
89 $this->langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
90 $this->cssDir = $this->gEnv->getSystemRootPath() . self::CSS_DIR; // CSSファイル読み込みディレクトリ
92 $act = $request->trimValueOf('act');
93 $this->serialNo = $request->trimValueOf('serial'); // 選択項目のシリアル番号
94 $this->configId = $request->trimValueOf('item_id'); // 定義ID
95 if (empty($this->configId)) $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
98 $name = $request->trimValueOf('item_name'); // 定義名
99 $this->css = $request->valueOf('item_css'); // CSS
102 $this->cssFiles = array();
103 $files = $this->getFiles($this->cssDir);
104 for ($i = 0; $i < count($files); $i++){
105 $itemName = self::FIELD_HEAD . ($i + 1);
106 $itemValue = $request->trimValueOf($itemName);
107 if (!empty($itemValue)) $this->cssFiles[] = $itemValue;
110 $replaceNew = false; // データを再取得するかどうか
111 if ($act == 'add'){// 新規追加
113 $this->checkInput($name, '名前');
114 $this->checkInput($this->css, 'CSS');
117 for ($i = 0; $i < count($this->paramObj); $i++){
118 $targetObj = $this->paramObj[$i]->object;
119 if ($name == $targetObj->name){ // 定義名
120 $this->setUserErrorMsg('名前が重複しています');
126 if ($this->getMsgCount() == 0){
128 $newObj = new stdClass;
129 $newObj->name = $name;// 表示名
130 $newObj->css = $this->css; // CSS
131 $newObj->cssFiles = $this->cssFiles; // CSSファイル
133 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
135 $this->setGuidanceMsg('データを追加しました');
137 $this->configId = $defConfigId; // 定義定義IDを更新
138 $replaceNew = true; // データ再取得
140 $this->setAppErrorMsg('データ追加に失敗しました');
143 } else if ($act == 'update'){ // 設定更新のとき
145 $this->checkInput($this->css, 'CSS');
147 if ($this->getMsgCount() == 0){ // エラーのないとき
149 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
152 $targetObj->css = $this->css; // CSS
153 $targetObj->cssFiles = $this->cssFiles; // CSSファイル
157 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
159 $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
160 $replaceNew = true; // データ再取得
162 $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
165 } else if ($act == 'select'){ // 定義IDを変更
166 $replaceNew = true; // データ再取得
167 } else { // 初期起動時、または上記以外の場合
169 $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
170 $replaceNew = true; // データ再取得
173 if (empty($this->configId)){ // 新規登録の場合
174 $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
175 if ($replaceNew){ // データ再取得時
176 $name = $this->createDefaultName(); // デフォルト登録項目名
177 $this->css = self::DEFAULT_CSS; // CSS
178 $this->cssFiles = array(); // CSSファイル
182 if ($replaceNew){// データ再取得時
183 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
185 $name = $targetObj->name; // 名前
186 $this->css = $targetObj->css; // CSS
187 $this->cssFiles = $targetObj->cssFiles; // CSSファイル
190 $this->serialNo = $this->configId;
192 // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
193 if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
197 $this->createItemMenu();
200 $addFiles = array(); // すでに登録されているので追加が必要なファイル
201 for ($i = 0; $i < count($this->cssFiles); $i++){
202 $file = $this->cssDir . $this->cssFiles[$i];
203 if (!in_array($file, $files)) $addFiles[] = $file;
205 $files = array_merge($files, $addFiles);
207 $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');
209 $this->createCssFileList($files);
213 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId); // 定義ID
214 $this->tmpl->addVar("item_name_visible", "name", $name);
215 $this->tmpl->addVar("_widget", "css", $this->css);
216 $this->tmpl->addVar("_widget", "css_dir", $this->cssDir); // CSSファイル読み込みディレクトリ
217 $this->tmpl->addVar("_widget", "file_count", count($files)); // CSSファイル数
218 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
221 if (empty($this->serialNo)){ // 新規追加項目を選択しているとき
222 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
224 $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
227 $this->convertHelp('update_button');
230 // ページ定義IDとページ定義のレコードシリアル番号を更新
231 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
238 function createItemMenu()
240 for ($i = 0; $i < count($this->paramObj); $i++){
241 $id = $this->paramObj[$i]->id;// 定義ID
242 $targetObj = $this->paramObj[$i]->object;
243 $name = $targetObj->name;// 定義名
245 if ($this->configId == $id) $selected = 'selected';
248 'name' => $name, // 名前
249 'value' => $id, // 定義ID
250 'selected' => $selected // 選択中の項目かどうか
252 $this->tmpl->addVars('title_list', $row);
253 $this->tmpl->parseTemplate('title_list', 'a');
259 * @return string デフォルト名
261 function createDefaultName()
263 $name = self::DEFAULT_NAME_HEAD;
264 for ($j = 1; $j < 100; $j++){
265 $name = self::DEFAULT_NAME_HEAD . $j;
267 for ($i = 0; $i < count($this->paramObj); $i++){
268 $targetObj = $this->paramObj[$i]->object;
269 if ($name == $targetObj->name){ // 定義名
274 if ($i == count($this->paramObj)) break;
281 * @param RequestManager $request HTTPリクエスト処理クラス
284 function createList($request)
286 // ページ定義IDとページ定義のレコードシリアル番号を取得
287 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
289 $langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
290 $act = $request->trimValueOf('act');
292 if ($act == 'delete'){ // メニュー項目の削除
293 $listedItem = explode(',', $request->trimValueOf('seriallist'));
295 for ($i = 0; $i < count($listedItem); $i++){
297 $itemName = 'item' . $i . '_selected';
298 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
300 if ($itemValue){ // チェック項目
301 $delItems[] = $listedItem[$i];
304 if (count($delItems) > 0){
305 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
306 if ($ret){ // データ削除成功のとき
307 $this->setGuidanceMsg('データを削除しました');
309 $this->setAppErrorMsg('データ削除に失敗しました');
314 $this->createItemList();
316 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
318 // ページ定義IDとページ定義のレコードシリアル番号を更新
319 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
326 function createItemList()
328 for ($i = 0; $i < count($this->paramObj); $i++){
329 $id = $this->paramObj[$i]->id;// 定義ID
330 $targetObj = $this->paramObj[$i]->object;
331 $name = $targetObj->name;// 定義名
336 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
338 $operationDisagled = '';
339 if ($defCount > 0) $operationDisagled = 'disabled';
344 'ope_disabled' => $operationDisagled, // 選択可能かどうか
345 'name' => $this->convertToDispString($name), // 名前
346 'def_count' => $defCount // 使用数
348 $this->tmpl->addVars('itemlist', $row);
349 $this->tmpl->parseTemplate('itemlist', 'a');
352 $this->serialArray[] = $id;
358 * @param array $files CSSファイルフルパス
361 function createCssFileList($files)
363 for ($i = 0; $i < count($files); $i++){
364 $path = str_replace($this->cssDir, '', $files[$i]); // 相対パス
365 $name = trim($path, DIRECTORY_SEPARATOR);
368 if (in_array($path, $this->cssFiles)) $checked = 'checked';
372 'value' => $this->convertToDispString($path), // 値
373 'name' => $this->convertToDispString($name), // 名前
374 'file_checked' => $checked // 選択中かどうか
376 $this->tmpl->addVars('itemlist', $row);
377 $this->tmpl->parseTemplate('itemlist', 'a');
383 * @param string $path 読み込みディレクトリ
384 * @return array ファイル名一覧
386 function getFiles($path)
391 while (($file = $dir->read()) !== false){
392 if ($file == '.' || $file == '..') continue;
394 $filePath = $path . '/' . $file;
395 if (strncmp($file, '.', 1) != 0 && // 「.」で始まる名前のディレクトリ、ファイルは読み込まない
396 strncmp($file, '_', 1) != 0){ // 「_」で始まる名前のディレクトリ、ファイルは読み込まない
397 if (is_dir($filePath)){ // ディレクトリのとき
398 $addFiles = $this->getFiles($filePath);
399 $files = array_merge($files, $addFiles);
400 } else if (is_file($filePath)){// ファイルのとき
401 $files[] = $filePath;