OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / s / css_add / include / container / admin_s_css_addWidgetContainer.php
1 <?php
2 /**
3  * コンテナクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
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
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17
18 class admin_s_css_addWidgetContainer extends BaseAdminWidgetContainer
19 {
20         private $serialNo;              // 選択中の項目のシリアル番号
21         private $serialArray = array();                 // 表示中のシリアル番号
22         private $langId;
23         private $configId;              // 定義ID
24         private $paramObj;              // パラメータ保存用オブジェクト
25         private $css;                   // CSS
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ファイル選択項目名ヘッダ
32         
33         /**
34          * コンストラクタ
35          */
36         function __construct()
37         {
38                 // 親クラスを呼び出す
39                 parent::__construct();
40         }
41         /**
42          * テンプレートファイルを設定
43          *
44          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
45          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
46          *
47          * @param RequestManager $request               HTTPリクエスト処理クラス
48          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
49          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
50          */
51         function _setTemplate($request, &$param)
52         {
53                 $task = $request->trimValueOf('task');
54                 if ($task == 'list'){           // 一覧画面
55                         return 'admin_list.tmpl.html';
56                 } else {                        // 一覧画面
57                         return 'admin.tmpl.html';
58                 }
59         }
60         /**
61          * テンプレートにデータ埋め込む
62          *
63          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
64          *
65          * @param RequestManager $request               HTTPリクエスト処理クラス
66          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
67          * @param                                                               なし
68          */
69         function _assign($request, &$param)
70         {
71                 $task = $request->trimValueOf('task');
72                 if ($task == 'list'){           // 一覧画面
73                         return $this->createList($request);
74                 } else {                        // 詳細設定画面
75                         return $this->createDetail($request);
76                 }
77         }
78         /**
79          * 詳細画面作成
80          *
81          * @param RequestManager $request               HTTPリクエスト処理クラス
82          * @param                                                               なし
83          */
84         function createDetail($request)
85         {
86                 // ページ定義IDとページ定義のレコードシリアル番号を取得
87                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
88                 
89                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
90                 $this->cssDir   =  $this->gEnv->getSystemRootPath() . self::CSS_DIR;            // CSSファイル読み込みディレクトリ
91                 
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
96                 
97                 // 入力値を取得
98                 $name   = $request->trimValueOf('item_name');                   // 定義名
99                 $this->css      = $request->valueOf('item_css');                // CSS
100                 
101                 // 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;
108                 }
109
110                 $replaceNew = false;            // データを再取得するかどうか
111                 if ($act == 'add'){// 新規追加
112                         // 入力チェック
113                         $this->checkInput($name, '名前');
114                         $this->checkInput($this->css, 'CSS');
115                         
116                         // 設定名の重複チェック
117                         for ($i = 0; $i < count($this->paramObj); $i++){
118                                 $targetObj = $this->paramObj[$i]->object;
119                                 if ($name == $targetObj->name){         // 定義名
120                                         $this->setUserErrorMsg('名前が重複しています');
121                                         break;
122                                 }
123                         }
124                         
125                         // エラーなしの場合は、データを登録
126                         if ($this->getMsgCount() == 0){
127                                 // 追加オブジェクト作成
128                                 $newObj = new stdClass;
129                                 $newObj->name   = $name;// 表示名
130                                 $newObj->css    = $this->css;                                   // CSS
131                                 $newObj->cssFiles = $this->cssFiles;                    // CSSファイル
132                                 
133                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
134                                 if ($ret){
135                                         $this->setGuidanceMsg('データを追加しました');
136                                         
137                                         $this->configId = $defConfigId;         // 定義定義IDを更新
138                                         $replaceNew = true;                     // データ再取得
139                                 } else {
140                                         $this->setAppErrorMsg('データ追加に失敗しました');
141                                 }
142                         }
143                 } else if ($act == 'update'){           // 設定更新のとき
144                         // 入力値のエラーチェック
145                         $this->checkInput($this->css, 'CSS');
146                         
147                         if ($this->getMsgCount() == 0){                 // エラーのないとき
148                                 // 現在の設定値を取得
149                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
150                                 if ($ret){
151                                         // ウィジェットオブジェクト更新
152                                         $targetObj->css         = $this->css;                                   // CSS
153                                         $targetObj->cssFiles = $this->cssFiles;                 // CSSファイル
154                                 }
155                                 
156                                 // 設定値を更新
157                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
158                                 if ($ret){
159                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
160                                         $replaceNew = true;                     // データ再取得
161                                 } else {
162                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
163                                 }
164                         }
165                 } else if ($act == 'select'){   // 定義IDを変更
166                         $replaceNew = true;                     // データ再取得
167                 } else {        // 初期起動時、または上記以外の場合
168                         // デフォルト値設定
169                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
170                         $replaceNew = true;                     // データ再取得
171                 }
172                 // 表示用データを取得
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ファイル
179                         }
180                         $this->serialNo = 0;
181                 } else {
182                         if ($replaceNew){// データ再取得時
183                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
184                                 if ($ret){
185                                         $name           = $targetObj->name;     // 名前
186                                         $this->css      = $targetObj->css;      // CSS
187                                         $this->cssFiles = $targetObj->cssFiles;                 // CSSファイル
188                                 }
189                         }
190                         $this->serialNo = $this->configId;
191                                 
192                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
193                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
194                 }
195
196                 // 設定項目選択メニュー作成
197                 $this->createItemMenu();
198                 
199                 // CSSファイル一覧作成
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;
204                 }
205                 $files = array_merge($files, $addFiles);
206                 if (empty($files)){
207                         $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');
208                 } else {
209                         $this->createCssFileList($files);
210                 }
211                 
212                 // 画面にデータを埋め込む
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を設定
219                 
220                 // ボタンの表示制御
221                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
222                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
223                 } else {
224                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
225                         
226                         // ヘルプの追加
227                         $this->convertHelp('update_button');
228                 }
229                 
230                 // ページ定義IDとページ定義のレコードシリアル番号を更新
231                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
232         }
233         /**
234          * 選択用メニューを作成
235          *
236          * @return なし                                               
237          */
238         function createItemMenu()
239         {
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;// 定義名
244                         $selected = '';
245                         if ($this->configId == $id) $selected = 'selected';
246
247                         $row = array(
248                                 'name' => $name,                // 名前
249                                 'value' => $id,         // 定義ID
250                                 'selected' => $selected // 選択中の項目かどうか
251                         );
252                         $this->tmpl->addVars('title_list', $row);
253                         $this->tmpl->parseTemplate('title_list', 'a');
254                 }
255         }
256         /**
257          * デフォルトの名前を取得
258          *
259          * @return string       デフォルト名                                              
260          */
261         function createDefaultName()
262         {
263                 $name = self::DEFAULT_NAME_HEAD;
264                 for ($j = 1; $j < 100; $j++){
265                         $name = self::DEFAULT_NAME_HEAD . $j;
266                         // 設定名の重複チェック
267                         for ($i = 0; $i < count($this->paramObj); $i++){
268                                 $targetObj = $this->paramObj[$i]->object;
269                                 if ($name == $targetObj->name){         // 定義名
270                                         break;
271                                 }
272                         }
273                         // 重複なしのときは終了
274                         if ($i == count($this->paramObj)) break;
275                 }
276                 return $name;
277         }
278         /**
279          * 一覧画面作成
280          *
281          * @param RequestManager $request               HTTPリクエスト処理クラス
282          * @param                                                               なし
283          */
284         function createList($request)
285         {
286                 // ページ定義IDとページ定義のレコードシリアル番号を取得
287                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
288                 
289                 $langId = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
290                 $act = $request->trimValueOf('act');
291                 
292                 if ($act == 'delete'){          // メニュー項目の削除
293                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
294                         $delItems = array();
295                         for ($i = 0; $i < count($listedItem); $i++){
296                                 // 項目がチェックされているかを取得
297                                 $itemName = 'item' . $i . '_selected';
298                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
299                                 
300                                 if ($itemValue){                // チェック項目
301                                         $delItems[] = $listedItem[$i];
302                                 }
303                         }
304                         if (count($delItems) > 0){
305                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
306                                 if ($ret){              // データ削除成功のとき
307                                         $this->setGuidanceMsg('データを削除しました');
308                                 } else {
309                                         $this->setAppErrorMsg('データ削除に失敗しました');
310                                 }
311                         }
312                 }
313                 // 定義一覧作成
314                 $this->createItemList();
315                 
316                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
317                 
318                 // ページ定義IDとページ定義のレコードシリアル番号を更新
319                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
320         }
321         /**
322          * 定義一覧作成
323          *
324          * @return なし                                               
325          */
326         function createItemList()
327         {
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;// 定義名
332                 
333                         // 使用数
334                         $defCount = 0;
335                         if (!empty($id)){
336                                 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
337                         }
338                         $operationDisagled = '';
339                         if ($defCount > 0) $operationDisagled = 'disabled';
340                         
341                         $row = array(
342                                 'index' => $i,
343                                 'id' => $id,
344                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
345                                 'name' => $this->convertToDispString($name),            // 名前
346                                 'def_count' => $defCount                                                        // 使用数
347                         );
348                         $this->tmpl->addVars('itemlist', $row);
349                         $this->tmpl->parseTemplate('itemlist', 'a');
350                         
351                         // シリアル番号を保存
352                         $this->serialArray[] = $id;
353                 }
354         }
355         /**
356          * CSSファイル一覧作成
357          *
358          * @param array $files          CSSファイルフルパス
359          * @return なし
360          */
361         function createCssFileList($files)
362         {
363                 for ($i = 0; $i < count($files); $i++){
364                         $path = str_replace($this->cssDir, '', $files[$i]);     // 相対パス
365                         $name = trim($path, DIRECTORY_SEPARATOR);
366                         
367                         $checked = '';
368                         if (in_array($path, $this->cssFiles)) $checked = 'checked';
369
370                         $row = array(
371                                 'index'         => $i + 1,
372                                 'value'         => $this->convertToDispString($path),                   // 値
373                                 'name'          => $this->convertToDispString($name),                   // 名前
374                                 'file_checked'  => $checked                     // 選択中かどうか
375                         );
376                         $this->tmpl->addVars('itemlist', $row);
377                         $this->tmpl->parseTemplate('itemlist', 'a');
378                 }
379         }
380         /**
381          * 指定ディレクトリのファイル一覧を取得
382          *
383          * @param string $path          読み込みディレクトリ
384          * @return array                        ファイル名一覧
385          */
386         function getFiles($path)
387         {
388                 $files = array();
389                 if (is_dir($path)){
390                         $dir = dir($path);
391                         while (($file = $dir->read()) !== false){
392                                 if ($file == '.' || $file == '..') continue;
393                                 
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;
402                                         }
403                                 }
404                         }
405                         $dir->close();
406                 }
407                 return $files;
408         }
409 }
410 ?>