Rev 17010 | 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(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();$request = $this->getRequest();if($request->isGet()) {$headers = $request->getHeaders();$isJson = false;if($headers->has('Accept')) {$accept = $headers->get('Accept');$prioritized = $accept->getPrioritized();foreach($prioritized as $key => $value) {$raw = trim($value->getRaw());if(!$isJson) {$isJson = strpos($raw, 'json');}}}if($isJson) {$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');$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->fetchAllDataTableTemplates($currentCompany->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,'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]) : '',]];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 {$form = new HabitSkillForm();$this->layout()->setTemplate('layout/layout-backend');$viewModel = new ViewModel();$viewModel->setTemplate('leaders-linked/habits/skills.phtml');$viewModel->setVariable('form', $form);return $viewModel ;}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);;}}public function addAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();$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->company_id = $currentCompany->id;$habitSkill->monday_time = $dataPost['monday_hour']. ':' . $dataPost['monday_minute'];$habitSkill->tuesday_time = $dataPost['tuesday_hour']. ':' . $dataPost['tuesday_minute'];$habitSkill->wednesday_time = $dataPost['wednesday_hour']. ':' . $dataPost['wednesday_minute'];$habitSkill->thursday_time = $dataPost['thursday_hour']. ':' . $dataPost['thursday_minute'];$habitSkill->friday_time = $dataPost['friday_hour']. ':' . $dataPost['friday_minute'];$habitSkill->saturday_time = $dataPost['saturday_hour']. ':' . $dataPost['saturday_minute'];$habitSkill->sunday_time = $dataPost['monday_hour']. ':' . $dataPost['sunday_minute'];$habitSkill->template = HabitSkill::TEMPLATE_YES;$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();$currentCompany = $currentUserPlugin->getCompany();$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($currentCompany->id != $habitSkill->company_id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if($request->isGet()) {list($hour, $minute) = explode(':', $habitSkill->monday_time);$monday_hour = $hour;$monday_minute = $minute;list($hour, $minute) = explode(':', $habitSkill->tuesday_time);$tuesday_hour = $hour;$tuesday_minute = $minute;list($hour, $minute) = explode(':', $habitSkill->wednesday_time);$wednesday_hour = $hour;$wednesday_minute = $minute;list($hour, $minute) = explode(':', $habitSkill->thursday_time);$thursday_hour = $hour;$thursday_minute = $minute;list($hour, $minute) = explode(':', $habitSkill->friday_time);$friday_hour = $hour;$friday_minute = $minute;list($hour, $minute) = explode(':', $habitSkill->saturday_time);$saturday_hour = $hour;$saturday_minute = $minute;list($hour, $minute) = explode(':', $habitSkill->sunday_time);$sunday_hour = $hour;$sunday_minute = $minute;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_hour' => $monday_hour,'tuesday_hour' => $tuesday_hour,'wednesday_hour' => $wednesday_hour,'thursday_hour' => $thursday_hour,'friday_hour' => $friday_hour,'saturday_hour' => $saturday_hour,'sunday_hour' => $sunday_hour,'monday_minute' => $monday_minute,'tuesday_minute' => $tuesday_minute,'wednesday_minute' => $wednesday_minute,'thursday_minute' => $thursday_minute,'friday_minute' => $friday_minute,'saturday_minute' => $saturday_minute,'sunday_minute' => $sunday_minute,'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);$habitSkill->monday_time = $dataPost['monday_hour']. ':' . $dataPost['monday_minute'];$habitSkill->tuesday_time = $dataPost['tuesday_hour']. ':' . $dataPost['tuesday_minute'];$habitSkill->wednesday_time = $dataPost['wednesday_hour']. ':' . $dataPost['wednesday_minute'];$habitSkill->thursday_time = $dataPost['thursday_hour']. ':' . $dataPost['thursday_minute'];$habitSkill->friday_time = $dataPost['friday_hour']. ':' . $dataPost['friday_minute'];$habitSkill->saturday_time = $dataPost['saturday_hour']. ':' . $dataPost['saturday_minute'];$habitSkill->sunday_time = $dataPost['monday_hour']. ':' . $dataPost['sunday_minute'];$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();$currentCompany = $currentUserPlugin->getCompany();$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($currentCompany->id != $habitSkill->company_id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}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']);}}}