PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/module/Provisioning/src/Provisioning/Controller/CommandController.php

https://gitlab.com/jeann2015/nexus
PHP | 255 lines | 200 code | 47 blank | 8 comment | 20 complexity | e9576ff64d701cc50f746eeb2537a05c 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 Provisioning\Controller;
  10. use Zend\View\Model\ViewModel;
  11. use Zend\View\Model\JsonModel;
  12. use Provisioning\Model\Command;
  13. use Provisioning\Model\Variable;
  14. use Provisioning\Model\Customer;
  15. use Provisioning\Model\Group;
  16. use Provisioning\Model\Order;
  17. use Provisioning\Model\Product;
  18. use Provisioning\Model\Device;
  19. use Provisioning\Model\User;
  20. use Provisioning\Model\Domain;
  21. use Provisioning\Form\CommandForm;
  22. use Preference\Dao\PreferenceDao;
  23. use Generic\Controller\GenericController;
  24. class CommandController extends GenericController
  25. {
  26. public $serviceDao;
  27. public function indexAction()
  28. {
  29. if (! $this->getServiceLocator()
  30. ->get('AuthService')->hasIdentity()){
  31. return $this->redirect()->toRoute('auth');
  32. }
  33. return new ViewModel();
  34. }
  35. public function listAction()
  36. {
  37. if (! $this->getServiceLocator()
  38. ->get('AuthService')->hasIdentity()){
  39. return $this->redirect()->toRoute('auth');
  40. }
  41. $commands = $this->getDao()->fetchAll();
  42. return new ViewModel(array('commands' => $commands));
  43. }
  44. public function addAction()
  45. {
  46. $request = $this->getRequest();
  47. if ($request->isPost()) {
  48. $command = new Command();
  49. $form = new CommandForm();
  50. $form->setInputFilter($command->getInputFilter());
  51. $form->setData($request->getPost());
  52. if ($form->isValid()) {
  53. $command->exchangeArray($form->getData());
  54. $id = $this->getDao()->save($command);
  55. $xml = $command->xml;
  56. $xml = str_replace("\n", '', $xml);
  57. $xml = str_replace("\t", '', $xml);
  58. if(preg_match_all('/%[A-Z_-]*%/mi', $xml, $variablesInXml) === 0)
  59. {
  60. $this->getDao("VariableDao")->deleteByCommandId($id);
  61. return $this->redirect()->toRoute('command',array('action'=>'list'));
  62. } else {
  63. if(!is_array($variablesInXml)) { $variablesInXml[0] = $variablesInXml; }
  64. foreach ($variablesInXml[0] as $variableInXml){
  65. $variable = $this->getDao("VariableDao")->getByCommandIdAndName($id,$variableInXml);
  66. if($variable === null){
  67. $variable = new Variable();
  68. $variable->setCommandId($id);
  69. $variable->setName($variableInXml);
  70. $this->getDao("VariableDao")->save($variable);
  71. }
  72. }
  73. return $this->redirect()->toRoute('command',array("action"=>"variable","id"=>$id));
  74. }
  75. }
  76. }
  77. return new ViewModel();
  78. }
  79. public function editAction()
  80. {
  81. $id = $this->params()->fromRoute('id');
  82. $command = $this->getDao()->get($id);
  83. $form = new CommandForm();
  84. $request = $this->getRequest();
  85. if ($request->isPost()) {
  86. $command = new Command();
  87. $form->setInputFilter($command->getInputFilter());
  88. $form->setData($request->getPost());
  89. if ($form->isValid()) {
  90. $command->exchangeArray($form->getData());
  91. $id = $this->getDao()->save($command);
  92. $xml = $command->xml;
  93. $xml = str_replace("\n", '', $xml);
  94. $xml = str_replace("\t", '', $xml);
  95. if(preg_match_all('/%[A-Z_-]*%/mi', $xml, $variablesInXml) === 0)
  96. {
  97. $this->getDao("VariableDao")->deleteByCommandId($id);
  98. return $this->redirect()->toRoute('command',array('action'=>'list'));
  99. } else {
  100. if(!is_array($variablesInXml)) { $variablesInXml[0] = $variablesInXml; }
  101. foreach ($variablesInXml[0] as $variableInXml){
  102. $variable = $this->getDao("VariableDao")->getByCommandIdAndName($id,$variableInXml);
  103. if($variable === null){
  104. $variable = new Variable();
  105. $variable->setCommandId($id);
  106. $variable->setName($variableInXml);
  107. $this->getDao("VariableDao")->save($variable);
  108. }
  109. }
  110. return $this->redirect()->toRoute('command',array("action"=>"variable","id"=>$id));
  111. }
  112. }
  113. } else {
  114. $form->setData($command->getArrayCopy());
  115. }
  116. return new ViewModel(array(
  117. 'form' => $form,
  118. 'command' => $command,
  119. ));
  120. }
  121. public function variableAction()
  122. {
  123. $id = $this->params()->fromRoute('id');
  124. $request = $this->getRequest();
  125. if ($request->isPost()) {
  126. foreach($request->getPost() as $key => $value){
  127. $pos = strpos($key, "id");
  128. if($pos === 0){
  129. $variable = new Variable();
  130. $variable->setId($value);
  131. $variable->setXmltemplateId($id);
  132. $variable->setName($request->getPost("name".$value));
  133. $variable->setMappingObject($request->getPost("mappingObject".$value));
  134. $variable->setMappingAttribute($request->getPost("mappingAttribute".$value));
  135. $variable->setTreatment($request->getPost("treatment".$value));
  136. $this->getDao("VariableDao")->save($variable);
  137. } else {
  138. $pos = strpos($key, "delete");
  139. if($pos === 0){
  140. $this->getDao("VariableDao")->delete($value);
  141. }
  142. }
  143. }
  144. return $this->redirect()->toRoute('command',array('action'=>'list'));
  145. }
  146. $command = $this->getDao()->get($id);
  147. $variables = $this->getDao("VariableDao")->fetchByCommandId($id);
  148. $xml = $command->xml;
  149. $xml = str_replace("\n", '', $xml);
  150. $xml = str_replace("\t", '', $xml);
  151. preg_match_all('/%[A-Z_-]*%/mi', $xml, $variablesInXml);
  152. if(!is_array($variablesInXml)) { $variablesInXml[0] = $variablesInXml; }
  153. $preferenceFetch = $this->getDao("PreferenceDao","Preference")->fetchByCategory("provisioning");
  154. $preferences = array();
  155. foreach ($preferenceFetch as $p) {
  156. array_push($preferences, $p->name);
  157. }
  158. return new ViewModel(array(
  159. 'id' => $id,
  160. 'variables' => $variables,
  161. 'variablesInXml' => $variablesInXml[0],
  162. 'objects' => array(
  163. "Order" =>"Orden",
  164. "Customer" =>"Cliente",
  165. "Group" =>"Grupo",
  166. "Product" =>"Producto",
  167. "Preference"=>"Preferencias",
  168. "Device" =>"Dispositivo",
  169. "User" =>"Usuario",
  170. "Domain" =>"Dominio",
  171. ),
  172. 'attributes' => array(
  173. "Customer" => array_keys( get_object_vars ( new Customer() ) ),
  174. "Group" => array_keys( get_object_vars ( new Group() ) ),
  175. "Order" => array_keys( get_object_vars ( new Order() ) ),
  176. "Product" => array_keys( get_object_vars ( new Product() ) ),
  177. "Device" => array_keys( get_object_vars ( new Device() ) ),
  178. "User" => array_keys( get_object_vars ( new User() ) ),
  179. "Domain" => array_keys( get_object_vars ( new Domain() ) ),
  180. "Preference"=> $preferences,
  181. ),
  182. ));
  183. }
  184. public function deleteAction()
  185. {
  186. $model = new JsonModel();
  187. try {
  188. $id = $this->params()->fromRoute('id');
  189. $this->getDao()->delete($id);
  190. $model->setVariable('status', "success");
  191. $model->setVariable('payload', $id);
  192. } catch (\Exception $ex) {
  193. $model->setVariable('status', 'error');
  194. $model->setVariable('payload', $ex->getMessage());
  195. }
  196. return $model;
  197. }
  198. public function getDao($dao = "CommandDao",$namespace = "Provisioning")
  199. {
  200. //if (!$this->serviceDao) {
  201. $sm = $this->getServiceLocator();
  202. $this->serviceDao = $sm->get($namespace.'\Dao\\'.$dao);
  203. //}
  204. return $this->serviceDao;
  205. }
  206. }