Rev 310 | 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\HabitGoal;use LeadersLinked\Mapper\HabitGoalMapper;use LeadersLinked\Form\Habit\HabitGoalForm;class HabitGoalController 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();if($request->isGet()) {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/add');$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/edit');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/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';}$habitGoalMapper = HabitGoalMapper::getInstance($this->adapter);$paginator = $habitGoalMapper->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,'actions' => ['link_edit' => $allowEdit ? $this->url()->fromRoute('habits/goals/edit', ['id' => $record->uuid ], ['force_canonical' => true]) : '','link_delete' => $allowDelete ? $this->url()->fromRoute('habits/goals/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/goals/add', [], ['force_canonical' => true]) : '',]]);} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);;}}public function addAction(){$request = $this->getRequest();if($request->isGet()) {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$skills = [];$habitSkillMapper = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);$records = $habitSkillMapper->fetchAllByUserId($currentUser->id);foreach($records as $record){$skills[ $record->uuid ] = $record->name;}return new JsonModel(['success' => true,'data' => $skills]);} else if($request->isPost()) {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$form = new HabitGoalForm($this->adapter, $currentUser->id);$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if($form->isValid()) {$dataPost = (array) $form->getData();$habitGoal = new HabitGoal();$habitGoal->network_id = $currentUser->network_id;$habitGoal->user_id = $currentUser->id;$habitGoal->name = $dataPost['name'];$habitGoal->description = $dataPost['description'];$habitGoal->value = $dataPost['value'];$habitGoal->start_date = $dataPost['start_date'];$habitGoal->end_date = $dataPost['end_date'];$habitGoalMapper = \LeadersLinked\Mapper\HabitGoalMapper::getInstance($this->adapter);$result = $habitGoalMapper->insert($habitGoal );if($result) {if(!empty($dataPost['skill_id'])) {$habitSkillMapper = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);$habitGoalSkillMapper = \LeadersLinked\Mapper\HabitGoalSkillMapper::getInstance($this->adapter);foreach($dataPost['skill_id'] as $uuid){$habitSkill = $habitSkillMapper->fetchOneByUuid($uuid);if($habitSkill->user_id == $currentUser->id) {$habitGoalSkill = new \LeadersLinked\Model\HabitGoalSkill();$habitGoalSkill->network_id = $currentUser->network_id;$habitGoalSkill->user_id = $currentUser->id;$habitGoalSkill->goal_id = $habitGoal->id;$habitGoalSkill->skill_id = $habitGoal->id;$habitGoalSkillMapper->insert($habitGoalSkill);}}}$this->logger->info('Se agrego el valor : ' . $habitGoal->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$habitGoal = $habitGoalMapper->fetchOne($habitGoal->id);$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/edit');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/delete');$data = ['success' => true,'message' => 'LABEL_RECORD_ADDED','data' => ['id' => $habitGoal->uuid,'name' => $habitGoal->name,'description' => $habitGoal->description,'actions' => ['link_edit' => $allowEdit ? $this->url()->fromRoute('habits/goals/edit', ['id' => $habitGoal->uuid ], ['force_canonical' => true]) : '','link_delete' => $allowDelete ? $this->url()->fromRoute('habits/goals/delete', ['id' => $habitGoal->uuid ], ['force_canonical' => true]) : '',]]];} else {$data = ['success' => false,'data' => $habitGoalMapper->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']);}$habitGoalMapper = HabitGoalMapper::getInstance($this->adapter);$habitGoal = $habitGoalMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);if(!$habitGoal) {return new JsonModel(['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND']);}if($currentUser->id != $habitGoal->user_id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if($request->isGet()) {$skills = [];$habitGoalSkillMapper = \LeadersLinked\Mapper\HabitGoalSkillMapper::getInstance($this->adapter);$habitSkillMapper = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);$records = $habitGoalSkillMapper->fetchAllByGoalId($habitGoal->id);foreach($records as $record){$habitSkill = $habitSkillMapper->fetchOne($record->skill_id);array_push($skills, $habitSkill->uuid);}return new JsonModel(['success' => true,'data' => ['name' => $habitGoal->name,'description' => $habitGoal->description,'value' => $habitGoal->value,'start_date' => $habitGoal->start_date,'end_date' => $habitGoal->end_date,'skill_id' => $skills]]);} else if($request->isPost()) {$form = new HabitGoalForm($this->adapter, $currentUser->id);$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if($form->isValid()) {$dataPost = (array) $form->getData();$habitGoal->name = $dataPost['name'];$habitGoal->description = $dataPost['description'];$habitGoal->value = $dataPost['value'];$habitGoal->start_date = $dataPost['start_date'];$habitGoal->end_date = $dataPost['end_date'];$habitGoalMapper = \LeadersLinked\Mapper\HabitGoalMapper::getInstance($this->adapter);$result = $habitGoalMapper->update($habitGoal);if($result) {$habitGoalSkillMapper = \LeadersLinked\Mapper\HabitGoalSkillMapper::getInstance($this->adapter);$habitGoalSkillMapper->deleteAllByGoalId($habitGoal->id);if(!empty($dataPost['skill_id'])) {$habitSkillMapper = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);foreach($dataPost['skill_id'] as $uuid){$habitSkill = $habitSkillMapper->fetchOneByUuid($uuid);if($habitSkill && $habitSkill->user_id == $currentUser->id) {$habitGoalSkill = new \LeadersLinked\Model\HabitGoalSkill();$habitGoalSkill->network_id = $currentUser->network_id;$habitGoalSkill->user_id = $currentUser->id;$habitGoalSkill->goal_id = $habitGoal->id;$habitGoalSkill->skill_id = $habitSkill->id;$habitGoalSkillMapper->insert($habitGoalSkill);}}}$this->logger->info('Se edito el valor : ' . $habitGoal->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/edit');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/delete');$data = ['success' => true,'message' => 'LABEL_RECORD_UPDATED','data' => ['id' => $habitGoal->uuid,'name' => $habitGoal->name,'description' => $habitGoal->description,'actions' => ['link_edit' => $allowEdit ? $this->url()->fromRoute('habits/goals/edit', ['id' => $habitGoal->uuid ], ['force_canonical' => true]) : '','link_delete' => $allowDelete ? $this->url()->fromRoute('habits/goals/delete', ['id' => $habitGoal->uuid ], ['force_canonical' => true]) : '',]]];} else {$data = ['success' => false,'data' => $habitGoalMapper->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']);}$habitGoalMapper = HabitGoalMapper::getInstance($this->adapter);$habitGoal = $habitGoalMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);if(!$habitGoal) {return new JsonModel(['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND']);}if($currentUser->id != $habitGoal->user_id) {$response = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($response);}if($request->isPost()) {$result = $habitGoalMapper->delete($habitGoal);if($result) {$this->logger->info('Se borro el valor : ' . $habitGoal->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);return new JsonModel(['success' => true,'data' => 'LABEL_RECORD_DELETED']);} else {return new JsonModel(['success' => false,'data' => $habitGoalMapper->getError()]);}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}}