OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / picasa / include / container / admin_picasaWidgetContainer.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-2011 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_picasaWidgetContainer.php 4233 2011-07-25 09:39:04Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17
18 class admin_picasaWidgetContainer extends BaseAdminWidgetContainer
19 {
20         private $sysDb; // DB接続オブジェクト
21         private $serialNo;              // 選択中の項目のシリアル番号
22         private $serialArray = array();                 // 表示中のシリアル番号
23         private $langId;
24         private $configId;              // 定義ID
25         private $paramObj;              // パラメータ保存用オブジェクト
26         private $displayTypeArray;              // Picasa表示タイプ選択用
27         private $displayType;                   // Picasa表示タイプ
28         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
29         const CONTENT_WIDGET_ID = 's/content';                  // コンテンツ編集ウィジェット
30         const DEFAULT_DISPLAY_TYPE = 'title_image';             // デフォルトのPicasa表示タイプ
31         const DEFAULT_IMAGE_WIDTH = 64;                 // 画像幅
32         const DEFAULT_IMAGE_HEIGHT = 64;                        // 画像幅
33         const DEFAULT_IMAGE_STYLE = 'margin:5px;';      // 画像スタイル
34         const DEFAULT_ALBUM_COUNT = 10;                 // アルバム数
35         const DEFAULT_IMAGE_COUNT = 10;                 // 画像数
36         const DEFAULT_COL_COUNT = 2;                    // カラム数
37         const MAX_RESULT_COUNT = 300;                   // 問い合わせ結果の取得数
38         const DEFAULT_GOOGLE_LANG = 'ja';                       // Googleの表示言語 en_US
39         
40         /**
41          * コンストラクタ
42          */
43         function __construct()
44         {
45                 // 親クラスを呼び出す
46                 parent::__construct();
47                 
48                 // DBオブジェクト作成
49                 $this->sysDb = $this->gInstance->getSytemDbObject();
50                 
51                 // Picasa表示タイプ選択用
52                 $this->displayTypeArray = array(        array(  'name' => 'アルバムタイトル',                           'value' => 'title'),
53                                                                                         array(  'name' => 'アルバムタイトルと画像',                  'value' => 'title_image'),
54                                                                                         array(  'name' => 'ランダムアルバム',                           'value' => 'random_album'),
55                                                                                         array(  'name' => '選択アルバム',                                 'value' => 'select_album'));
56         }
57         /**
58          * テンプレートファイルを設定
59          *
60          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
61          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
62          *
63          * @param RequestManager $request               HTTPリクエスト処理クラス
64          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
65          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
66          */
67         function _setTemplate($request, &$param)
68         {
69                 $task = $request->trimValueOf('task');
70                 if ($task == 'list'){           // 一覧画面
71                         return 'admin_list.tmpl.html';
72                 } else {                        // 一覧画面
73                         return 'admin.tmpl.html';
74                 }
75         }
76         /**
77          * テンプレートにデータ埋め込む
78          *
79          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
80          *
81          * @param RequestManager $request               HTTPリクエスト処理クラス
82          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
83          * @param                                                               なし
84          */
85         function _assign($request, &$param)
86         {
87                 $task = $request->trimValueOf('task');
88                 if ($task == 'list'){           // 一覧画面
89                         return $this->createList($request);
90                 } else {                        // 詳細設定画面
91                         return $this->createDetail($request);
92                 }
93         }
94         /**
95          * 詳細画面作成
96          *
97          * @param RequestManager $request               HTTPリクエスト処理クラス
98          * @param                                                               なし
99          */
100         function createDetail($request)
101         {
102                 // ページ定義IDとページ定義のレコードシリアル番号を取得
103                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
104                 
105                 $userId         = $this->gEnv->getCurrentUserId();
106                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
107                 $act = $request->trimValueOf('act');
108                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
109
110                 // 入力値を取得
111                 $name           = $request->trimValueOf('item_name');                   // 設定タイトル
112                 $picasaId       = $request->trimValueOf('item_picasa_id');              // PicasaユーザID
113                 $this->displayType = $request->trimValueOf('item_display_type');                        // Picasa表示タイプ
114                 $imageWidth     = $request->trimValueOf('item_image_width');                    // 画像幅
115                 $imageHeight    = $request->trimValueOf('item_image_height');                   // 画像幅
116                 $imageStyle     = $request->trimValueOf('item_image_style');                    // 画像スタイル
117                 $showTitle      = ($request->trimValueOf('item_show_title') == 'on') ? 1 : 0;                   // アルバムタイトルを表示するかどうか
118                 $albumCount     = $request->trimValueOf('item_album_count');                    // アルバム数
119                 $imageCount     = $request->trimValueOf('item_image_count');                    // 画像数
120                 $colCount       = $request->trimValueOf('item_col_count');                      // カラム数
121                 $this->albumId = $request->trimValueOf('item_select_album');                    // アルバムID
122                 
123                 $this->configId = $request->trimValueOf('item_id');             // 定義ID
124                 if (empty($this->configId)) $this->configId = $defConfigId;             // 呼び出しウィンドウから引き継いだ定義ID
125                 
126                 $replaceNew = false;            // データを再取得するかどうか
127                 if (empty($act)){// 初期起動時
128                         // デフォルト値設定
129                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
130                         $replaceNew = true;                     // データ再取得
131                 } else if ($act == 'add'){// 新規追加
132                         // 入力チェック
133                         $this->checkInput($name, '名前');
134                         $this->checkInput($picasaId, 'PicasaユーザID');
135                         
136                         // 設定名の重複チェック
137                         for ($i = 0; $i < count($this->paramObj); $i++){
138                                 $targetObj = $this->paramObj[$i]->object;
139                                 if ($name == $targetObj->name){         // 定義名
140                                         $this->setUserErrorMsg('名前が重複しています');
141                                         break;
142                                 }
143                         }
144                         
145                         // エラーなしの場合は、データを登録
146                         if ($this->getMsgCount() == 0){
147                                 // 追加オブジェクト作成
148                                 $newObj = new stdClass;
149                                 $newObj->name           = $name;// 表示名
150                                 $newObj->picasaId       = $picasaId;            // PicasaユーザID
151                                 $newObj->displayType = $this->displayType;                      // Picasa表示タイプ
152                                 $newObj->imageWidth             = $imageWidth;                  // 画像幅
153                                 $newObj->imageHeight    = $imageHeight;                 // 画像幅
154                                 $newObj->imageStyle             = $imageStyle;                  // 画像スタイル
155                                 $newObj->showTitle      = $showTitle;                   // アルバムタイトルを表示するかどうか
156                                 $newObj->albumCount     = $albumCount;                  // アルバム数
157                                 $newObj->imageCount     = $imageCount;                  // 画像数
158                                 $newObj->colCount       = $colCount;                    // カラム数
159                                 $newObj->albumId        = $this->albumId;                       // アルバムID
160                 
161                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
162                                 if ($ret){
163                                         $this->setGuidanceMsg('データを追加しました');
164                                         
165                                         $this->configId = $defConfigId;         // 定義定義IDを更新
166                                         $replaceNew = true;                     // データ再取得
167                                 } else {
168                                         $this->setAppErrorMsg('データ追加に失敗しました');
169                                 }
170                         }
171                 } else if ($act == 'update'){           // 設定更新のとき
172                         // 入力値のエラーチェック
173                         $this->checkInput($picasaId, 'PicasaユーザID');
174                         
175                         if ($this->getMsgCount() == 0){                 // エラーのないとき
176                                 // 現在の設定値を取得
177                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
178                                 if ($ret){
179                                         // ウィジェットオブジェクト更新
180                                         $targetObj->picasaId    = $picasaId;            // PicasaユーザID
181                                         $targetObj->displayType = $this->displayType;                   // Picasa表示タイプ
182                                         $targetObj->imageWidth  = $imageWidth;                  // 画像幅
183                                         $targetObj->imageHeight = $imageHeight;                 // 画像幅
184                                         $targetObj->imageStyle  = $imageStyle;                  // 画像スタイル
185                                         $targetObj->showTitle   = $showTitle;                   // アルバムタイトルを表示するかどうか
186                                         $targetObj->albumCount  = $albumCount;                  // アルバム数
187                                         $targetObj->imageCount  = $imageCount;                  // 画像数
188                                         $targetObj->colCount    = $colCount;                    // カラム数
189                                         $targetObj->albumId     = $this->albumId;                       // アルバムID
190                                 }
191                                 
192                                 // 設定値を更新
193                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
194                                 if ($ret){
195                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
196                                         
197                                         $replaceNew = true;                     // データ再取得
198                                 } else {
199                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
200                                 }
201                         }
202                 } else if ($act == 'select'){   // 定義IDを変更
203                         $replaceNew = true;                     // データ再取得
204                 }
205                 
206                 // 表示用データを取得
207                 if (empty($this->configId)){            // 新規登録の場合
208                         $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
209                         if ($replaceNew){               // データ再取得時
210                                 $name = $this->createDefaultName();                     // デフォルト登録項目名
211                                 $picasaId = '';         // PicasaユーザID
212                                 $this->displayType = self::DEFAULT_DISPLAY_TYPE;                        // Picasa表示タイプ
213                                 $imageWidth = self::DEFAULT_IMAGE_WIDTH;                        // 画像幅
214                                 $imageHeight = self::DEFAULT_IMAGE_HEIGHT;                      // 画像幅
215                                 $imageStyle = self::DEFAULT_IMAGE_STYLE;                        // 画像スタイル
216                                 $showTitle = 1;                 // アルバムタイトルを表示するかどうか
217                                 $albumCount = self::DEFAULT_ALBUM_COUNT;                        // アルバム数
218                                 $imageCount = self::DEFAULT_IMAGE_COUNT;                        // 画像数
219                                 $colCount = self::DEFAULT_COL_COUNT;                    // カラム数
220                                 $this->albumId = '';                    // アルバムID
221                         }
222                         $this->serialNo = 0;
223                 } else {
224                         if ($replaceNew){
225                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
226                                 if ($ret){
227                                         $name = $targetObj->name;// 名前
228                                         $picasaId = $targetObj->picasaId;               // PicasaユーザID
229                                         $this->displayType = $targetObj->displayType;                   // Picasa表示タイプ
230                                         $imageWidth = $targetObj->imageWidth;                   // 画像幅
231                                         $imageHeight = $targetObj->imageHeight;                 // 画像幅
232                                         $imageStyle     = $targetObj->imageStyle;                       // 画像スタイル
233                                         $showTitle      = $targetObj->showTitle;                        // アルバムタイトルを表示するかどうか
234                                         $albumCount     = $targetObj->albumCount;                       // アルバム数
235                                         $imageCount     = $targetObj->imageCount;                       // 画像数
236                                         $colCount       = $targetObj->colCount;                 // カラム数
237                                         $this->albumId = $targetObj->albumId;                   // アルバムID
238                                 }
239                         }
240                         $this->serialNo = $this->configId;
241                                 
242                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
243                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
244                 }
245                 
246                 // 設定項目選択メニュー作成
247                 $this->createItemMenu();
248                 
249                 // Picasa表示タイプメニュー作成
250                 $this->createDisplayTypeMenu();
251                 
252                 // アルバム選択メニュー作成
253                 if ($this->displayType == 'select_album' && !empty($picasaId)){
254                         $this->createAlbumMenu($picasaId);
255                 }
256                 
257                 // 画面にデータを埋め込む
258                 $this->tmpl->addVar("item_name_visible", "name", $this->convertToDispString($name));            // 名前
259                 $this->tmpl->addVar("_widget", "picasa_id",     $this->convertToDispString($picasaId)); // PicasaユーザID
260                 $this->tmpl->addVar("_widget", "image_width",   $this->convertToDispString($imageWidth));       // 画像幅
261                 $this->tmpl->addVar("_widget", "image_height",  $this->convertToDispString($imageHeight));      // 画像幅
262                 $this->tmpl->addVar("_widget", "image_style",   $this->convertToDispString($imageStyle));       // 画像スタイル
263                 $this->tmpl->addVar("_widget", "album_count",   $this->convertToDispString($albumCount));                       // アルバム数
264                 $this->tmpl->addVar("_widget", "image_count",   $this->convertToDispString($imageCount));                       // 画像数
265                 $this->tmpl->addVar("_widget", "col_count",             $this->convertToDispString($colCount));                 // カラム数
266                 $checked = '';
267                 if (!empty($showTitle)) $checked = 'checked';
268                 $this->tmpl->addVar("_widget", "checked_show_title", $checked);         // アルバムタイトルを表示するかどうか
269                 
270                 // パスの設定
271                 $this->tmpl->addVar('_widget', 'admin_url', $this->getUrl($this->gEnv->getDefaultAdminUrl()));// 管理者URL
272                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
273                 $this->tmpl->addVar('_widget', 'content_widget_id', self::CONTENT_WIDGET_ID);// コンテンツ表示ウィジェット
274                                 
275                 // ボタンの表示制御
276                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
277                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
278                 } else {
279                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
280                 }
281                 // ページ定義IDとページ定義のレコードシリアル番号を更新
282                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
283         }
284         /**
285          * 選択用メニューを作成
286          *
287          * @return なし                                               
288          */
289         function createItemMenu()
290         {
291                 for ($i = 0; $i < count($this->paramObj); $i++){
292                         $id = $this->paramObj[$i]->id;// 定義ID
293                         $targetObj = $this->paramObj[$i]->object;
294                         $name = $targetObj->name;// 定義名
295                         $selected = '';
296
297                         if ($this->configId == $id) $selected = 'selected';
298                         $row = array(
299                                 'name' => $name,                // 名前
300                                 'value' => $id,         // 定義ID
301                                 'selected' => $selected // 選択中の項目かどうか
302                         );
303                         $this->tmpl->addVars('title_list', $row);
304                         $this->tmpl->parseTemplate('title_list', 'a');
305                 }
306         }
307         /**
308          * デフォルトの名前を取得
309          *
310          * @return string       デフォルト名                                              
311          */
312         function createDefaultName()
313         {
314                 $name = self::DEFAULT_NAME_HEAD;
315                 for ($j = 1; $j < 100; $j++){
316                         $name = self::DEFAULT_NAME_HEAD . $j;
317                         // 設定名の重複チェック
318                         for ($i = 0; $i < count($this->paramObj); $i++){
319                                 $targetObj = $this->paramObj[$i]->object;
320                                 if ($name == $targetObj->name){         // 定義名
321                                         break;
322                                 }
323                         }
324                         // 重複なしのときは終了
325                         if ($i == count($this->paramObj)) break;
326                 }
327                 return $name;
328         }
329         /**
330          * 一覧画面作成
331          *
332          * @param RequestManager $request               HTTPリクエスト処理クラス
333          * @param                                                               なし
334          */
335         function createList($request)
336         {
337                 // ページ定義IDとページ定義のレコードシリアル番号を取得
338                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
339                 
340                 $userId         = $this->gEnv->getCurrentUserId();
341                 $langId = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
342                 $act = $request->trimValueOf('act');
343                 
344                 if ($act == 'delete'){          // メニュー項目の削除
345                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
346                         $delItems = array();
347                         for ($i = 0; $i < count($listedItem); $i++){
348                                 // 項目がチェックされているかを取得
349                                 $itemName = 'item' . $i . '_selected';
350                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
351                                 
352                                 if ($itemValue){                // チェック項目
353                                         $delItems[] = $listedItem[$i];
354                                 }
355                         }
356                         if (count($delItems) > 0){
357                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
358                                 if ($ret){              // データ削除成功のとき
359                                         $this->setGuidanceMsg('データを削除しました');
360                                 } else {
361                                         $this->setAppErrorMsg('データ削除に失敗しました');
362                                 }
363                         }
364                 }
365                 // 定義一覧作成
366                 $this->createItemList();
367                 
368                 // 項目がないときは、一覧を表示しない
369                 if (count($this->serialArray) == 0) $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');              
370                 
371                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
372                 
373                 // ページ定義IDとページ定義のレコードシリアル番号を更新
374                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
375         }
376         /**
377          * 定義一覧作成
378          *
379          * @return なし                                               
380          */
381         function createItemList()
382         {
383                 for ($i = 0; $i < count($this->paramObj); $i++){
384                         $id = $this->paramObj[$i]->id;// 定義ID
385                         $targetObj = $this->paramObj[$i]->object;
386                         $name = $targetObj->name;// 定義名
387                         
388                         $defCount = 0;
389                         if (!empty($id)){
390                                 $defCount = $this->sysDb->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
391                         }
392                         $operationDisagled = '';
393                         if ($defCount > 0) $operationDisagled = 'disabled';
394                         $row = array(
395                                 'index' => $i,
396                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
397                                 'name' => $this->convertToDispString($name),            // 名前
398                                 'def_count' => $defCount                                                        // 使用数
399                         );
400                         $this->tmpl->addVars('itemlist', $row);
401                         $this->tmpl->parseTemplate('itemlist', 'a');
402                         
403                         // シリアル番号を保存
404                         $this->serialArray[] = $id;
405                 }
406         }
407         /**
408          * Picasa表示タイプ選択メニュー作成
409          *
410          * @return なし
411          */
412         function createDisplayTypeMenu()
413         {
414                 for ($i = 0; $i < count($this->displayTypeArray); $i++){
415                         $value = $this->displayTypeArray[$i]['value'];
416                         $name = $this->displayTypeArray[$i]['name'];
417                         
418                         $selected = '';
419                         if ($value == $this->displayType) $selected = 'selected';
420                         
421                         $row = array(
422                                 'value'    => $value,                   // ページID
423                                 'name'     => $name,                    // ページ名
424                                 'selected' => $selected                                                                                                         // 選択中かどうか
425                         );
426                         $this->tmpl->addVars('display_list', $row);
427                         $this->tmpl->parseTemplate('display_list', 'a');
428                 }
429         }
430         /**
431          * アルバム選択メニュー作成
432          *
433          * @param string $picasaId              ユーザID
434          * @return なし
435          */
436         function createAlbumMenu($picasaId)
437         {
438                 // RSSを取得
439                 $commonParam = '&hl=' . self::DEFAULT_GOOGLE_LANG . '&access=public';
440                 $rssUrl = 'https://picasaweb.google.com/data/feed/api/user/' . $picasaId . '?alt=rss&kind=album' . $commonParam . '&max-results=' . self::MAX_RESULT_COUNT;     // RSS 2.0でアルバムを取得
441                 
442                 $xml = simplexml_load_file($rssUrl);
443                 if ($xml !== false){    // 正常終了のとき
444                         $albumCount = count($xml->channel->item);               // アルバム数
445                         
446                         for ($i = 0; $i < $albumCount; $i++){
447                                 $album = $xml->channel->item[$i];
448                                 $albumTitle = $album->title;
449                                 $albumUrl = str_replace('https://', 'http://', $album->link);           // HTTPに統一
450                                 $photoCount = (int)$album->children('http://schemas.google.com/photos/2007')->numphotos;// 画像数
451                                 $albumDir = basename($albumUrl);                // アルバムディレクトリ名
452                                 $albumPath = parse_url($album->guid);
453                                 $albumId = basename($albumPath['path']);                // アルバムID
454                                 
455                                 $selected = '';
456                                 if ($albumId == $this->albumId) $selected = 'selected';
457                         
458                                 $row = array(
459                                         'value'    => $this->convertToDispString($albumId),                     // アルバムID
460                                         'name'     => $this->convertToDispString($albumTitle),                  // アルバムタイトル
461                                         'selected' => $selected                                                                                                         // 選択中かどうか
462                                 );
463                                 $this->tmpl->addVars('album_list', $row);
464                                 $this->tmpl->parseTemplate('album_list', 'a');
465                         }
466                 }
467         }
468 }
469 ?>