Rev 1114 | 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\BehaviorsMapper;use LeadersLinked\Form\BehaviorsForm;use LeadersLinked\Model\Behaviors;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(){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 BehaviorsForm();$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if($form->isValid()) {$dataPost = (array) $form->getData();$hydrator = new ObjectPropertyHydrator();$behavior = new Behaviors();$hydrator->hydrate($dataPost, $behavior);if(!$behavior->status) {$behavior->status = Behaviors::STATUS_INACTIVE;}if($currentCompany) {$behavior->company_id = $currentCompany->id;}$behaviorMapper = BehaviorsMapper::getInstance($this->adapter);$result = $behaviorMapper->insert($behavior);if($result) {$this->logger->info('Se agrego la conducta observable ' . $behavior->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$data = ['success' => true,'data' => 'LABEL_RECORD_ADDED'];} 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 {$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();$request = $this->getRequest();$id = $this->params()->fromRoute('id');if(!$id) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$behaviorMapper = BehaviorsMapper::getInstance($this->adapter);$behavior = $behaviorMapper->fetchOneByUuid($id);if(!$behavior) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if($request->isPost()) {$form = new BehaviorsForm();$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if($form->isValid()) {$dataPost = (array) $form->getData();$hydrator = new ObjectPropertyHydrator();$hydrator->hydrate($dataPost, $behavior);if(!$behavior->status) {$behavior->status = Behaviors::STATUS_INACTIVE;}$result = $behaviorMapper->update($behavior);if($result) {$this->logger->info('Se actualizo la conducta observable ' . $behavior->name, ['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' => $hydrator->extract($behavior)];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();$request = $this->getRequest();$id = $this->params()->fromRoute('id');if(!$id) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$behaviorMapper = BehaviorsMapper::getInstance($this->adapter);$behavior = $behaviorMapper->fetchOneByUuid($id);if(!$behavior) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if($request->isPost()) {$result = $behaviorMapper->delete($behavior);if($result) {$this->logger->info('Se borro el tipo de competencia ' . $behavior->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$data = ['success' => true,'data' => 'LABEL_RECORD_DELETED'];} else {$data = ['success' => false,'data' => $behaviorMapper->getError()];return new JsonModel($data);}} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}}