1 <?php
2
3 namespace Objednavky;
4
5 use Nette\Application\UI;
6 use Nette\Utils\Html;
7 use Nette\Database\Table\Selection;
8 use Kdyby\BootstrapFormRenderer\BootstrapRenderer;
9 use PLus\Orders\Repositories;
10
11 12 13 14 15
16 class ConsultationPresenter extends BasePresenter {
17
18
19
20 public $orderRepository;
21
22
23 public $ordertypeRepository;
24
25
26
27 public $unituserRepository;
28
29
30 public $unitRepository;
31
32
33 public $userRepository;
34
35
36 public $roleRepository;
37
38
39 public $consultationRepository;
40
41
42 public $postRepository;
43
44
45
46 public $cfgRepository;
47
48
49 public $parameters;
50
51
52 public $mailerCfg;
53
54
55
56 57 58 59 60
61 public function actionView($id) {
62 $lastpost = $this->postRepository->getLastPost($id);
63 if($lastpost->user_id != $this->getUser()->getId()) {
64
65
66 $this->consultationRepository->updateById($id, array('visit' => NULL));
67 }
68 }
69
70 public function renderView($id) {
71
72 $user = $this->getUser();
73 $item = $this->consultationRepository->findById($id);
74
75
76 if ($item === false) {
77 $this->flashMessage('Konzultace č. '. $id .' nebyla dohledána.', 'error');
78 $this->redirect($this->getSession()->getSection('history')->beforePresenter.':view',$this->getSession()->getSection('history')->beforeId);
79 }
80
81
82 if ($item->from != $user->getId() && $item->to != $user->getId()) {
83 $this->flashMessage('Nemáte právo přístupu ke konzultaci č. '.$id, 'error');
84 $this->redirect($this->getSession()->getSection('history')->beforePresenter.':view',$this->getSession()->getSection('history')->beforeId);
85 }
86
87
88 $this->template->consultationhead = $this->consultationRepository->getConsultationOrderHeadById($id,$item->order_id);
89 $this->template->posts = $this->postRepository->getPost($item->id);
90
91
92 $this->template->linkback = $this->getSession()->getSection('history')->beforePresenter;
93 }
94
95
96 97 98 99 100
101 protected function createComponentPostForm() {
102
103 $form = new UI\Form();
104 $form->setRenderer(new BootstrapRenderer());
105
106 $form->getElementPrototype()->class = 'form-horizontal2';
107
108 $form->addTextArea('post', 'Odpověď:', NULL, 6)
109 ->setAttribute('class', 'form-control')
110 ->setRequired('Vložte odpověď');
111
112 $form->addSubmit('reply', 'Odpovědět')
113 ->setAttribute('class', 'btn btn-primary');
114
115 $form->onSuccess[] = array($this,'postFormSubmitted');
116
117 return $form;
118 }
119
120
121 public function postFormSubmitted($form, $values) {
122
123 try {
124
125 $user = $this->getUser();
126 $consultation_id = $this->getParameter('id');
127 if (mb_strlen(trim($values->post)) > 0) {
128
129
130 $this->postRepository->insertData(array(
131 'consultation_id' => $consultation_id,
132 'user_id' => $user->getId(),
133 'created' => date('Y-m-d H:i:s'),
134 'message' => $values->post
135 ));
136
137 $this->consultationRepository->updateById($consultation_id, array('lastmod' => date('Y-m-d H:i:s'), 'lastpostman' => $user->getId(),'visit' => 1));
138
139
140 $uri = $this->getHttpRequest()->getUrl();
141 $from = $this->parameters->getFrom();
142 $support = $this->parameters->getSupport();
143
144 $consultation = $this->consultationRepository->findById($consultation_id);
145 if($consultation->from != $user->getId()) {
146 $to = $this->userRepository->getUserEmail($consultation->from);
147 $roles = $this->roleRepository->findRoleById($consultation->from);
148 }
149 else {
150 $to = $this->userRepository->getUserEmail($consultation->to);
151 $roles = $this->roleRepository->findRoleById($consultation->to);
152 }
153
154 $is_consultant = FALSE;
155 foreach ($roles as $role) {
156 if($role == 'consultant') {
157 $is_consultant = TRUE;
158 }
159 }
160
161 $subject = 'Konzultace k objednávce';
162 $params = array(
163 'order_id' => $consultation->order_id,
164 'contact' => $support,
165 'url' => $uri->hostUrl.'/consultant&execution=view&id='.$consultation->order_id,
166 'consultation_id' => $consultation_id,
167 'path' => ($is_consultant)? 'Konzultant' : 'Konzultace'
168
169 );
170 $template = __DIR__ . '/../templates/Mailer/emailToConsultant.latte';
171 $image = __DIR__ . '/../../www/img/logo32x32.gif';
172 $mailer = $this->mailerCfg->getMailConfig();
173
174 MailerPresenter::sendMail($from, $to, $subject, $params, $template, $image, $mailer);
175
176
177 $this->flashMessage('Odpověď byla uložena.', 'success');
178 $this->redirect('this');
179 }
180 else {
181 $this->flashMessage('Nelze odeslat prázdnou odpověď.', 'error');
182 }
183 } catch (\PDOException $ex) {
184 $this->flashMessage('Chyba při uložení odpovědi.', 'error');
185 }
186 }
187
188 }
189