Rev 15444 | Rev 16768 | Ir a la última revisión | 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\Cache\Storage\Adapter\AbstractAdapter;use Laminas\Mvc\Controller\AbstractActionController;use Laminas\Log\LoggerInterface;use Laminas\View\Model\ViewModel;use Laminas\View\Model\JsonModel;use LeadersLinked\Library\Functions;use LeadersLinked\Mapper\BehaviorMapper;use LeadersLinked\Form\Behavior\BehaviorForm;use LeadersLinked\Model\Behavior;use LeadersLinked\Hydrator\ObjectPropertyHydrator;class BehaviorsController extends AbstractActionController {/**** @var AdapterInterface*/private $adapter;/**** @var AbstractAdapter*/private $cache;/**** @var LoggerInterface*/private $logger;/**** @var array*/private $config;/**** @param AdapterInterface $adapter* @param AbstractAdapter $cache* @param LoggerInterface $logger* @param array $config*/public function __construct($adapter, $cache, $logger, $config) {$this->adapter = $adapter;$this->cache = $cache;$this->logger = $logger;$this->config = $config;}public function indexAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();$request = $this->getRequest();$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');$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/behaviors/edit');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/behaviors/delete');$search = $this->params()->fromQuery('search');$search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);$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(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));$fields = ['description'];$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'description';if(!in_array($order_direction, ['ASC', 'DESC'])) {$order_direction = 'ASC';}$behaviorMapper = BehaviorMapper::getInstance($this->adapter);if($currentCompany) {$paginator = $behaviorMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);} else {$paginator = $behaviorMapper->fetchAllDataTableDefault($search, $page, $records_x_page, $order_field, $order_direction);}$items = [];$records = $paginator->getCurrentItems();foreach($records as $record){$item = ['description' => $record->description,'status' => $record->status,'actions' => ['link_edit' => $allowEdit ? $this->url()->fromRoute('jobs-description/behaviors/edit', ['id' => $record->uuid ]) : '','link_delete' => $allowDelete ? $this->url()->fromRoute('jobs-description/behaviors/delete', ['id' => $record->uuid ]) : '',],];array_push($items, $item);}return new JsonModel(['success' => true,'data' => ['items' => $items,'total' => $paginator->getTotalItemCount(),]]);} else {$form = new BehaviorForm();$this->layout()->setTemplate('layout/layout-backend');$viewModel = new ViewModel();$viewModel->setTemplate('leaders-linked/behaviors/index.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()) {$inline = $this->params()->fromRoute('inline');$inline = $inline == 'yes' ? true : false;$form = new BehaviorForm();$dataPost = $request->getPost()->toArray();if($inline) {$dataPost['status'] = Behavior::STATUS_ACTIVE;}$form->setData($dataPost);if ($form->isValid()) {$dataPost = (array) $form->getData();$hydrator = new ObjectPropertyHydrator();$behavior = new Behavior();$hydrator->hydrate($dataPost, $behavior);if(empty($dataPost['status'])) {$behavior->status = Behavior::STATUS_INACTIVE;}if ($currentCompany) {$behavior->company_id = $currentCompany->id;}$behaviorMapper = BehaviorMapper::getInstance($this->adapter);$result = $behaviorMapper->insert($behavior);if ($result) {if($inline) {$behavior = $behaviorMapper->fetchOne($behavior->id);$response = ['success' => true,'data' => ['message' => 'LABEL_RECORD_ADDED','behavior' => ['uuid' => $behavior->uuid,'description' => $behavior->description,]]];} else {$response = ['success' => true,'data' => 'LABEL_RECORD_ADDED',];}$this->logger->info('Se agrego la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);} else {$response = ['success' => false,'data' => $behaviorMapper->getError()];}return new JsonModel($response);} 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 {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}public function editAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();$request = $this->getRequest();$id = $this->params()->fromRoute('id');if (!$id) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$behaviorMapper = BehaviorMapper::getInstance($this->adapter);$behavior = $behaviorMapper->fetchOneByUuid($id);if (!$behavior) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if($currentCompany) {if($behavior->company_id != $currentCompany->id) {$response = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($response);}} else {if($behavior->company_id) {$response = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($response);}}if ($request->isPost()) {$form = new BehaviorForm();$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if ($form->isValid()) {$dataPost = (array) $form->getData();$hydrator = new ObjectPropertyHydrator();$hydrator->hydrate($dataPost, $behavior);if(empty($dataPost['status'])) {$behavior->status = Behavior::STATUS_INACTIVE;}$result = $behaviorMapper->update($behavior);if ($result) {$this->logger->info('Se actualizo la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$data = ['success' => true,'data' => 'LABEL_RECORD_UPDATED'];} else {$data = ['success' => false,'data' => $behaviorMapper->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 if ($request->isGet()) {$hydrator = new ObjectPropertyHydrator();$data = ['success' => true,'data' => ['description' => $behavior->description,'status' => $behavior->status,]];return new JsonModel($data);} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}public function deleteAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();$request = $this->getRequest();if ($request->isPost()) {$id = $this->params()->fromRoute('id');if (!$id) {$response = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($response);}$behaviorMapper = BehaviorMapper::getInstance($this->adapter);$behavior = $behaviorMapper->fetchOneByUuid($id);if (!$behavior) {$response = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($response);}if($currentCompany) {if($behavior->company_id != $currentCompany->id) {$response = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($response);}} else {if($behavior->company_id) {$response = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($response);}}$result = $behaviorMapper->delete($behavior->id);if ($result) {$this->logger->info('Se borro la conducta observable : ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$response = ['success' => true,'data' => 'LABEL_RECORD_DELETED'];} else {$response = ['success' => false,'data' => $behaviorMapper->getError()];}} else {$response = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];}return new JsonModel($response);}public function importAction() {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();if (!$currentCompany) {$data = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($data);}$request = $this->getRequest();if ($request->isPost()) {$behaviorMapper =BehaviorMapper::getInstance($this->adapter);$behaviorsDefault = $behaviorMapper->fetchAllByDefault();$new_records = 0;foreach ($behaviorsDefault as $behaviorDefault) {$behavior = $behaviorMapper->fetchOneByCompanyIdAndIdDefault($currentCompany->id, $behaviorDefault->id);if (!$behavior) {$behavior = new Behavior();$behavior->behavior_id_default = $behaviorDefault->id;$behavior->company_id = $currentCompany->id;$behavior->description = $behaviorDefault->description;$behavior->status = $behaviorDefault->status;if ($behaviorMapper->insert($behavior)) {$new_records++;} else {$data = ['success' => false,'data' => 'ERROR_CANT_ADD_BEHAVIOR'];return new JsonModel($data);}}}if ($new_records) {if (1 == $new_records) {$data = ['success' => true,'data' => 'LABEL_1_BEHAVIOR_IMPORTED'];return new JsonModel($data);} else {$data = ['success' => true,'data' => $new_records . ' LABEL_MULTI_BEHAVIORS_IMPORTED'];return new JsonModel($data);}} else {$data = ['success' => true,'data' => 'LABEL_NO_BEHAVIOR_IMPORTED'];return new JsonModel($data);}} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}}