PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/module/Provisioning/src/Provisioning/Controller/XmlTemplateController.php

https://gitlab.com/jeann2015/nexus
PHP | 346 lines | 268 code | 69 blank | 9 comment | 21 complexity | cb585ce601c0e16bf14d1d016607fd6f 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\XmlTemplate;
  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\Model\DomainAdd;
  22. use Provisioning\Model\ServiceProviderEmpresaAdd;
  23. use Provisioning\Model\UserModifyAdd;
  24. use Provisioning\Model\ProviderAdd;
  25. use Provisioning\Model\Provider;
  26. use Provisioning\Model\Dn;
  27. use Provisioning\Model\Services;
  28. use Provisioning\Model\GroupServiceProvider;
  29. use Provisioning\Model\AdditionLine;
  30. use Provisioning\Model\ChangeNumber;
  31. use Provisioning\Model\DeleteLine;
  32. use Provisioning\Model\CreationGroup;
  33. use Provisioning\Model\DeleteGroup;
  34. use Provisioning\Form\XmlTemplateForm;
  35. use Preference\Dao\PreferenceDao;
  36. use Generic\Controller\GenericController;
  37. class XmlTemplateController extends GenericController
  38. {
  39. public $serviceDao;
  40. public function indexAction()
  41. {
  42. if (! $this->getServiceLocator()
  43. ->get('AuthService')->hasIdentity()){
  44. return $this->redirect()->toRoute('auth');
  45. }
  46. return new ViewModel();
  47. }
  48. public function listAction()
  49. {
  50. if (! $this->getServiceLocator()
  51. ->get('AuthService')->hasIdentity()){
  52. return $this->redirect()->toRoute('auth');
  53. }
  54. $xmltemplates = $this->getDao()->fetchAll();
  55. return new ViewModel(array('xmltemplates' => $xmltemplates));
  56. }
  57. public function addAction()
  58. {
  59. $form = new XmlTemplateForm();
  60. $addxmltemplatesSelect = $form->get("addxmltemplateId");
  61. $addxmltemplates = $this->getDao()->fetchByCategory('add');
  62. $addxmltemplatesArray = array(null => "");
  63. foreach ($addxmltemplates as $addxmltemplate) {
  64. $addxmltemplatesArray[$addxmltemplate->id] = $addxmltemplate->name . " - " . $addxmltemplate->command;
  65. }
  66. $addxmltemplatesSelect->setAttribute('options',$addxmltemplatesArray);
  67. $request = $this->getRequest();
  68. if ($request->isPost()) {
  69. $xmltemplate = new XmlTemplate();
  70. $xmltemplate->unsetInputFilter();
  71. $form->setInputFilter($xmltemplate->getInputFilter());
  72. $form->setData($request->getPost());
  73. if ($form->isValid()) {
  74. $xmltemplate->exchangeArray($form->getData());
  75. $id = $this->getDao()->save($xmltemplate);
  76. $xml = $xmltemplate->xml;
  77. $xml = str_replace("\n", '', $xml);
  78. $xml = str_replace("\t", '', $xml);
  79. if(preg_match_all('/%[A-Z_-]*%/mi', $xml, $variablesInXml) === 0)
  80. {
  81. $this->getDao("VariableDao")->deleteByXmlTemplateId($id);
  82. return $this->redirect()->toRoute('xmltemplate',array('action'=>'list'));
  83. } else {
  84. if(!is_array($variablesInXml)) { $variablesInXml[0] = $variablesInXml; }
  85. foreach ($variablesInXml[0] as $variableInXml){
  86. $variable = $this->getDao("VariableDao")->getByXmlTemplateIdAndName($id,$variableInXml);
  87. if($variable === null){
  88. $variable = new Variable();
  89. $variable->setXmlTemplateId($id);
  90. $variable->setName($variableInXml);
  91. $this->getDao("VariableDao")->save($variable);
  92. }
  93. }
  94. return $this->redirect()->toRoute('xmltemplate',array("action"=>"variable","id"=>$id));
  95. }
  96. } else {
  97. var_dump($form->getMessages());
  98. }
  99. }
  100. //exit;
  101. return new ViewModel(array(
  102. 'form' => $form,
  103. ));
  104. }
  105. public function editAction()
  106. {
  107. $id = $this->params()->fromRoute('id');
  108. $xmltemplate = $this->getDao()->get($id);
  109. $form = new XmlTemplateForm();
  110. $addxmltemplatesSelect = $form->get("addxmltemplateId");
  111. $addxmltemplates = $this->getDao()->fetchByCategory('add');
  112. $addxmltemplatesArray = array(0 => "");
  113. foreach ($addxmltemplates as $addxmltemplate) {
  114. $addxmltemplatesArray[$addxmltemplate->id] = $addxmltemplate->name . " - " . $addxmltemplate->command;
  115. }
  116. $addxmltemplatesSelect->setAttribute('options',$addxmltemplatesArray);
  117. $request = $this->getRequest();
  118. if ($request->isPost()) {
  119. $xmltemplate = new XmlTemplate();
  120. $form->setInputFilter($xmltemplate->getInputFilter());
  121. $form->setData($request->getPost());
  122. if ($form->isValid()) {
  123. $xmltemplate->exchangeArray($form->getData());
  124. $id = $this->getDao()->save($xmltemplate);
  125. $xml = $xmltemplate->xml;
  126. $xml = str_replace("\n", '', $xml);
  127. $xml = str_replace("\t", '', $xml);
  128. if(preg_match_all('/%[A-Z_-]*%/mi', $xml, $variablesInXml) === 0)
  129. {
  130. $this->getDao("VariableDao")->deleteByXmlTemplateId($id);
  131. return $this->redirect()->toRoute('xmltemplate',array('action'=>'list'));
  132. } else {
  133. if(!is_array($variablesInXml)) { $variablesInXml[0] = $variablesInXml; }
  134. foreach ($variablesInXml[0] as $variableInXml){
  135. $variable = $this->getDao("VariableDao")->getByXmlTemplateIdAndName($id,$variableInXml);
  136. if($variable === null){
  137. $variable = new Variable();
  138. $variable->setXmltemplateId($id);
  139. $variable->setName($variableInXml);
  140. $this->getDao("VariableDao")->save($variable);
  141. }
  142. }
  143. return $this->redirect()->toRoute('xmltemplate',array("action"=>"variable","id"=>$id));
  144. }
  145. }
  146. } else {
  147. $form->setData($xmltemplate->getArrayCopy());
  148. }
  149. return new ViewModel(array(
  150. 'form' => $form,
  151. 'xmltemplate' => $xmltemplate,
  152. ));
  153. }
  154. public function fetchbyTypeProcesingAction()
  155. {
  156. $id = $this->params()->fromRoute('id');
  157. $typeProcesingFetch = $this->getDao()->getTypeProcesing($id);
  158. $typeProcesing = array();
  159. foreach ($typeProcesingFetch as $typeProcesing) {
  160. $typeProce[] = $typeProcesing;
  161. }
  162. return new JsonModel($typeProce);
  163. }
  164. public function variableAction()
  165. {
  166. $id = $this->params()->fromRoute('id');
  167. $request = $this->getRequest();
  168. if ($request->isPost()) {
  169. foreach($request->getPost() as $key => $value){
  170. $pos = strpos($key, "id");
  171. if($pos === 0){
  172. $variable = new Variable();
  173. $variable->setId($value);
  174. $variable->setXmltemplateId($id);
  175. $variable->setName($request->getPost("name".$value));
  176. $variable->setMappingObject($request->getPost("mappingObject".$value));
  177. $variable->setMappingAttribute($request->getPost("mappingAttribute".$value));
  178. $variable->setTreatment($request->getPost("treatment".$value));
  179. $this->getDao("VariableDao")->save($variable);
  180. } else {
  181. $pos = strpos($key, "delete");
  182. if($pos === 0){
  183. $this->getDao("VariableDao")->delete($value);
  184. }
  185. }
  186. }
  187. return $this->redirect()->toRoute('xmltemplate',array('action'=>'list'));
  188. }
  189. $xmltemplate = $this->getDao()->get($id);
  190. $variables = $this->getDao("VariableDao")->fetchByXmlTemplateId($id);
  191. $xml = $xmltemplate->xml;
  192. $xml = str_replace("\n", '', $xml);
  193. $xml = str_replace("\t", '', $xml);
  194. preg_match_all('/%[A-Z_-]*%/mi', $xml, $variablesInXml);
  195. if(!is_array($variablesInXml)) { $variablesInXml[0] = $variablesInXml; }
  196. $preferenceFetch = $this->getDao("PreferenceDao","Preference")->fetchByCategory("provisioning");
  197. $preferences = array();
  198. foreach ($preferenceFetch as $p) {
  199. array_push($preferences, $p->name);
  200. }
  201. return new ViewModel(array(
  202. 'id' => $id,
  203. 'variables' => $variables,
  204. 'variablesInXml' => $variablesInXml[0],
  205. 'objects' => array(
  206. "Order" =>"Orden",
  207. "Customer" =>"Cliente",
  208. "Group" =>"Grupo",
  209. "Product" =>"Producto",
  210. "Preference"=>"Preferencias",
  211. "Device" =>"Dispositivo",
  212. "User" =>"Usuario",
  213. "Domain" =>"Dominio",
  214. "Provider" =>"Service Provider",
  215. "Dn" =>"NĂşmero Dn",
  216. "Services" =>"Servicios",
  217. "DomainAdd" =>"Dominios por Ordenes",
  218. "ProviderAdd" =>"Service Provider por Ordenes",
  219. "ServiceProviderEmpresaAdd" =>"Service Provider por Ordenes Empresa",
  220. "UserModifyAdd" =>"Modificacion de Usuario",
  221. "GroupServiceProvider" => "Agregar Servicios de Grupo y Usuarios a la Empresa",
  222. "AdditionLine" => "Adicionde Linea",
  223. "ChangeNumber" => "Cambio de Numero",
  224. "DeleteLine" => "Eliminacion de Linea",
  225. "CreationGroup" => "Creacion de Grupo",
  226. "DeleteGroup" => "Eliminacion de Grupo",
  227. ),
  228. 'attributes' => array(
  229. "Customer" => array_keys( get_object_vars ( new Customer() ) ),
  230. "Group" => array_keys( get_object_vars ( new Group() ) ),
  231. "Order" => array_keys( get_object_vars ( new Order() ) ),
  232. "Product" => array_keys( get_object_vars ( new Product() ) ),
  233. "Device" => array_keys( get_object_vars ( new Device() ) ),
  234. "User" => array_keys( get_object_vars ( new User() ) ),
  235. "Domain" => array_keys( get_object_vars ( new Domain() ) ),
  236. "Provider" => array_keys( get_object_vars ( new Provider() ) ),
  237. "Dn" => array_keys( get_object_vars ( new Dn() ) ),
  238. "Services" => array_keys( get_object_vars ( new Services() ) ),
  239. "DomainAdd" => array_keys( get_object_vars ( new DomainAdd() ) ),
  240. "ProviderAdd" => array_keys( get_object_vars ( new ProviderAdd() ) ),
  241. "ServiceProviderEmpresaAdd" => array_keys( get_object_vars ( new ServiceProviderEmpresaAdd() ) ),
  242. "UserModifyAdd" => array_keys( get_object_vars ( new UserModifyAdd() ) ),
  243. "GroupServiceProvider" => array_keys( get_object_vars ( new GroupServiceProvider() ) ),
  244. "AdditionLine" => array_keys( get_object_vars ( new AdditionLine() ) ),
  245. "ChangeNumber" => array_keys( get_object_vars ( new ChangeNumber() ) ),
  246. "DeleteLine" => array_keys( get_object_vars ( new DeleteLine() ) ),
  247. "CreationGroup" => array_keys( get_object_vars ( new CreationGroup() ) ),
  248. "DeleteGroup" => array_keys( get_object_vars ( new DeleteGroup() ) ),
  249. "Preference"=> $preferences
  250. ),
  251. ));
  252. }
  253. public function deleteAction()
  254. {
  255. $model = new JsonModel();
  256. try {
  257. $id = $this->params()->fromRoute('id');
  258. $this->getDao()->delete($id);
  259. $model->setVariable('status', "success");
  260. $model->setVariable('payload', $id);
  261. } catch (\Exception $ex) {
  262. $model->setVariable('status', 'error');
  263. $model->setVariable('payload', $ex->getMessage());
  264. }
  265. return $model;
  266. }
  267. public function getDao($dao = "XmlTemplateDao",$namespace = "Provisioning")
  268. {
  269. //if (!$this->serviceDao) {
  270. $sm = $this->getServiceLocator();
  271. $this->serviceDao = $sm->get($namespace.'\Dao\\'.$dao);
  272. //}
  273. return $this->serviceDao;
  274. }
  275. }