Rev 335 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Controller;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Mvc\Controller\AbstractActionController;use Laminas\View\Model\ViewModel;use Laminas\View\Model\JsonModel;use LeadersLinked\Library\Functions;use LeadersLinked\Model\HabitSkill;use LeadersLinked\Mapper\HabitSkillMapper;use LeadersLinked\Form\Habit\HabitSkillForm;class HabitSkillController extends AbstractActionController{/**** @var \Laminas\Db\Adapter\AdapterInterface*/private $adapter;/**** @var \LeadersLinked\Cache\CacheInterface*/private $cache;/**** @var \Laminas\Log\LoggerInterface*/private $logger;/**** @var array*/private $config;/**** @var \Laminas\Mvc\I18n\Translator*/private $translator;/**** @param \Laminas\Db\Adapter\AdapterInterface $adapter* @param \LeadersLinked\Cache\CacheInterface $cache* @param \Laminas\Log\LoggerInterface LoggerInterface $logger* @param array $config* @param \Laminas\Mvc\I18n\Translator $translator*/public function __construct($adapter, $cache, $logger, $config, $translator){$this->adapter = $adapter;$this->cache = $cache;$this->logger = $logger;$this->config = $config;$this->translator = $translator;}public function indexAction(){$request = $this->getRequest();$request = $this->getRequest();if($request->isGet()) {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/add');$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/edit');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/delete');$allowRegister = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/registers');$search = $this->params()->fromQuery('search');$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);$page = intval($this->params()->fromQuery('start', 1), 10);$records_x_page = intval($this->params()->fromQuery('length', 10), 10);$order = $this->params()->fromQuery('order', []);$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));$fields = ['name'];$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';if(!in_array($order_direction, ['ASC', 'DESC'])) {$order_direction = 'ASC';}$habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);$paginator = $habitSkillMapper->fetchAllDataTable($currentUser->id, $search, $page, $records_x_page, $order_field, $order_direction);$items = [];$records = $paginator->getCurrentItems();foreach($records as $record){$item = ['id' => $record->uuid,'name' => $record->name,'description' => $record->description,'intelligence' => $record->intelligence,'actions' => ['link_edit' => $allowEdit ? $this->url()->fromRoute('habits/skills/edit', ['id' => $record->uuid ], ['force_canonical' => true]) : '','link_delete' => $allowDelete ? $this->url()->fromRoute('habits/skills/delete', ['id' =>$record->uuid ], ['force_canonical' => true]) : '','link_registers' => $allowRegister ? $this->url()->fromRoute('habits/skills/registers', ['id' =>$record->uuid ], ['force_canonical' => true]) : '',]];array_push($items, $item);}return new JsonModel(['success' => true,'data' => ['items' => $items,'total' => $paginator->getTotalItemCount(),'link_add' => $allowAdd ? $this->url()->fromRoute('habits/skills/add', [], ['force_canonical' => true]) : '',]]);} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);;}}public function addAction(){$request = $this->getRequest();if($request->isPost()) {$form = new HabitSkillForm($this->adapter);$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if($form->isValid()) {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$dataPost = (array) $form->getData();$habitSkill = new HabitSkill();$hydrator = new \LeadersLinked\Hydrator\ObjectPropertyHydrator();$hydrator->hydrate($dataPost, $habitSkill);$habitSkill->network_id = $currentUser->network_id;$habitSkill->user_id = $currentUser->id;$habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);$result = $habitSkillMapper->insert($habitSkill );if($result) {$this->logger->info('Se agrego el valor : ' . $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$habitSkill = $habitSkillMapper->fetchOne($habitSkill->id);$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/edit');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/delete');$data = ['success' => true,'message' => 'LABEL_RECORD_ADDED','data' => ['id' => $habitSkill->uuid,'name' => $habitSkill->name,'description' => $habitSkill->description,'actions' => ['link_edit' => $allowEdit ? $this->url()->fromRoute('habits/skills/edit', ['id' => $habitSkill->uuid ], ['force_canonical' => true]) : '','link_delete' => $allowDelete ? $this->url()->fromRoute('habits/skills/delete', ['id' => $habitSkill->uuid ], ['force_canonical' => true]) : '',]]];} else {$data = ['success' => false,'data' => $habitSkillMapper->getError()];}return new JsonModel($data);} else {$messages = [];$form_messages = (array) $form->getMessages();foreach($form_messages as $fieldname => $field_messages){$messages[$fieldname] = array_values($field_messages);}return new JsonModel(['success' => false,'data' => $messages]);}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}public function editAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$id = $this->params()->fromRoute('id');if(!$id) {return new JsonModel(['success' => false,'data' => 'ERROR_INVALID_PARAMETER']);}$habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);$habitSkill = $habitSkillMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);if(!$habitSkill) {return new JsonModel(['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND']);}if($currentUser->id != $habitSkill->user_id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if($request->isGet()) {return new JsonModel(['success' => true,'data' => ['name' => $habitSkill->name,'description' => $habitSkill->description,'monday_active' => $habitSkill->monday_active,'tuesday_active' => $habitSkill->tuesday_active,'wednesday_active' => $habitSkill->wednesday_active,'thursday_active' => $habitSkill->thursday_active,'friday_active' => $habitSkill->friday_active,'saturday_active' => $habitSkill->saturday_active,'sunday_active' => $habitSkill->sunday_active,'monday_time' => $habitSkill->monday_time,'tuesday_time' => $habitSkill->tuesday_time,'wednesday_time' => $habitSkill->wednesday_time,'thursday_time' => $habitSkill->thursday_time,'friday_time' => $habitSkill->friday_time,'saturday_time' => $habitSkill->saturday_time,'sunday_time' => $habitSkill->sunday_time,'quantitative_value' => $habitSkill->quantitative_value,'qualitative_description' => $habitSkill->qualitative_description,'notification_10min_before' => $habitSkill->notification_10min_before,'notification_30min_before' => $habitSkill->notification_30min_before,'intelligence' => $habitSkill->intelligence,]]);} else if($request->isPost()) {$form = new HabitSkillForm($this->adapter);$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if($form->isValid()) {$dataPost = (array) $form->getData();$hydrator = new \LeadersLinked\Hydrator\ObjectPropertyHydrator();$hydrator->hydrate($dataPost, $habitSkill);$result = $habitSkillMapper->update($habitSkill);if($result) {$this->logger->info('Se edito el valor : ' . $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/edit');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/delete');$data = ['success' => true,'message' => 'LABEL_RECORD_UPDATED','data' => ['id' => $habitSkill->uuid,'name' => $habitSkill->name,'description' => $habitSkill->description,'actions' => ['link_edit' => $allowEdit ? $this->url()->fromRoute('habits/skills/edit', ['id' => $habitSkill->uuid ], ['force_canonical' => true]) : '','link_delete' => $allowDelete ? $this->url()->fromRoute('habits/skills/delete', ['id' => $habitSkill->uuid ], ['force_canonical' => true]) : '',]]];} else {$data = ['success' => false,'data' => $habitSkillMapper->getError()];}return new JsonModel($data);} else {$messages = [];$form_messages = (array) $form->getMessages();foreach($form_messages as $fieldname => $field_messages){$messages[$fieldname] = array_values($field_messages);}return new JsonModel(['success' => false,'data' => $messages]);}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}public function deleteAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$id = $this->params()->fromRoute('id');if(!$id) {return new JsonModel(['success' => false,'data' => 'ERROR_INVALID_PARAMETER']);}$habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);$habitSkill = $habitSkillMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);if(!$habitSkill) {return new JsonModel(['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND']);}if($currentUser->id != $habitSkill->user_id) {$response = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($response);}if($request->isPost()) {$result = $habitSkillMapper->delete($habitSkill);if($result) {$this->logger->info('Se borro el valor : ' . $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);return new JsonModel(['success' => true,'data' => 'LABEL_RECORD_DELETED']);} else {return new JsonModel(['success' => false,'data' => $habitSkillMapper->getError()]);}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}}