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-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
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
18 class admin_picasaWidgetContainer extends BaseAdminWidgetContainer
20 private $sysDb; // DB接続オブジェクト
21 private $serialNo; // 選択中の項目のシリアル番号
22 private $serialArray = array(); // 表示中のシリアル番号
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
43 function __construct()
46 parent::__construct();
49 $this->sysDb = $this->gInstance->getSytemDbObject();
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'));
60 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
61 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
63 * @param RequestManager $request HTTPリクエスト処理クラス
64 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
65 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
67 function _setTemplate($request, &$param)
69 $task = $request->trimValueOf('task');
70 if ($task == 'list'){ // 一覧画面
71 return 'admin_list.tmpl.html';
73 return 'admin.tmpl.html';
79 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
81 * @param RequestManager $request HTTPリクエスト処理クラス
82 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
85 function _assign($request, &$param)
87 $task = $request->trimValueOf('task');
88 if ($task == 'list'){ // 一覧画面
89 return $this->createList($request);
91 return $this->createDetail($request);
97 * @param RequestManager $request HTTPリクエスト処理クラス
100 function createDetail($request)
102 // ページ定義IDとページ定義のレコードシリアル番号を取得
103 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
105 $userId = $this->gEnv->getCurrentUserId();
106 $this->langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
107 $act = $request->trimValueOf('act');
108 $this->serialNo = $request->trimValueOf('serial'); // 選択項目のシリアル番号
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
123 $this->configId = $request->trimValueOf('item_id'); // 定義ID
124 if (empty($this->configId)) $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
126 $replaceNew = false; // データを再取得するかどうか
127 if (empty($act)){// 初期起動時
129 $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
130 $replaceNew = true; // データ再取得
131 } else if ($act == 'add'){// 新規追加
133 $this->checkInput($name, '名前');
134 $this->checkInput($picasaId, 'PicasaユーザID');
137 for ($i = 0; $i < count($this->paramObj); $i++){
138 $targetObj = $this->paramObj[$i]->object;
139 if ($name == $targetObj->name){ // 定義名
140 $this->setUserErrorMsg('名前が重複しています');
146 if ($this->getMsgCount() == 0){
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
161 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
163 $this->setGuidanceMsg('データを追加しました');
165 $this->configId = $defConfigId; // 定義定義IDを更新
166 $replaceNew = true; // データ再取得
168 $this->setAppErrorMsg('データ追加に失敗しました');
171 } else if ($act == 'update'){ // 設定更新のとき
173 $this->checkInput($picasaId, 'PicasaユーザID');
175 if ($this->getMsgCount() == 0){ // エラーのないとき
177 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
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
193 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
195 $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
197 $replaceNew = true; // データ再取得
199 $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
202 } else if ($act == 'select'){ // 定義IDを変更
203 $replaceNew = true; // データ再取得
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
225 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
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
240 $this->serialNo = $this->configId;
242 // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
243 if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
247 $this->createItemMenu();
250 $this->createDisplayTypeMenu();
253 if ($this->displayType == 'select_album' && !empty($picasaId)){
254 $this->createAlbumMenu($picasaId);
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)); // カラム数
267 if (!empty($showTitle)) $checked = 'checked';
268 $this->tmpl->addVar("_widget", "checked_show_title", $checked); // アルバムタイトルを表示するかどうか
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);// コンテンツ表示ウィジェット
276 if (empty($this->serialNo)){ // 新規追加項目を選択しているとき
277 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
279 $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
281 // ページ定義IDとページ定義のレコードシリアル番号を更新
282 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
289 function createItemMenu()
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;// 定義名
297 if ($this->configId == $id) $selected = 'selected';
299 'name' => $name, // 名前
300 'value' => $id, // 定義ID
301 'selected' => $selected // 選択中の項目かどうか
303 $this->tmpl->addVars('title_list', $row);
304 $this->tmpl->parseTemplate('title_list', 'a');
310 * @return string デフォルト名
312 function createDefaultName()
314 $name = self::DEFAULT_NAME_HEAD;
315 for ($j = 1; $j < 100; $j++){
316 $name = self::DEFAULT_NAME_HEAD . $j;
318 for ($i = 0; $i < count($this->paramObj); $i++){
319 $targetObj = $this->paramObj[$i]->object;
320 if ($name == $targetObj->name){ // 定義名
325 if ($i == count($this->paramObj)) break;
332 * @param RequestManager $request HTTPリクエスト処理クラス
335 function createList($request)
337 // ページ定義IDとページ定義のレコードシリアル番号を取得
338 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
340 $userId = $this->gEnv->getCurrentUserId();
341 $langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
342 $act = $request->trimValueOf('act');
344 if ($act == 'delete'){ // メニュー項目の削除
345 $listedItem = explode(',', $request->trimValueOf('seriallist'));
347 for ($i = 0; $i < count($listedItem); $i++){
349 $itemName = 'item' . $i . '_selected';
350 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
352 if ($itemValue){ // チェック項目
353 $delItems[] = $listedItem[$i];
356 if (count($delItems) > 0){
357 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
358 if ($ret){ // データ削除成功のとき
359 $this->setGuidanceMsg('データを削除しました');
361 $this->setAppErrorMsg('データ削除に失敗しました');
366 $this->createItemList();
369 if (count($this->serialArray) == 0) $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');
371 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
373 // ページ定義IDとページ定義のレコードシリアル番号を更新
374 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
381 function createItemList()
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;// 定義名
390 $defCount = $this->sysDb->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
392 $operationDisagled = '';
393 if ($defCount > 0) $operationDisagled = 'disabled';
396 'ope_disabled' => $operationDisagled, // 選択可能かどうか
397 'name' => $this->convertToDispString($name), // 名前
398 'def_count' => $defCount // 使用数
400 $this->tmpl->addVars('itemlist', $row);
401 $this->tmpl->parseTemplate('itemlist', 'a');
404 $this->serialArray[] = $id;
408 * Picasa表示タイプ選択メニュー作成
412 function createDisplayTypeMenu()
414 for ($i = 0; $i < count($this->displayTypeArray); $i++){
415 $value = $this->displayTypeArray[$i]['value'];
416 $name = $this->displayTypeArray[$i]['name'];
419 if ($value == $this->displayType) $selected = 'selected';
422 'value' => $value, // ページID
423 'name' => $name, // ページ名
424 'selected' => $selected // 選択中かどうか
426 $this->tmpl->addVars('display_list', $row);
427 $this->tmpl->parseTemplate('display_list', 'a');
433 * @param string $picasaId ユーザID
436 function createAlbumMenu($picasaId)
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でアルバムを取得
442 $xml = simplexml_load_file($rssUrl);
443 if ($xml !== false){ // 正常終了のとき
444 $albumCount = count($xml->channel->item); // アルバム数
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
456 if ($albumId == $this->albumId) $selected = 'selected';
459 'value' => $this->convertToDispString($albumId), // アルバムID
460 'name' => $this->convertToDispString($albumTitle), // アルバムタイトル
461 'selected' => $selected // 選択中かどうか
463 $this->tmpl->addVars('album_list', $row);
464 $this->tmpl->parseTemplate('album_list', 'a');