Proyectos de Subversion LeadersLinked - Backend

Rev

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