Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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