Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1 | Rev 16768 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16766 efrain 7
use LeadersLinked\Cache\CacheInterface;
1 www 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Mapper\LogMapper;
14
use LeadersLinked\Model\Log;
15
use LeadersLinked\Mapper\DeviceMapper;
16
 
17
class DeviceController extends AbstractActionController
18
{
19
    /**
20
     *
21
     * @var AdapterInterface
22
     */
23
    private $adapter;
24
 
25
 
26
    /**
27
     *
16766 efrain 28
     * @var CacheInterface
1 www 29
     */
30
    private $cache;
31
 
32
    /**
33
     *
34
     * @var  LoggerInterface
35
     */
36
    private $logger;
37
 
38
    /**
39
     *
40
     * @var array
41
     */
42
    private $config;
43
 
44
 
45
 
46
    /**
47
     *
48
     * @param AdapterInterface $adapter
16766 efrain 49
     * @param CacheInterface $cache
1 www 50
     * @param LoggerInterface $logger
51
     * @param array $config
52
     */
53
    public function __construct($adapter, $cache , $logger, $config)
54
    {
55
        $this->adapter      = $adapter;
56
        $this->cache        = $cache;
57
        $this->logger       = $logger;
58
        $this->config       = $config;
59
 
60
    }
61
 
62
    public function indexAction()
63
    {
64
        $request = $this->getRequest();
65
 
66
        if($request->isPost())
67
        {
68
            $search = $this->params()->fromQuery('search', []);
16766 efrain 69
            $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 70
 
71
            $page               = intval($this->params()->fromQuery('start', 1), 10);
72
            $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
73
            $order =  $this->params()->fromQuery('order', []);
74
            $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 75
            $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 76
 
77
            $fields =  ['device_id', 'device', 'model', 'product', 'osversion', 'user', 'email'];
78
            $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'device_id';
79
 
80
            if(!in_array($order_direction, ['ASC', 'DESC'])) {
81
                $order_direction = 'ASC';
82
            }
83
 
84
 
85
 
86
 
87
            $deviceMapper = DeviceMapper::getInstance($this->adapter);
88
            $paginator = $deviceMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction);
89
 
90
 
91
            $items = [];
92
            $records = $paginator->getCurrentItems();
93
            foreach($records as $record)
94
            {
95
 
96
                $item = [
97
                    'user' => $record->user,
98
                    'email' => $record->email,
99
                    'device_id' => $record->device_id,
100
                    'device' => $record->device,
101
                    'model' => $record->model,
102
                    'product' => $record->product,
103
                    'osversion' => $record->osversion,
104
                    'updated_on' => $record->updated_on,
105
                    'push'  => $record->token ? true : false,
106
 
107
                ];
108
 
109
                array_push($items, $item);
110
            }
111
            return new JsonModel([
112
                'success' => true,
113
                'data' => [
114
                    'items' => $items,
115
                    'total' => $paginator->getTotalItemCount(),
116
                ]
117
            ]);
118
 
119
 
120
        }
121
        else if($request->isGet()) {
122
 
123
            $this->layout()->setTemplate('layout/layout.phtml');
124
            $viewModel = new ViewModel();
125
            $viewModel->setTemplate('leaders-linked/devices/index.phtml');
126
            return $viewModel ;
127
 
128
        } else {
129
            return new JsonModel([
130
                'success' => false,
131
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
132
            ]);;
133
        }
134
    }
135
 
136
 
137
}