1 <?php
2 3 4 5 6 7 8
9
10
11 Yii::setPathOfAlias('P3Page', dirname(__FILE__));
12 Yii::import('P3Page.*');
13
14 class P3Page extends BaseP3Page {
15
16 const PAGE_ID_KEY = 'pageId';
17 const PAGE_NAME_KEY = 'pageName';
18
19 public function get_label() {
20 return $this->t('menuName', null, true). " #". $this->id ;
21 }
22
23
24 public static function model($className = __CLASS__) {
25 return parent::model($className);
26 }
27
28 public function init() {
29 return parent::init();
30 }
31
32 public function __toString() {
33 return (string) $this->layout;
34 }
35
36 public function behaviors() {
37 return array_merge(
38 array(
39
40
41
42 'MetaData' => array(
43 'class' => 'P3MetaDataBehavior',
44 'metaDataRelation' => 'p3PageMeta',
45 'parentRelation' => 'treeParent',
46 'childrenRelation' => 'p3PageMetas',
47 'contentRelation' => 'id0',
48 ),
49 'Translation' => array(
50 'class' => 'P3TranslationBehavior',
51 'relation' => 'p3PageTranslations',
52 'fallbackLanguage' => (isset(Yii::app()->params['p3.fallbackLanguage'])) ? Yii::app()->params['p3.fallbackLanguage'] : 'en',
53 'fallbackIndicator' => array('menuName'=>' *'),
54 'fallbackValue' => 'Page*',
55
56
57 )
58 ), parent::behaviors()
59 );
60 }
61
62 public function rules() {
63 return array_merge(
64 array(
65 array('route', 'match', 'pattern' => '/"route":"|{}/', 'message' => 'If not empty, route JSON must contain a \'route\' element'),
66 ), parent::rules()
67 );
68 }
69
70 public function createUrl($additionalParams = array(), $absolute = false) {
71
72 if ($this->id == 1) {
73 return Yii::app()->homeUrl;
74 } elseif (is_array(CJSON::decode($this->route)) && count(CJSON::decode($this->route)) !== 0) {
75 $link = CJSON::decode($this->route);
76 } elseif ($this->route && $this->route !== "{}") {
77 return $this->route;
78 } else {
79 $link['route'] = '/p3pages/default/page';
80 $link['params'] = CMap::mergeArray($additionalParams, array(P3Page::PAGE_ID_KEY => $this->id, P3Page::PAGE_NAME_KEY => $this->t('seoUrl')));
81 }
82
83 if (isset($link['route'])) {
84 $params = (isset($link['params'])) ? $link['params'] : array();
85 if ($absolute === true)
86 return Yii::app()->controller->createAbsoluteUrl($link['route'], $params);
87 else
88 return Yii::app()->controller->createUrl($link['route'], $params);
89 } else {
90 Yii::log('Could not determine URL string for P3Page #'.$this->id , CLogger::LEVEL_WARNING);
91 }
92 }
93
94 public function isActive() {
95 if (self::getActivePage() !== null) {
96 return (self::getActivePage()->id == $this->id);
97 } else {
98 return false;
99 }
100 }
101
102 public function isActiveParent($model = null) {
103 if ($model === null) {
104 $model = $this;
105 }
106 if (count($model->p3PageMeta->p3PageMetas)) {
107 foreach($model->p3PageMeta->p3PageMetas AS $metaModel) {
108 if ((self::getActivePage()) && $metaModel->id0->id === self::getActivePage()->id) {
109 return true;
110 }
111 if (count($metaModel->p3PageMetas) && $metaModel->id0) {
112 return $this->isActiveParent($metaModel->id0);
113 }
114 }
115 }
116 return false;
117 }
118
119 public static function getActivePage() {
120 static $page;
121
122 if (isset($page)) {
123 return $page;
124 } elseif (isset($_GET[P3Page::PAGE_ID_KEY])) {
125 return $page = P3Page::model()->findByPk($_GET[P3Page::PAGE_ID_KEY]);
126 } elseif (isset($_GET[P3Page::PAGE_NAME_KEY])) {
127 return $page = P3Page::model()->findByAttributes(array('name' => $_GET[P2Page::PAGE_NAME_KEY]));
128 } else {
129
130 $criteria = new CDbCriteria;
131 $criteria->condition = "route LIKE :route";
132 $criteria->params = array(':route'=>"%".Yii::app()->controller->route."%");
133 return $page = P3Page::model()->find($criteria);
134 }
135 }
136
137 static public function ($rootNode) {
138
139
140 if (!$rootNode instanceof P3Page) {
141 Yii::log('Invalid root node', CLogger::LEVEL_WARNING);
142 return array();
143 }
144 $models = $rootNode->getChildren();
145 $items = array();
146 foreach ($models AS $model) {
147 if ($model->getMenuItems($model) === array( )){
148 $items[] = array('label' => $model->t('menuName', null, true), 'url' => $model->createUrl(), 'active' => ($model->isActive() || $model->isActiveParent()));
149 } else {
150 $items[] = array('label' => $model->t('menuName', null, true), 'url' => $model->createUrl(), 'items' => $model->getMenuItems($model), 'active' => ($model->isActive() || $model->isActiveParent()));
151 }
152 }
153 return $items;
154 }
155
156 }
157