source: branches/version-2_13-dev/data/class/helper/SC_Helper_PageLayout.php @ 22786

Revision 22786, 13.5 KB checked in by Seasoft, 11 years ago (diff)

#2201 (SEO周りの整備)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/**
25 * Webページのレイアウト情報を制御するヘルパークラス.
26 *
27 * @package Helper
28 * @author LOCKON CO.,LTD.
29 * @version $Id:SC_Helper_PageLayout.php 15532 2007-08-31 14:39:46Z nanasess $
30 */
31class SC_Helper_PageLayout
32{
33
34    // }}}
35    // {{{ functions
36
37    /**
38     * ページのレイアウト情報を取得し, 設定する.
39     *
40     * 現在の URL に応じたページのレイアウト情報を取得し, LC_Page インスタンスに
41     * 設定する.
42     *
43     * @access public
44     * @param LC_Page $objPage LC_Page インスタンス
45     * @param boolean $preview プレビュー表示の場合 true
46     * @param string $url ページのURL($_SERVER['SCRIPT_NAME'] の情報)
47     * @param integer $device_type_id 端末種別ID
48     * @return void
49     */
50    function sfGetPageLayout(&$objPage, $preview = false, $url = '', $device_type_id = DEVICE_TYPE_PC)
51    {
52
53        // URLを元にページ情報を取得
54        if ($preview === false) {
55            $url = preg_replace('|^' . preg_quote(ROOT_URLPATH) . '|', '', $url);
56            $arrPageData = $this->getPageProperties($device_type_id, null, 'url = ?', array($url));
57        }
58        // プレビューの場合は, プレビュー用のデータを取得
59        else {
60            $arrPageData = $this->getPageProperties($device_type_id, 0);
61        }
62
63        $objPage->tpl_mainpage = $this->getTemplatePath($device_type_id) . $arrPageData[0]['filename'] . '.tpl';
64        $objPage->arrPageLayout =& $arrPageData[0];
65        if (strlen($objPage->arrPageLayout['author']) === 0) {
66            $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
67            $objPage->arrPageLayout['author'] = $arrInfo['company_name'];
68        }
69
70        // ページタイトルを設定
71        if (SC_Utils_Ex::isBlank($objPage->tpl_title)) {
72            $objPage->tpl_title = $objPage->arrPageLayout['page_name'];
73        }
74
75        // 該当ページのブロックを取得し, 配置する
76        $masterData = new SC_DB_MasterData_Ex();
77        $arrTarget = $masterData->getMasterData('mtb_target');
78        $arrBlocs = $this->getBlocPositions($device_type_id, $objPage->arrPageLayout['page_id']);
79        // 無効なプラグインのブロックを取り除く.
80        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance();
81        $arrBlocs = $objPlugin->getEnableBlocs($arrBlocs);
82        // php_path, tpl_path が存在するものを, 各ターゲットに配置
83        foreach ($arrTarget as $target_id => $value) {
84            foreach ($arrBlocs as $arrBloc) {
85                if ($arrBloc['target_id'] != $target_id) {
86                    continue;
87                }
88                if (is_file($arrBloc['php_path'])
89                    || is_file($arrBloc['tpl_path'])) {
90                    $objPage->arrPageLayout[$arrTarget[$target_id]][] = $arrBloc;
91                } else {
92                    $error = "ブロックが見つかりません\n"
93                        . 'tpl_path: ' . $arrBloc['tpl_path'] . "\n"
94                        . 'php_path: ' . $arrBloc['php_path'];
95                    trigger_error($error, E_USER_WARNING);
96                }
97            }
98        }
99        // カラム数を取得する
100        $objPage->tpl_column_num = $this->getColumnNum($objPage->arrPageLayout);
101    }
102
103    /**
104     * ページの属性を取得する.
105     *
106     * この関数は, dtb_pagelayout の情報を検索する.
107     * $device_type_id は必須. デフォルト値は DEVICE_TYPE_PC.
108     * $page_id が null の場合は, $page_id が 0 以外のものを検索する.
109     *
110     * @access public
111     * @param integer $device_type_id 端末種別ID
112     * @param integer $page_id ページID; null の場合は, 0 以外を検索する.
113     * @param string $where 追加の検索条件
114     * @param array $arrParams 追加の検索パラメーター
115     * @return array ページ属性の配列
116     */
117    function getPageProperties($device_type_id = DEVICE_TYPE_PC, $page_id = null, $where = '', $arrParams = array())
118    {
119        $objQuery =& SC_Query_Ex::getSingletonInstance();
120        $where = 'device_type_id = ? ' . (SC_Utils_Ex::isBlank($where) ? $where : 'AND ' . $where);
121        if ($page_id === null) {
122            $where = 'page_id <> ? AND ' . $where;
123            $page_id = 0;
124        } else {
125            $where = 'page_id = ? AND ' . $where;
126        }
127        $objQuery->setOrder('page_id');
128        $arrParams = array_merge(array($page_id, $device_type_id), $arrParams);
129        return $objQuery->select('*', 'dtb_pagelayout', $where, $arrParams);
130    }
131
132    /**
133     * ブロック情報を取得する.
134     *
135     * @access public
136     * @param integer $device_type_id 端末種別ID
137     * @param string $where 追加の検索条件
138     * @param array $arrParams 追加の検索パラメーター
139     * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true
140     * @return array ブロック情報の配列
141     */
142    function getBlocs($device_type_id = DEVICE_TYPE_PC, $where = '', $arrParams = array(), $has_realpath = true)
143    {
144        $objBloc = new SC_Helper_Bloc_Ex($device_type_id);
145        $arrBlocs = $objBloc->getWhere($where, $arrParams);
146        if ($has_realpath) {
147            $this->setBlocPathTo($device_type_id, $arrBlocs);
148        }
149        return $arrBlocs;
150    }
151
152    /**
153     * ブロック配置情報を取得する.
154     *
155     * @access public
156     * @param integer $device_type_id 端末種別ID
157     * @param integer $page_id ページID
158     * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true
159     * @return array 配置情報を含めたブロックの配列
160     */
161    function getBlocPositions($device_type_id, $page_id, $has_realpath = true)
162    {
163        $objQuery =& SC_Query_Ex::getSingletonInstance();
164        $table = <<< __EOF__
165        dtb_blocposition AS pos
166            JOIN dtb_bloc AS bloc
167                ON bloc.bloc_id = pos.bloc_id
168                    AND bloc.device_type_id = pos.device_type_id
169__EOF__;
170        $where = 'bloc.device_type_id = ? AND ((anywhere = 1 AND pos.page_id != 0) OR pos.page_id = ?)';
171        $objQuery->setOrder('target_id, bloc_row');
172        $arrBlocs = $objQuery->select('*', $table, $where, array($device_type_id, $page_id));
173        if ($has_realpath) {
174            $this->setBlocPathTo($device_type_id, $arrBlocs);
175        }
176       
177        //全ページ設定と各ページのブロックの重複を削除
178        foreach ($arrBlocs as $index => $arrBloc) {
179            if($arrBloc['anywhere'] == 1){
180                $arrUniqBlocIds[] = $arrBloc['bloc_id'];
181            }
182        }
183        foreach ($arrBlocs as $bloc_index => $arrBlocData) {
184            if(in_array($arrBlocData['bloc_id'], $arrUniqBlocIds) && $arrBlocData['anywhere'] == 0){
185                unset($arrBlocs[$bloc_index]);
186            }
187        }
188        return $arrBlocs;
189    }
190
191    /**
192     * ページ情報を削除する.
193     *
194     * XXX ファイルを確実に削除したかどうかのチェック
195     *
196     * @access public
197     * @param integer $page_id ページID
198     * @param integer $device_type_id 端末種別ID
199     * @return integer 削除数
200     */
201    function lfDelPageData($page_id, $device_type_id = DEVICE_TYPE_PC)
202    {
203        $objQuery =& SC_Query_Ex::getSingletonInstance();
204        // page_id が空でない場合にはdeleteを実行
205        if ($page_id != '') {
206            $arrPageData = $this->getPageProperties($device_type_id, $page_id);
207            $ret = $objQuery->delete('dtb_pagelayout', 'page_id = ? AND device_type_id = ?', array($page_id, $device_type_id));
208            // ファイルの削除
209            $this->lfDelFile($arrPageData[0]['filename'], $device_type_id);
210        }
211        return $ret;
212    }
213
214    /**
215     * ページのファイルを削除する.
216     *
217     * dtb_pagelayout の削除後に呼び出すこと。
218     *
219     * @access private
220     * @param string $filename
221     * @param integer $device_type_id 端末種別ID
222     * @return void // TODO boolean にするべき?
223     */
224    function lfDelFile($filename, $device_type_id)
225    {
226        $objQuery =& SC_Query_Ex::getSingletonInstance();
227
228        /*
229         * 同名ファイルの使用件数
230         * PHP ファイルは, 複数のデバイスで共有するため, device_type_id を条件に入れない
231         */
232        $exists = $objQuery->exists('dtb_pagelayout', 'filename = ?', array($filename));
233
234        if (!$exists) {
235            // phpファイルの削除
236            $del_php = HTML_REALDIR . $filename . '.php';
237            if (file_exists($del_php)) {
238                unlink($del_php);
239            }
240        }
241
242        // tplファイルの削除
243        $del_tpl = $this->getTemplatePath($device_type_id) . $filename . '.tpl';
244        if (file_exists($del_tpl)) {
245            unlink($del_tpl);
246        }
247    }
248
249    /**
250     * 編集可能ページかどうか.
251     *
252     * @access public
253     * @param integer $device_type_id 端末種別ID
254     * @param integer $page_id ページID
255     * @return 編集可能ページの場合 true
256     */
257    function isEditablePage($device_type_id, $page_id)
258    {
259        if ($page_id == 0) {
260            return false;
261        }
262        $arrPages = $this->getPageProperties($device_type_id, $page_id);
263        if ($arrPages[0]['edit_flg'] != 2) {
264            return true;
265        }
266        return false;
267    }
268
269    /**
270     * テンプレートのパスを取得する.
271     *
272     * @access public
273     * @param integer $device_type_id 端末種別ID
274     * @param boolean $isUser USER_REALDIR 以下のパスを返す場合 true
275     * @return string テンプレートのパス
276     */
277    function getTemplatePath($device_type_id = DEVICE_TYPE_PC, $isUser = false)
278    {
279        $templateName = '';
280        switch ($device_type_id) {
281            case DEVICE_TYPE_MOBILE:
282                $dir = MOBILE_TEMPLATE_REALDIR;
283                $templateName = MOBILE_TEMPLATE_NAME;
284                break;
285
286            case DEVICE_TYPE_SMARTPHONE:
287                $dir = SMARTPHONE_TEMPLATE_REALDIR;
288                $templateName = SMARTPHONE_TEMPLATE_NAME;
289                break;
290
291            case DEVICE_TYPE_PC:
292            default:
293                $dir = TEMPLATE_REALDIR;
294                $templateName = TEMPLATE_NAME;
295                break;
296        }
297        $userPath = USER_REALDIR;
298        if ($isUser) {
299            $dir = $userPath . USER_PACKAGE_DIR . $templateName . '/';
300        }
301        return $dir;
302    }
303
304    /**
305     * DocumentRoot から user_data のパスを取得する.
306     *
307     * 引数 $hasPackage を true にした場合は, user_data/packages/template_name
308     * を取得する.
309     *
310     * @access public
311     * @param integer $device_type_id 端末種別ID
312     * @param boolean $hasPackage パッケージのパスも含める場合 true
313     * @return string 端末に応じた DocumentRoot から user_data までのパス
314     */
315    function getUserDir($device_type_id = DEVICE_TYPE_PC, $hasPackage = false)
316    {
317        switch ($device_type_id) {
318        case DEVICE_TYPE_MOBILE:
319            $templateName = MOBILE_TEMPLATE_NAME;
320            break;
321
322        case DEVICE_TYPE_SMARTPHONE:
323            $templateName = SMARTPHONE_TEMPLATE_NAME;
324            break;
325
326        case DEVICE_TYPE_PC:
327        default:
328            $templateName = TEMPLATE_NAME;
329        }
330        $userDir = ROOT_URLPATH . USER_DIR;
331        if ($hasPackage) {
332            return $userDir . USER_PACKAGE_DIR . $templateName . '/';
333        }
334        return $userDir;
335    }
336
337    // }}}
338    // {{{ private functions
339
340    /**
341     * ブロックの php_path, tpl_path を設定する.
342     *
343     * @access private
344     * @param integer $device_type_id 端末種別ID
345     * @param array $arrBlocs 設定するブロックの配列
346     * @return void
347     */
348    function setBlocPathTo($device_type_id = DEVICE_TYPE_PC, &$arrBlocs = array())
349    {
350        foreach ($arrBlocs as $key => $value) {
351            $arrBloc =& $arrBlocs[$key];
352            $arrBloc['php_path'] = SC_Utils_Ex::isBlank($arrBloc['php_path']) ? '' : HTML_REALDIR . $arrBloc['php_path'];
353            $bloc_dir = $this->getTemplatePath($device_type_id) . BLOC_DIR;
354            $arrBloc['tpl_path'] = SC_Utils_Ex::isBlank($arrBloc['tpl_path']) ? '' : $bloc_dir . $arrBloc['tpl_path'];
355        }
356    }
357
358    /**
359     * カラム数を取得する.
360     *
361     * @access private
362     * @param array $arrPageLayout レイアウト情報の配列
363     * @return integer $col_num カラム数
364     */
365    function getColumnNum($arrPageLayout)
366    {
367        // メインは確定
368        $col_num = 1;
369        // LEFT NAVI
370        if (count($arrPageLayout['LeftNavi']) > 0) $col_num++;
371        // RIGHT NAVI
372        if (count($arrPageLayout['RightNavi']) > 0) $col_num++;
373
374        return $col_num;
375    }
376}
Note: See TracBrowser for help on using the repository browser.