Helper
[ class tree: Helper ] [ index: Helper ] [ all elements ]

Source for file SC_Helper_PageLayout.php

Documentation is available at SC_Helper_PageLayout.php

  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) 2000-2011 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.  */
  31.  
  32.     // }}}
  33.     // {{{ functions
  34.  
  35.     /**
  36.      * ページのレイアウト情報を取得し, 設定する.
  37.      *
  38.      * 現在の URL に応じたページのレイアウト情報を取得し, LC_Page インスタンスに
  39.      * 設定する.
  40.      *
  41.      * @access public
  42.      * @param LC_Page $objPage LC_Page インスタンス
  43.      * @param boolean $preview プレビュー表示の場合 true
  44.      * @param string $url ページのURL($_SERVER['PHP_SELF'] の情報)
  45.      * @param integer $device_type_id 端末種別ID
  46.      * @return void 
  47.      */
  48.     function sfGetPageLayout(&$objPage$preview false$url ""$device_type_id DEVICE_TYPE_PC{
  49.  
  50.         // URLを元にページ情報を取得
  51.         if ($preview === false{
  52.             $url preg_replace('|^' preg_quote(ROOT_URLPATH'|'''$url);
  53.             $arrPageData $this->getPageProperties($device_type_idnull'url = ?'array($url));
  54.         }
  55.         // プレビューの場合は, プレビュー用のデータを取得
  56.         else {
  57.             $arrPageData $this->getPageProperties($device_type_id0);
  58.         }
  59.  
  60.         $objPage->tpl_mainpage $this->getTemplatePath($device_type_id$arrPageData[0]['filename'".tpl";
  61.         $objPage->arrPageLayout =$arrPageData[0];
  62.  
  63.         // ページタイトルを設定
  64.         if (SC_Utils_Ex::isBlank($objPage->tpl_title)) {
  65.             $objPage->tpl_title $objPage->arrPageLayout['page_name'];
  66.         }
  67.  
  68.         // 該当ページのブロックを取得し, 配置する
  69.         $masterData new SC_DB_MasterData();
  70.         $arrTarget $masterData->getMasterData("mtb_target");
  71.         $arrBlocs $this->getBlocPositions($device_type_id$objPage->arrPageLayout['page_id']);
  72.         // php_path, tpl_path が存在するものを, 各ターゲットに配置
  73.         foreach (array_keys($arrTargetas $target_id{
  74.             foreach ($arrBlocs as $arrBloc{
  75.                 if ($arrBloc['target_id'!= $target_id{
  76.                     continue;
  77.                 }
  78.                 if (is_file($arrBloc['php_path'])
  79.                     || is_file($arrBloc['tpl_path'])) {
  80.                     $objPage->arrPageLayout[$arrTarget[$target_id]][$arrBloc;
  81.                 else {
  82.                     $error "ブロックが見つかりません\n"
  83.                         . "tpl_path: " $arrBloc['tpl_path'"\n"
  84.                         . "php_path: " $arrBloc['php_path'];
  85.                     GC_Utils_Ex::gfPrintLog($error);
  86.                 }
  87.             }
  88.         }
  89.         // カラム数を取得する
  90.         $objPage->tpl_column_num $this->getColumnNum($objPage->arrPageLayout);
  91.     }
  92.  
  93.     /**
  94.      * ページの属性を取得する.
  95.      *
  96.      * この関数は, dtb_pagelayout の情報を検索する.
  97.      * $device_type_id は必須. デフォルト値は DEVICE_TYPE_PC.
  98.      * $page_id が null の場合は, $page_id が 0 以外のものを検索する.
  99.      *
  100.      * @access public
  101.      * @param integer $device_type_id 端末種別ID
  102.      * @param integer $page_id ページID; null の場合は, 0 以外を検索する.
  103.      * @param string $where 追加の検索条件
  104.      * @param array $arrParams 追加の検索パラメーター
  105.      * @return array ページ属性の配列
  106.      */
  107.     function getPageProperties($device_type_id DEVICE_TYPE_PC$page_id null$where ''$arrParams array()) {
  108.         $objQuery =SC_Query_Ex::getSingletonInstance();
  109.         $where 'device_type_id = ? ' (SC_Utils_Ex::isBlank($where$where 'AND ' $where);
  110.         if ($page_id === null{
  111.             $where 'page_id <> ? AND ' $where;
  112.             $page_id 0;
  113.         else {
  114.             $where 'page_id = ? AND ' $where;
  115.         }
  116.         $objQuery->setOrder('page_id');
  117.         $arrParams array_merge(array($page_id$device_type_id)$arrParams);
  118.         return $objQuery->select('*''dtb_pagelayout'$where$arrParams);
  119.     }
  120.  
  121.     /**
  122.      * ブロック情報を取得する.
  123.      *
  124.      * @access public
  125.      * @param integer $device_type_id 端末種別ID
  126.      * @param string $where 追加の検索条件
  127.      * @param array $arrParams 追加の検索パラメーター
  128.      * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true
  129.      * @return array ブロック情報の配列
  130.      */
  131.     function getBlocs($device_type_id DEVICE_TYPE_PC$where ''$arrParams array()$has_realpath true{
  132.         $objQuery =SC_Query_Ex::getSingletonInstance();
  133.         $where 'device_type_id = ? ' (SC_Utils_Ex::isBlank($where$where 'AND ' $where);
  134.         $arrParams array_merge(array($device_type_id)$arrParams);
  135.         $objQuery->setOrder('bloc_id');
  136.         $arrBlocs $objQuery->select('*''dtb_bloc'$where$arrParams);
  137.         if ($has_realpath{
  138.             $this->setBlocPathTo($device_type_id$arrBlocs);
  139.         }
  140.         return $arrBlocs;
  141.     }
  142.  
  143.     /**
  144.      * ブロック配置情報を取得する.
  145.      *
  146.      * @access public
  147.      * @param integer $device_type_id 端末種別ID
  148.      * @param integer $page_id ページID
  149.      * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true
  150.      * @return array 配置情報を含めたブロックの配列
  151.      */
  152.     function getBlocPositions($device_type_id$page_id$has_realpath true{
  153.         $objQuery =SC_Query_Ex::getSingletonInstance();
  154.         $table = <<< __EOF__
  155.             dtb_blocposition AS pos
  156.        JOIN dtb_bloc AS bloc
  157.          ON bloc.bloc_id = pos.bloc_id
  158.         AND bloc.device_type_id = pos.device_type_id
  159. __EOF__;
  160.         $where "bloc.device_type_id = ? AND (anywhere = 1 OR pos.page_id = ?)";
  161.         $objQuery->setOrder('target_id, bloc_row');
  162.         $arrBlocs $objQuery->select("*"$table$where,
  163.                                       array($device_type_id$page_id));
  164.         if ($has_realpath{
  165.             $this->setBlocPathTo($device_type_id$arrBlocs);
  166.         }
  167.         return $arrBlocs;
  168.     }
  169.  
  170.     /**
  171.      * ページ情報を削除する.
  172.      *
  173.      * XXX ファイルを確実に削除したかどうかのチェック
  174.      *
  175.      * @access public
  176.      * @param integer $page_id ページID
  177.      * @param integer $device_type_id 端末種別ID
  178.      * @return integer 削除数
  179.      */
  180.     function lfDelPageData($page_id$device_type_id DEVICE_TYPE_PC{
  181.         $objQuery =SC_Query_Ex::getSingletonInstance();
  182.         $arrDelData array();      // 抽出データ用
  183.  
  184.         // page_id が空でない場合にはdeleteを実行
  185.         if ($page_id != ''{
  186.             $arrPageData $this->getPageProperties($device_type_id$page_id);
  187.             $ret $objQuery->delete("dtb_pagelayout""page_id = ? AND device_type_id = ?"array($page_id$device_type_id));
  188.             // ファイルの削除
  189.             $this->lfDelFile($arrPageData[0]['filename']$device_type_id);
  190.         }
  191.         return $ret;
  192.     }
  193.  
  194.     /**
  195.      * ページのファイルを削除する.
  196.      *
  197.      * dtb_pagelayout の削除後に呼び出すこと。
  198.      *
  199.      * @access private
  200.      * @param string $filename 
  201.      * @param integer $device_type_id 端末種別ID
  202.      * @return void // TODO boolean にするべき?
  203.      */
  204.     function lfDelFile($filename$device_type_id{
  205.         $objQuery =SC_Query_Ex::getSingletonInstance();
  206.  
  207.         /*
  208.          * 同名ファイルの使用件数
  209.          * PHP ファイルは, 複数のデバイスで共有するため, device_type_id を条件に入れない
  210.          */
  211.         $count $objQuery->count('dtb_pagelayout''filename = ?'array($filename));
  212.  
  213.         if ($count == 0{
  214.             // phpファイルの削除
  215.             $del_php HTML_REALDIR $filename '.php';
  216.             if (file_exists($del_php)) {
  217.                 unlink($del_php);
  218.             }
  219.         }
  220.  
  221.         // tplファイルの削除
  222.         $del_tpl $this->getTemplatePath($device_type_id$filename '.tpl';
  223.         if (file_exists($del_tpl)) {
  224.             unlink($del_tpl);
  225.         }
  226.     }
  227.  
  228.     /**
  229.      * 編集可能ページかどうか.
  230.      *
  231.      * @access public
  232.      * @param integer $device_type_id 端末種別ID
  233.      * @param integer $page_id ページID
  234.      * @return 編集可能ページの場合 true
  235.      */
  236.     function isEditablePage($device_type_id$page_id{
  237.         if ($page_id == 0{
  238.             return false;
  239.         }
  240.         $arrPages $this->getPageProperties($device_type_id$page_id);
  241.         if ($arrPages[0]['edit_flg'!= 2{
  242.             return true;
  243.         }
  244.         return false;
  245.     }
  246.  
  247.     /**
  248.      * テンプレートのパスを取得する.
  249.      *
  250.      * @access public
  251.      * @param integer $device_type_id 端末種別ID
  252.      * @param boolean $isUser USER_REALDIR 以下のパスを返す場合 true
  253.      * @return string テンプレートのパス
  254.      */
  255.     function getTemplatePath($device_type_id DEVICE_TYPE_PC$isUser false{
  256.         $templateName "";
  257.         switch ($device_type_id{
  258.         case DEVICE_TYPE_MOBILE:
  259.             $dir MOBILE_TEMPLATE_REALDIR;
  260.             $templateName MOBILE_TEMPLATE_NAME;
  261.             break;
  262.  
  263.         case DEVICE_TYPE_SMARTPHONE:
  264.             $dir SMARTPHONE_TEMPLATE_REALDIR;
  265.             $templateName SMARTPHONE_TEMPLATE_NAME;
  266.             break;
  267.  
  268.         case DEVICE_TYPE_PC:
  269.         default:
  270.             $dir TEMPLATE_REALDIR;
  271.             $templateName TEMPLATE_NAME;
  272.         }
  273.         $userPath USER_REALDIR;
  274.         if ($isUser{
  275.             $dir $userPath USER_PACKAGE_DIR $templateName "/";
  276.         }
  277.         return $dir;
  278.     }
  279.  
  280.     /**
  281.      * DocumentRoot から user_data のパスを取得する.
  282.      *
  283.      * 引数 $hasPackage を true にした場合は, user_data/packages/template_name
  284.      * を取得する.
  285.      *
  286.      * @access public
  287.      * @param integer $device_type_id 端末種別ID
  288.      * @param boolean $hasPackage パッケージのパスも含める場合 true
  289.      * @return string 端末に応じた DocumentRoot から user_data までのパス
  290.      */
  291.     function getUserDir($device_type_id DEVICE_TYPE_PC$hasPackage false{
  292.         switch ($device_type_id{
  293.         case DEVICE_TYPE_MOBILE:
  294.             $templateName MOBILE_TEMPLATE_NAME;
  295.             break;
  296.  
  297.         case DEVICE_TYPE_SMARTPHONE:
  298.             $templateName SMARTPHONE_TEMPLATE_NAME;
  299.             break;
  300.  
  301.         case DEVICE_TYPE_PC:
  302.         default:
  303.             $templateName TEMPLATE_NAME;
  304.         }
  305.         $userDir ROOT_URLPATH USER_DIR;
  306.         if ($hasPackage{
  307.             return $userDir USER_PACKAGE_DIR $templateName "/";
  308.         }
  309.         return $userDir;
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ private functions
  314.  
  315.     /**
  316.      * ブロックの php_path, tpl_path を設定する.
  317.      *
  318.      * @access private
  319.      * @param integer $device_type_id 端末種別ID
  320.      * @param array $arrBlocs 設定するブロックの配列
  321.      * @return void 
  322.      */
  323.     function setBlocPathTo($device_type_id DEVICE_TYPE_PC&$arrBlocs{
  324.         foreach (array_keys($arrBlocsas $key{
  325.             $arrBloc =$arrBlocs[$key];
  326.             $arrBloc['php_path'SC_Utils_Ex::isBlank($arrBloc['php_path']'' HTML_REALDIR $arrBloc['php_path'];
  327.             $bloc_dir $this->getTemplatePath($device_type_idBLOC_DIR;
  328.             $arrBloc['tpl_path'SC_Utils_Ex::isBlank($arrBloc['tpl_path']'' $bloc_dir $arrBloc['tpl_path'];
  329.         }
  330.     }
  331.  
  332.     /**
  333.      * カラム数を取得する.
  334.      *
  335.      * @access private
  336.      * @param array $arrPageLayout レイアウト情報の配列
  337.      * @return integer $col_num カラム数
  338.      */
  339.     function getColumnNum($arrPageLayout{
  340.         // メインは確定
  341.         $col_num 1;
  342.         // LEFT NAVI
  343.         if (count($arrPageLayout['LeftNavi']0$col_num++;
  344.         // RIGHT NAVI
  345.         if (count($arrPageLayout['RightNavi']0$col_num++;
  346.  
  347.         return $col_num;
  348.     }
  349. }
  350. ?>

Documentation generated on Fri, 24 Feb 2012 14:02:46 +0900 by Seasoft