1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
|
|
class ViewHelper
|
9
|
|
|
{
|
10
|
|
|
/**
|
11
|
|
|
* Строит виджет FMenu по входному массиву или масииву объектов
|
12
|
|
|
* @param CComponent[]|array $elements
|
13
|
|
|
* @param array $attributeOptions атрибуты для построения эламента массива из объектов
|
14
|
|
|
* @param array $menuOptions опции виджета FMenu
|
15
|
|
|
*
|
16
|
|
|
* @return array
|
17
|
|
|
*
|
18
|
|
|
* Пример использования:
|
19
|
|
|
* ViewHelper::menu($part, array('label' => 'name', 'url' => 'url'), array('itemTemplate' => '{menu} ({linkCount})'))
|
20
|
|
|
*/
|
21
|
7 |
|
public static function menu($elements, $attributeOptions = array(), $menuOptions = array())
|
22
|
|
|
{
|
23
|
7 |
|
$items = array();
|
24
|
|
|
|
25
|
7 |
|
foreach($elements as $key => $element)
|
26
|
|
|
{
|
27
|
7 |
|
if( $element instanceof CComponent )
|
|
|
|
|
28
|
7 |
|
{
|
29
|
|
|
$item = array();
|
30
|
|
|
foreach($attributeOptions as $name => $attribute)
|
31
|
|
|
{
|
32
|
|
|
$item[$name] = $element->$attribute;
|
33
|
|
|
}
|
34
|
|
|
}
|
35
|
|
|
else
|
36
|
|
|
{
|
37
|
7 |
|
$item = $element;
|
38
|
|
|
}
|
39
|
|
|
|
40
|
7 |
|
if( isset($menuOptions['itemTemplate']) )
|
41
|
7 |
|
{
|
42
|
|
|
$item['template'] = self::replace($menuOptions['itemTemplate'], $element);
|
43
|
|
|
}
|
44
|
|
|
|
45
|
7 |
|
$items[$key] = $item;
|
46
|
7 |
|
}
|
47
|
|
|
|
48
|
7 |
|
Yii::app()->controller->widget('FMenu', CMap::mergeArray($menuOptions, array('items' => $items)));
|
49
|
7 |
|
}
|
50
|
|
|
|
51
|
|
|
public static function header($header, $tag = 'h1', $htmlOptions = array())
|
52
|
|
|
{
|
53
|
|
|
echo CHtml::tag($tag, $htmlOptions, Yii::app()->meta->setHeader($header));
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* Заменяет содержащиеся в $template выражения вида {выражение} на своиства или заначения $data
|
58
|
|
|
* @param string $template шаблон
|
59
|
|
|
* @param array|CComponent $data источник данных для замены объект или массив
|
60
|
|
|
* @param bool $clearNotReplaced очистить не замененные выражения default false
|
61
|
|
|
*
|
62
|
|
|
* @return string
|
63
|
|
|
*/
|
64
|
1 |
|
public static function replace($template, $data, $clearNotReplaced = false)
|
65
|
|
|
{
|
66
|
1 |
|
if( preg_match_all('/{([^{}\s]*)}/', $template, $matches) )
|
67
|
1 |
|
{
|
68
|
|
|
$replaceArray = array();
|
69
|
|
|
foreach(Arr::reset($matches) as $matchIndex => $expression)
|
70
|
|
|
{
|
71
|
|
|
$attribute = $matches[1][$matchIndex];
|
72
|
|
|
|
73
|
|
|
if( is_array($data) && isset($data[$attribute]) )
|
74
|
|
|
$replaceArray[$expression] = $data[$attribute];
|
75
|
|
|
else if( is_object($data) && isset($data->{$attribute}) )
|
76
|
|
|
$replaceArray[$expression] = $data->{$attribute};
|
77
|
|
|
|
78
|
|
|
if( $attribute == 'url' && is_array($replaceArray[$expression]) )
|
79
|
|
|
$replaceArray[$expression] = CHtml::normalizeUrl($replaceArray[$expression]);
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
if( $replaceArray )
|
|
|
|
|
83
|
|
|
$template = strtr($template, $replaceArray);
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
if( $clearNotReplaced )
|
87
|
1 |
|
$template = preg_replace('/({[^{}\s]*})/', '', $template);
|
88
|
|
|
|
89
|
1 |
|
return $template;
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
/**
|
93
|
|
|
* @return Contact|FActiveRecord|null
|
94
|
|
|
*/
|
95
|
7 |
|
public static function contact()
|
96
|
|
|
{
|
97
|
7 |
|
if( $contact = Yii::app()->controller->getHeaderContacts() )
|
98
|
7 |
|
return $contact;
|
99
|
|
|
|
100
|
|
|
return null;
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* @return ContactField[]
|
105
|
|
|
*/
|
106
|
7 |
|
public static function phones()
|
107
|
|
|
{
|
108
|
7 |
|
if( $contact = self::contact() )
|
109
|
7 |
|
{
|
110
|
7 |
|
return $contact->getFields('phones');
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
return array();
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
/**
|
117
|
|
|
* @param bool $clear
|
118
|
|
|
*
|
119
|
|
|
* @return ContactField|null|string
|
120
|
|
|
*/
|
121
|
|
|
public static function phone($clear = false)
|
122
|
|
|
{
|
123
|
|
|
if( $phones = self::phones() )
|
124
|
|
|
{
|
125
|
|
|
$phone = Arr::reset($phones);
|
126
|
|
|
|
127
|
|
|
return $clear ? $phone->getClearPhone() : $phone;
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
return null;
|
131
|
|
|
}
|
132
|
|
|
|
133
|
7 |
|
public static function getClearPhone($phone)
|
134
|
|
|
{
|
135
|
7 |
|
$clearPhone = preg_replace('/[^\d\+]/', '', $phone);
|
136
|
|
|
|
137
|
7 |
|
if( $clearPhone == 10 )
|
138
|
7 |
|
{
|
139
|
|
|
if( $clearPhone[0] == 8 )
|
140
|
|
|
$clearPhone = ltrim($clearPhone, '8');
|
141
|
|
|
$clearPhone = '+'.$clearPhone;
|
142
|
|
|
}
|
143
|
|
|
|
144
|
7 |
|
return $clearPhone;
|
145
|
|
|
}
|
146
|
|
|
} |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.