PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/module/DeviceManager/src/DeviceManager/Controller/DeviceTypeController.php

https://gitlab.com/jeann2015/nexus
PHP | 347 lines | 255 code | 80 blank | 12 comment | 36 complexity | d154b61680dc4af83114fb9dee9aab23 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace DeviceManager\Controller;
  10. use Zend\View\Model\ViewModel;
  11. use Zend\View\Model\JsonModel;
  12. use DeviceManager\Model\DeviceType;
  13. use DeviceManager\Form\DeviceTypeForm;
  14. use Generic\Controller\GenericController;
  15. use DeviceManager\Dao\IniTemplateDao;
  16. use DeviceManager\Dao\FirmwareDao;
  17. use Provisioning\Service\OCISoapService;
  18. use Provisioning\Service\XmlService;
  19. class DeviceTypeController extends GenericController
  20. {
  21. public $dao;
  22. public function indexAction()
  23. {
  24. if (! $this->getServiceLocator()
  25. ->get('AuthService')->hasIdentity()){
  26. return $this->redirect()->toRoute('auth');
  27. }
  28. return new ViewModel();
  29. }
  30. public function listAction()
  31. {
  32. if (! $this->getServiceLocator()
  33. ->get('AuthService')->hasIdentity()){
  34. return $this->redirect()->toRoute('auth');
  35. }
  36. return new ViewModel();
  37. }
  38. public function addAction()
  39. {
  40. $request = $this->getRequest();
  41. if ($request->isPost()) {
  42. $deviceType = new DeviceType();
  43. $form = new DeviceTypeForm();
  44. $form->setInputFilter($deviceType->getInputFilter());
  45. $form->setData($request->getPost());
  46. if ($form->isValid()) {
  47. $deviceType->exchangeArray($form->getData());
  48. $this->getDao()->save($deviceType);
  49. return $this->redirect()->toRoute('devicetype');
  50. }
  51. }
  52. return new ViewModel();
  53. }
  54. public function editAction()
  55. {
  56. $id = $this->params()->fromRoute('id');
  57. $devicetype = $this->getDao()->get($id);
  58. $iniTemplateTableGateway = $this->getServiceLocator()->get('IniTemplateTableGateway');
  59. $iniTemplateDao = new IniTemplateDao($iniTemplateTableGateway);
  60. $iniTemplateFetch = $iniTemplateDao->fetchAll();
  61. $iniTemplateArray = array(""=>"");
  62. foreach ($iniTemplateFetch as $iniTemplate) {
  63. $iniTemplateArray[$iniTemplate->id] = $iniTemplate->name;
  64. }
  65. $form = new DeviceTypeForm();
  66. $form->get('initemplate_id')->setValueOptions($iniTemplateArray);
  67. $form->get('initemplate_id')->setValue($devicetype->initemplateId);
  68. $firmwareTableGateway = $this->getServiceLocator()->get('FirmwareTableGateway');
  69. $firmwareDao = new FirmwareDao($firmwareTableGateway);
  70. $firmwareFetch = $firmwareDao->fetchAll();
  71. $firmwareArray = array(""=>"");
  72. foreach ($firmwareFetch as $firmware) {
  73. $firmwareArray[$firmware->id] = $firmware->name;
  74. }
  75. $form->get('firmware_id')->setValueOptions($firmwareArray);
  76. $form->get('firmware_id')->setValue($devicetype->firmwareId);
  77. $request = $this->getRequest();
  78. if ($request->isPost()) {
  79. $devicetype = new DeviceType();
  80. $inputFilter = $devicetype->getInputFilter();
  81. $inputFilter->add(array(
  82. 'name' => 'firmware_id',
  83. 'required' => false,
  84. 'allow_empty' => true,
  85. ));
  86. $inputFilter->add(array(
  87. 'name' => 'initemplate_id',
  88. 'required' => false,
  89. 'allow_empty' => true,
  90. ));
  91. $form->setInputFilter($inputFilter);
  92. $form->setData($request->getPost());
  93. if ($form->isValid()) {
  94. $devicetype->exchangeArray($form->getData());
  95. $devicetype->firmware_id = $devicetype->firmware_id == "" ? null : $devicetype->firmware_id;
  96. $devicetype->initemplate_id = $devicetype->initemplate_id == "" ? null : $devicetype->initemplate_id;
  97. $this->getDao()->save($devicetype);
  98. return $this->redirect()->toRoute('devicetype');
  99. } else {
  100. $err = $form->getMessages();
  101. print_r ($err);
  102. }
  103. } else {
  104. $form->setData($devicetype->getArrayCopy());
  105. }
  106. return new ViewModel(array(
  107. 'form' => $form,
  108. 'devicetype' => $devicetype,
  109. ));
  110. }
  111. public function deleteAction()
  112. {
  113. $model = new JsonModel();
  114. try {
  115. $id = $this->params()->fromRoute('id');
  116. $this->getDao()->delete($id);
  117. $model->setVariable('status', "success");
  118. $model->setVariable('payload', $id);
  119. } catch (\Exception $ex) {
  120. $model->setVariable('status', 'error');
  121. $model->setVariable('payload', $ex->getMessage());
  122. }
  123. return $model;
  124. }
  125. public function accessAction()
  126. {
  127. $id = $this->params()->fromRoute('id');
  128. $config = $this->getServiceLocator()->get('config');
  129. $routes = array_keys($config['router']['routes']);
  130. $exclude = array('root','application','auth','home');
  131. foreach ($exclude as $ex)
  132. {
  133. if(($key = array_search($ex, $routes)) !== false) {
  134. unset($routes[$key]);
  135. }
  136. }
  137. $request = $this->getRequest();
  138. if ($request->isPost()) {
  139. $post = $request->getPost();
  140. var_dump($post);
  141. }
  142. return new ViewModel(array(
  143. 'routes' => $routes,
  144. 'id' => $id,
  145. ));
  146. }
  147. public function getDao($daoName = "DeviceTypeDao",$namespace = "DeviceManager")
  148. {
  149. $dao = $namespace . "\Dao\\" . $daoName;
  150. if (!is_a($this->dao,$dao) ) {
  151. $is = 1;
  152. $sm = $this->getServiceLocator();
  153. $this->dao = $sm->get($dao);
  154. }
  155. return $this->dao;
  156. }
  157. public function syncAction()
  158. {
  159. $model = new JsonModel();
  160. try {
  161. $xmltemplate = $this->getDao("XmlTemplateDao","Provisioning")->getByCategoryLikeCommand('list','SystemAccessDeviceTypeGetList');
  162. if($xmltemplate) {
  163. $xml = $xmltemplate->getXml();
  164. $variableDao = $this->getDao("VariableDao","Provisioning");
  165. $variables = $variableDao->fetchByXmlTemplateId($xmltemplate->getId());
  166. //$array = $request->getPost();
  167. $array = array();
  168. $xmlService = new XmlService();
  169. $xmlGenerated = $xmlService->mergeArray($xml,$variables,$array);
  170. $OCISoapService = new OCISoapService($this->getServiceLocator());
  171. $response = $OCISoapService->send($xmlGenerated);
  172. $xmlresponse = $xmlService->getCommnadResponseFromXml($response);
  173. if($xmlresponse->result == "success"){
  174. $devices = $this->getDao()->fetchAll();
  175. foreach ($devices as $device) {
  176. $device->setInbroadsoft(0);
  177. unset($device->ini);
  178. unset($device->firmware);
  179. $this->getDao()->save($device);
  180. }
  181. foreach ((array)$xmlresponse->command->deviceType as $deviceType) {
  182. $modelo = $this->getDao()->getByType($deviceType);
  183. if(isset($modelo)){
  184. $modelo->setInbroadsoft(1);
  185. } else {
  186. $modelo = new DeviceType();
  187. $modelo->setType($deviceType);
  188. $modelo->setInbroadsoft(1);
  189. }
  190. $this->getDao()->save($modelo);
  191. }
  192. $model->setVariable('status', "success");
  193. $model->setVariable('payload', $summary);
  194. } else {
  195. $summary = $xmlresponse->summary;
  196. $model->setVariable('status', "error");
  197. $model->setVariable('payload', $summary);
  198. }
  199. } else {
  200. $summary = "[Error] No existe comando xml de consulta.";
  201. $model->setVariable('status', "error");
  202. $model->setVariable('payload', $summary);
  203. }
  204. } catch (\Exception $ex) {
  205. $model->setVariable('status', 'error');
  206. $model->setVariable('payload', $ex->getMessage());
  207. }
  208. return $model;
  209. }
  210. public function filterAction(){
  211. $model = new JsonModel();
  212. $request = $this->getRequest();
  213. if($request->isPost()){
  214. $post = (array)$request->getPost();
  215. $limit = isset( $post['limit'] ) ? $post['limit'] : null;
  216. $offset = $post['offset'];
  217. $order = $post['order'];
  218. $sort = $post['sort'];
  219. $filters = array_key_exists('filters',$post) ? $post['filters'] : null;
  220. if($filters)
  221. foreach ($filters as $key => $value) {
  222. if (strpos($key,'QGDATE') !== false) {
  223. if(stripos($value,":") !== false){
  224. $filters[$key] = $this->convertToUTC($value);
  225. } else {
  226. $filters[$key] = $this->convertToUTC($value,array('date'=>true));
  227. }
  228. //print $filters[$key] . "\n";
  229. }
  230. }
  231. $result = $this->getDao()->fetchFilter($limit,$offset,$order,$sort,$filters);
  232. }
  233. $items = array();
  234. foreach ($result['resultSet'] as $item) {
  235. $item->options = "";
  236. foreach ($item as $key => $value) {
  237. //Aqui se hace alguna transformaciè´¸n que los valores del grid requieran
  238. //$value = $this->translator($value);
  239. //$item->$key = $value;
  240. if($this->isDate($value)){
  241. if(stripos($value,":") === false){
  242. $item->$key = $this->convertFromUTC($value,array('date'=>true));
  243. } else {
  244. $item->$key = $this->convertFromUTC($value);
  245. }
  246. }
  247. if($key == "options"){
  248. $url = $this->url()->fromRoute('devicetype',array('action'=>'edit','id'=>$item->id));
  249. $item->$key = "<a href='".$url."'><i class='fa fa-edit'></i> Editar</a>";
  250. }
  251. if($key == "inbroadsoft"){
  252. $item->$key = $value == 1 ? "<i class='fa fa-check text-success'></i>" : "<i class='fa fa-close text-danger'></i>";
  253. }
  254. if($key == "active"){
  255. $item->$key = $value == 1 ? "<i class='fa fa-check text-success'></i>" : "";
  256. }
  257. }
  258. array_push($items, $item);
  259. }
  260. $model->setVariable("status","success");
  261. $model->setVariable("payload",array(
  262. "total" => $result['total'],
  263. "items" => $items,
  264. ));
  265. return $model;
  266. }
  267. }