Controller.php
7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class Controller extends CController
{
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout = '//layouts/main';
/**
* @var array context menu items. This property will be assigned to {@link CMenu::items}.
*/
public $menu = array();
/**
* @var array the breadcrumbs of the current page. The value of this property will
* be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs = array();
public $keywords = null;
public $description = null;
public $pageName;
private $_pageTitle;
public $headerGalleryId;
public $sectionGalleryId;
public $socialLinks;
public $socialLinksSport;
/** @var ContactForm */
public $contactForm;
public $siteData;
private $_contacts;
public function setSEOParams($title = null, $keywords = null, $description = null)
{
if ($title) $this->pageTitle = $title;
if ($keywords) $this->keywords = $keywords;
if ($description) $this->description = $description;
}
/**
* @return string the page title. Defaults to the controller name and the action name.
*/
public function getPageTitle()
{
if ($this->_pageTitle !== null)
return $this->_pageTitle;
else {
if (!empty($this->pageName))
$this->_pageTitle = $this->pageName . " | " . Yii::t('site', 'Авто лайф');
else
$this->_pageTitle = Yii::t('site', 'Авто лайф');
return $this->_pageTitle;
}
}
/**
* @param string $value the page title.
*/
public function setPageTitle($value)
{
$this->_pageTitle = $value;
}
/**
* @param $section
* @param $key
* @return array
*/
public function loadStaticPage($section, $key)
{
/** @var $page StaticPage */
$page = StaticPage::modelByPk($section, $key);
$this->pageName = $page->i18n->name;
$this->setSEOParams($page->i18n->title, $page->i18n->keywords, $page->i18n->description);
return $page->getDataAttributes() + $page->i18n->getDataAttributes();
}
public function init()
{
parent::init();
/** @var $dataModel StaticData */
$dataModel = StaticData::model()->with('i18n')->findByPk(array('section' => 'site', 'key' => 'general'));
// CVarDumper::dump($dataModel->i18n,10,true); exit();
$this->siteData = $dataModel->getDataAttributes() + $dataModel->i18n->getDataAttributes();
$this->headerGalleryId = $this->siteData['gallery_id'];
$this->socialLinks = $this->siteData['social_links'];
$this->socialLinksSport = $this->siteData['social_links_sport'];
}
protected function beforeAction($action)
{
$res = parent::beforeAction($action);
if ($res) {
$this->contactForm = new ContactForm();
if (isset($_POST['ContactForm'])) {
Yii::app()->request->validateCsrfToken(null);
$this->contactForm->setAttributes($_POST['ContactForm']);
$route = $this->id . '/' . $this->action->id;
$disabledRoutes = array(
'sport/index',
'site/index',
'calendarSection/calendar',
'albumsSection/albums',
'albumsSection/album',
'warranty/index',
'site/contact',
);
if ($this->contactForm->validate() && empty($this->contactForm->link) && (!in_array($route, $disabledRoutes))) {
/** @var $contact Contact */
$contact = Contact::model()->findByPk($this->contactForm->contact);
/** @var $mailer Swift_Mailer */
$mailer = Yii::app()->swiftMailer->getMailer();
$message = Swift_Message::newInstance();
$message->setReplyTo($this->contactForm->email);
$message->setFrom('site@auto-life.ua');
$message->setTo($contact->email);
$args = Swift_DependencyContainer::getInstance()
->createDependenciesFor('mime.message');
/** @var $grammar Swift_Mime_Grammar */
$grammar = $args[3];
if (isset($this->siteData['bcc_emails']) && (!empty($this->siteData['bcc_emails'])))
foreach (explode(',', $this->siteData['bcc_emails']) as $e)
if (preg_match('/^' . $grammar->getDefinition('addr-spec') . '$/D', $e))
$message->addBcc($e);
if (!empty($contact->bcc_email))
foreach (explode(',', $contact->bcc_email) as $e) {
if (preg_match('/^' . $grammar->getDefinition('addr-spec') . '$/D', $e))
$message->addBcc($e);
}
$message->setSubject('Вопрос, c сайта Авто-лайф');
$message->setBody(
"Имя: {$this->contactForm->name}\r\n" .
"E-Mail: {$this->contactForm->email}\r\n" .
"Контактный номер: {$this->contactForm->phone}\r\n" .
"Текст вопроса: {$this->contactForm->body}\r\n" .
"\r\nОтправлено со страницы: " . Yii::app()->getBaseUrl(true) . Yii::app()->request->getUrl());
$mailer->send($message);
Yii::app()->user->setFlash('contact', Yii::t('site', 'ВАШ ЗАПРОС УСПЕШНО ОТПРАВЛЕН! <br/>
Благодарим Bас за уделенное время, мы постараемся максимально быстро и оперативно ответить Вам.'));
//$this->refresh();
}
}
}
return $res;
}
/** @return GalleryPhoto[] */
public function getHeaderPhotos()
{
$criteria = new CDbCriteria();
$criteria->condition = 'gallery_id = :gallery_id';
$criteria->params[':gallery_id'] = $this->sectionGalleryId;
$criteria->order = 'rand()';
$r1 = GalleryPhoto::model()->findAll($criteria);
if (count($r1) > 0) return $r1;
$criteria->params[':gallery_id'] = $this->headerGalleryId;
return GalleryPhoto::model()->findAll($criteria);
}
public function setContacts($contacts, $force = false)
{
if (empty($contacts)) return;
foreach ($contacts as $k => $v) {
if (empty($v)) unset($contacts[$k]);
}
if (count($contacts) > 0 && ($this->_contacts === null || $force))
$this->_contacts = $contacts;
}
public function getContacts()
{
$contacts = array();
if (isset($this->_contacts) && (!empty($this->_contacts))) {
$criteria = new CDbCriteria();
$criteria->alias = 't';
$criteria->addInCondition('t.id', $this->_contacts);
$criteria->index = 'id';
$data = Contact::model()->with('i18n')->findAll($criteria);
foreach ($this->_contacts as $id) {
if (isset($data[$id])) $contacts[] = $data[$id];
}
}
return $contacts;
}
}