Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
12260 eleazar 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\MyTrainerCategoriesMapper;
15
use LeadersLinked\Mapper\TopicMapper;
16
use LeadersLinked\Mapper\FeedTopicMapper;
17
use LeadersLinked\Mapper\MyTrainerQuestionsMapper;
12652 eleazar 18
use LeadersLinked\Mapper\QueryMapper;
12260 eleazar 19
use LeadersLinked\Mapper\FeedMapper;
20
use LeadersLinked\Form\MyTrainerQuestionForm;
21
use LeadersLinked\Form\CreateFeedForm;
22
use LeadersLinked\Model\MyTrainerQuestions;
23
use LeadersLinked\Model\FeedTopic;
24
use LeadersLinked\Model\Feed;
25
use LeadersLinked\Model\Topic;
26
use LeadersLinked\Mapper\CompanyMapper;
27
use LeadersLinked\Model\Company;
28
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
29
 
12286 eleazar 30
class MyTrainerQuestionViewController extends AbstractActionController {
12260 eleazar 31
 
32
    /**
33
     *
34
     * @var AdapterInterface
35
     */
36
    private $adapter;
37
 
38
    /**
39
     *
40
     * @var AbstractAdapter
41
     */
42
    private $cache;
43
 
44
    /**
45
     *
46
     * @var  LoggerInterface
47
     */
48
    private $logger;
49
 
50
    /**
51
     *
52
     * @var array
53
     */
54
    private $config;
55
 
56
    /**
57
     *
58
     * @param AdapterInterface $adapter
59
     * @param AbstractAdapter $cache
60
     * @param LoggerInterface $logger
61
     * @param array $config
62
     */
63
    public function __construct($adapter, $cache, $logger, $config) {
64
        $this->adapter = $adapter;
65
        $this->cache = $cache;
66
        $this->logger = $logger;
67
        $this->config = $config;
68
    }
69
 
70
    public function indexAction() {
71
        $request = $this->getRequest();
72
        $currentUserPlugin = $this->plugin('currentUserPlugin');
73
        $currentCompany = $currentUserPlugin->getCompany();
74
        $currentUser = $currentUserPlugin->getUser();
75
 
76
        $id =  $this->params()->fromRoute('id');
77
 
12962 eleazar 78
 
12260 eleazar 79
        $request = $this->getRequest();
12962 eleazar 80
 
12260 eleazar 81
 
82
        if($request->isGet()) {
83
            $headers  = $request->getHeaders();
84
 
85
            $isJson = false;
86
            if($headers->has('Accept')) {
87
                $accept = $headers->get('Accept');
88
 
89
                $prioritized = $accept->getPrioritized();
90
 
91
                foreach($prioritized as $key => $value) {
92
                    $raw = trim($value->getRaw());
93
 
94
                    if(!$isJson) {
95
                        $isJson = strpos($raw, 'json');
96
                    }
12962 eleazar 97
 
12260 eleazar 98
                }
99
            }
100
 
12962 eleazar 101
                $topicMapper = TopicMapper::getInstance($this->adapter);
102
                $topic = $topicMapper->fetchOneByUUid($id);
12296 eleazar 103
 
12962 eleazar 104
                $topicsMapper = FeedTopicMapper::getInstance($this->adapter);
105
                $feedTopics = $topicsMapper->fetchAllByTopicId($topic->id);
106
 
107
                foreach ($feedTopics as $feed) {
108
                    $questionsMapper = FeedMapper::getInstance($this->adapter);
12698 eleazar 109
 
12962 eleazar 110
                    if ($question = $questionsMapper->fetchOne($feed->feed_id)) {
111
                        $questions [] = $question;
112
                    }
12296 eleazar 113
                }
12260 eleazar 114
 
12962 eleazar 115
                $topics = [];
116
                foreach ($questions as $question) {
117
                    $topicRelationships = $topicsMapper->fetchAllByFeedId($question->id);
118
                    $question->topics = [];
119
 
120
 
121
                    foreach ($topicRelationships as $rel) {
122
                        $topic = $topicMapper->fetchOne($rel->topic_id);
123
                        if ($topic) {
124
                            $question->topics []= $topic;
125
                        }
12364 eleazar 126
                    }
12962 eleazar 127
 
12364 eleazar 128
                }
12962 eleazar 129
 
130
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
131
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/add');
132
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/edit');
133
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/delete');
134
                $allowAnswer = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/answer');
135
                $page = $this->params()->fromQuery('page', 1);
136
                $perpage = $this->params()->fromQuery('perpage', 20);
137
 
138
                $form = new CreateFeedForm($this->adapter);
139
 
140
 
141
                $viewModel = new ViewModel();
142
                $this->layout()->setTemplate('layout/layout-backend');
143
                $viewModel->setTemplate('leaders-linked/my-trainer-question-view/index.phtml');
144
                $viewModel->setVariables([
145
                    'form' => $form,
146
                    'id' => $id,
147
                    'page' => $page,
148
                    'perpage' => $perpage,
149
                    'questions' => $questions,
150
                    // 'categories' => $categories
151
                ]);
152
                return $viewModel;
12260 eleazar 153
        } else {
154
            return new JsonModel([
155
                'success' => false,
156
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
157
            ]);
158
        }
12962 eleazar 159
 
160
 
12260 eleazar 161
    }
162
 
163
    public function addAction() {
164
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
165
        $currentCompany     = $currentUserPlugin->getCompany();
166
        $currentUser        = $currentUserPlugin->getUser();
167
 
168
        $request    = $this->getRequest();
169
        try{
170
        if($request->isPost()) {
171
            $form = new  CreateFeedForm($this->adapter);
172
            $dataPost = $request->getPost()->toArray();
173
            $categoriesId = $dataPost['category_id'] ?? [];
174
            // return new JsonModel([
175
            //     'success' => false,
176
            //     'data' => $categoriesId
177
            // ]);
178
            $dataPost['category_id'] = null;
179
            $form->setData($dataPost);
180
 
181
            if($form->isValid()) {
182
 
183
                $dataPost = (array) $form->getData();
184
                $hydrator = new ObjectPropertyHydrator();
185
                $feed = new Feed();
186
                $hydrator->hydrate($dataPost, $feed);
187
 
188
                $feed->user_id = $currentUser->id;
189
                $feed->company_id = $currentCompany->id;
190
                $feed->user_id          = $currentUser->id;
191
                $feed->type             = Feed::TYPE_MYT_QUESTION;
192
                $feed->posted_or_shared = Feed::POSTED;
193
                $feed->shared_with      = Feed::SHARE_WITH_PUBLIC;
194
                $feed->total_comments   = 0;
195
                $feed->total_shared     = 0;
196
 
197
                $feedMapper = FeedMapper::getInstance($this->adapter);
198
 
199
                if($feedMapper->insert($feed)) {
200
                     $feed = $feedMapper->fetchOne($feed->id);
201
 
202
                     $category = new FeedTopic();
203
 
204
                    if(!empty($categoriesId)){
205
                       // print_r($dataPost['job_description_id']);
206
 
207
                        $categoriesMapper = TopicMapper::getInstance($this->adapter);
208
                        $feedTopicMapper = FeedTopicMapper::getInstance($this->adapter);
209
 
210
                        $ok = true;
211
 
212
                        foreach($categoriesId as $category_id) {
213
 
214
                           // echo '$jobDescriptionUuid = ' . $jobDescriptionUuid ;
215
 
216
 
217
                            $category = $categoriesMapper->fetchOneByUuidOrTitle($category_id);
218
                            if(!$category){
219
                                $category = new Topic;
220
                                $category->title = $category_id;
221
                                $category->type='myt';
222
                                $categoriesMapper->insert($category);
223
                            }
224
 
225
                            $record = new FeedTopic();
226
                            $record->topic_id = $category->id;
227
                            $record->feed_id = $feed->id;
228
                            $result = $feedTopicMapper->insert($record);
229
                            $ok = $ok && $result;
230
                        }
231
                    }
232
 
233
                     $feed->related_feed = $feed->id;
234
                     $feedMapper->update($feed);
235
                    $this->logger->info('Se agrego la pregunta ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
236
 
237
                    $data = [
238
                        'success'   => true,
239
                        'data'   => 'LABEL_RECORD_ADDED'
240
                    ];
241
                } else {
242
                    $data = [
243
                        'success'   => false,
244
                        'data'      => $feedMapper->getError()
245
                    ];
246
 
247
                }
248
 
249
                return new JsonModel($data);
250
 
251
            } else {
252
                $messages = [];
253
                $form_messages = (array) $form->getMessages();
254
                foreach($form_messages  as $fieldname => $field_messages)
255
                {
256
 
257
                    $messages[$fieldname] = array_values($field_messages);
258
                }
259
 
260
            }
261
 
262
        } else {
263
            $data = [
264
                'success' => false,
265
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
266
            ];
267
 
268
            return new JsonModel($data);
269
        }
270
 
271
    } catch (\Throwable $e) {
272
        $e->getMessage();
273
        return new JsonModel([
274
            'success' => false,
275
            'data' => $e
276
        ]);
277
    }
278
    }
279
 
280
 
281
 
282
    public function deleteAction() {
283
        $currentUserPlugin = $this->plugin('currentUserPlugin');
284
        $currentUser = $currentUserPlugin->getUser();
285
        $currentCompany = $currentUserPlugin->getCompany();
286
 
287
        $request = $this->getRequest();
288
        if($request->isPost()) {
289
 
290
 
291
            $id =  $this->params()->fromRoute('id');
292
 
293
            $feedMapper = FeedMapper::getInstance($this->adapter);
294
            $feed = $feedMapper->fetchOneByUuid($id);
295
            if(!$feed) {
296
                $response = [
297
                    'success' => false,
298
                    'data' => 'ERROR_POST_NOT_FOUND'
299
                ];
300
                return new JsonModel($response);
301
            }
302
 
303
            if($feed->company_id != $currentCompany->id) {
304
                $response = [
305
                    'success' => false,
306
                    'data' => 'ERROR_UNAUTHORIZED'
307
                ];
308
                return new JsonModel($response);
309
            }
310
 
311
            $feedMapper = FeedMapper::getInstance($this->adapter);
312
            $feed->status = Feed::STATUS_DELETED;
313
            if($feedMapper->update($feed)) {
314
                $response = [
315
                    'success' => true,
316
                    'data' => 'LABEL_FEED_WAS_DELETED'
317
                ];
318
 
319
            } else {
320
                $response = [
321
                    'success' => false,
322
                    'data' => $feedMapper->getError()
323
                ];
324
            }
325
 
326
 
327
            return new JsonModel($response);
328
 
329
        } else {
330
            $response = [
331
                'success' => false,
332
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
333
            ];
334
        }
335
 
336
        return new JsonModel($response);
337
 
338
    }
339
}