PageRenderTime 54ms CodeModel.GetById 3ms RepoModel.GetById 1ms app.codeStats 0ms

/application/modules/campaign/controllers/Campaign.php

https://gitlab.com/estratega.pe/cc
PHP | 309 lines | 293 code | 15 blank | 1 comment | 32 complexity | 172b3e378c466527bf37a83ee1719cec MD5 | raw file
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Campaign extends MX_Controller {
  4. function __construct()
  5. {
  6. parent::__construct();
  7. $this->load->model('campaign/campaignModel', 'campaign');
  8. }
  9. public function index()
  10. {
  11. acl('ind_show', null, 403);
  12. $columnas = [
  13. ['data' => 'id_campaign']
  14. ,['data' => 'des_name']
  15. ,['data' => 'ind_status']
  16. ];
  17. $show = (!acl('ind_show')) ? '' : createLink(base_url() . 'campaign/viewCampaign/\'+data+\'', 'btn-info', 'icon-eye', 'Ver');
  18. $edit = (!acl('ind_edit')) ? '' : '&nbsp;' . createLink(base_url() . 'campaign/editCampaign/\'+data+\'', 'btn-success', 'icon-edit', 'Editar');
  19. $del = (!acl('ind_del')) ? '' : '&nbsp;' . createLink(base_url() . 'campaign/deleteCampaign/\'+data+\'', 'btn-danger', 'icon-close', 'Eliminar');
  20. $data['HEADER'] = ['PAGE_TITLE_HEADER' => 'Listado de Campa&ntilde;as', 'BASE_URL' => base_url()];
  21. $data['BODY'] = $this->parser->parse('listGridAjax', ['BASE_URL' => base_url()
  22. ,'BODY_TITLE' => 'Campa&ntilde;as'
  23. ,'URL_AJAX' => base_url() . 'campaign/getCampaigns/datatables'
  24. ,'BODY_DESCRIPTION' => 'Listado de Campa&ntilde;as'
  25. ,'BODY_SUBTITLE' => ''
  26. ,'TARGETS' => count($columnas)
  27. ,'ID_TARGET' => 'id_campaign'
  28. ,'COLUMNAS' => json_encode($columnas)
  29. ,'BODY_MENU' => (!acl('ind_add')) ? '' : createLink(base_url() . 'campaign/addCampaign', 'btn-blue-alt', 'icon-plus', 'Nuevo', true)
  30. ,'LINKS_ACTIONS' => $show . $edit . $del
  31. ,'TH_TABLE' => [['LABEL' => 'ID'], ['LABEL' => 'NOMBRE'], ['LABEL' => 'ACTIVO'], ['LABEL' => 'ACCIONES']]
  32. ], TRUE);
  33. renderPage($data);
  34. }
  35. public function addCampaign() {
  36. acl('ind_add', null, 403);
  37. if( $this->input->post() ) {
  38. $values = ['des_name' => $this->input->post('input_des_name', TRUE)
  39. ,'ind_status' => 0
  40. ,'id_type_campaign' => 1
  41. ,'id_client' => $this->input->post('select_id_client')
  42. ];
  43. $newID = $this->campaign->saveCampaign($values, $this->input->post('select_id_queue', TRUE));
  44. if( is_numeric($newID) ) {
  45. redirect(base_url() . 'campaign/editCampaign/' . $newID);
  46. }
  47. }
  48. $p_select_queues = [];
  49. $queues = $this->db->get_where('gen_queue')->result();
  50. foreach($queues as $k) {
  51. $p_select_queues[$k->id_queue] = $k->des_name;
  52. }
  53. $p_select_clients = [];
  54. $clients = $this->db->get_where('cc_client')->result();
  55. foreach($clients as $c) {
  56. $p_select_clients[$c->id_client] = $c->des_name;
  57. }
  58. $data['HEADER'] = ['PAGE_TITLE_HEADER' => 'Nueva Campa&ntilde;a', 'BASE_URL' => base_url()];
  59. $data['BODY'] = $this->parser->parse('campaign/addCampaign', ['BASE_URL' => base_url()
  60. ,'BODY_TITLE' => 'Nueva Campa&ntilde;a'
  61. ,'BODY_SUBTITLE' => ''
  62. ,'BODY_MENU' => ''
  63. ,'URL_POST' => base_url() .'campaign/addCampaign/'
  64. ,'INPUT_DES_NAME' => form_input('input_des_name', '', 'class="form-control" required')
  65. ,'SELECT_ID_QUEUE' => form_dropdown('select_id_queue', $p_select_queues, '', 'class="form-control" required')
  66. ,'SELECT_ID_CLIENT' => form_dropdown('select_id_client', $p_select_clients, '', 'class="form-control" required')
  67. ,'BUTTON_SUBMIT' => createSubmitButton('Crear', 'btn-blue-alt', 'icon-save')
  68. ,'BUTTON_CANCEL' => createLink(base_url() . 'campaign', 'btn-danger', 'icon-ban', 'Cancelar', true)
  69. ], TRUE);
  70. renderPage($data);
  71. }
  72. public function editCampaign() {
  73. acl('ind_edit', null, 403);
  74. $ID = (sizeof(func_get_args()) >= 0x0001) ? func_get_arg(0): $this->uri->segment(3);
  75. $p = $this->campaign->getCampaignRow(['id_campaign' => $ID]);
  76. if( $this->input->post() ) {
  77. $values = ['des_name' => $this->input->post('input_des_name', TRUE)
  78. ,'ind_status' => intval($this->input->post('checkbox_ind_status'))
  79. ,'id_client' => $this->input->post('select_id_client')
  80. ];
  81. if( $this->campaign->updateCampaign($values, $this->input->post('select_id_queue'), $ID) === FALSE) {
  82. $this->db->trans_rollback();
  83. } else {
  84. regenerateQueues();
  85. redirect(base_url() . 'campaign/viewCampaign/' . $ID);
  86. }
  87. }
  88. $p_select_ind_activo = [];
  89. $opciones_sino = $this->config->item('OPTIONS_SINO');
  90. while ($name = current($opciones_sino)) {
  91. array_push($p_select_ind_activo, ['ID' => key($opciones_sino), 'NAME' => $name]);
  92. next($opciones_sino);
  93. }
  94. $p_select_agents = [];
  95. $ag = modules::run('agent/getAgents', 'object');
  96. foreach($ag as $k) {
  97. $p_select_agents[$k->id_agent] = $k->des_name;
  98. }
  99. $p_selected_agents = [];
  100. $sa = $this->campaign->getCampaignAgents(['id_campaign' => $ID]);
  101. foreach($sa as $k) {
  102. $p_selected_agents[] = $k->id_agent;
  103. }
  104. $p_select_queues = [];
  105. $queues = $this->db->get_where('gen_queue')->result();
  106. foreach($queues as $k) {
  107. $p_select_queues[$k->id_queue] = $k->des_name;
  108. }
  109. $p_select_clients = [];
  110. $clients = $this->db->get_where('cc_client')->result();
  111. foreach($clients as $c) {
  112. $p_select_clients[$c->id_client] = $c->des_name;
  113. }
  114. $queue_selected = $this->db->get_where('gen_queue_campaign', ['id_campaign' => $ID])->row();
  115. $data['HEADER'] = ['PAGE_TITLE_HEADER' => 'Editar Campa&ntilde;a', 'BASE_URL' => base_url()];
  116. $data['BODY'] = $this->parser->parse('campaign/editCampaign', ['BASE_URL' => base_url()
  117. ,'BODY_TITLE' => 'Editar Campa&ntilde;a'
  118. ,'BODY_SUBTITLE' => '(' . $p->des_name . ')'
  119. ,'BODY_MENU' => ''
  120. ,'URL_POST' => base_url() . 'campaign/editCampaign/' . $p->id_campaign
  121. ,'INPUT_DES_NAME' => form_input('input_des_name', $p->des_name, 'class="form-control" required')
  122. ,'CHECKBOX_IND_STATUS' => form_checkbox('checkbox_ind_status', 1, ($p->ind_status==0x0001) ? TRUE: FALSE, ' id="checkbox_ind_status" class="input-switch-alt"')
  123. ,'SELECT_AGENTS' => form_dropdown('select_agents', $p_select_agents, $p_selected_agents, ' id="select_agents" multiple="multiple"')
  124. ,'SELECT_ID_QUEUE' => form_dropdown('select_id_queue', $p_select_queues, $queue_selected->id_queue, 'class="form-control" required')
  125. ,'SELECT_ID_CLIENT' => form_dropdown('select_id_client', $p_select_clients, $p->id_campaign, 'class="form-control" required')
  126. ,'BUTTON_SUBMIT' => createSubmitButton('Actualizar', 'btn-blue-alt', 'icon-save')
  127. ,'BUTTON_CANCEL' => createLink(base_url() . 'campaign', 'btn-danger', 'icon-ban', 'Cancelar', true)
  128. ,'URL_SET_CAMPAIGN_AGENT' => base_url() . 'campaign/setCampaignAgent/json/' . $p->id_campaign
  129. ,'URL_DEL_CAMPAIGN_AGENT' => base_url() . 'campaign/deleteCampaignAgent/json/' . $p->id_campaign
  130. ], TRUE);
  131. renderPage($data);
  132. }
  133. public function viewGroup() {
  134. acl('ind_show', null, 403);
  135. $ID = (sizeof(func_get_args()) >= 0x0001) ? func_get_arg(0): $this->uri->segment(3);
  136. $ID = filter_var(intval($ID),FILTER_VALIDATE_INT) ? $ID : 0;
  137. if($ID == 0x0000 ) {
  138. header('Location: ' . base_url() . 'group');
  139. exit();
  140. }
  141. $p = $this->group->getGroupRow(['id_type_user' => $ID]);
  142. $data['HEADER'] = ['PAGE_TITLE_HEADER' => 'Ver Tipo de Usuario', 'BASE_URL' => base_url()];
  143. $data['BODY'] = $this->parser->parse('group/viewGroup', ['BASE_URL' => base_url()
  144. ,'BODY_TITLE' => 'Ver Tipo de Usuario'
  145. ,'BODY_SUBTITLE' => '(' . $p->des_name . ')'
  146. ,'BODY_MENU' => ''
  147. ,'INPUT_DES_NAME' => form_input('input_des_name', $p->des_name, 'class="form-control" readonly')
  148. ,'INPUT_DES_MACHINE_NAME' => form_input('input_des_machine_name', $p->des_machine_name, 'class="form-control" readonly')
  149. ,'BUTTON_CANCEL' => createLink(base_url() . 'group', 'btn-danger', 'icon-ban', 'Volver', true)
  150. ], TRUE);
  151. renderPage($data);
  152. }
  153. public function deleteCampaign() {
  154. acl('ind_del', null, 403);
  155. $ID = (sizeof(func_get_args()) >= 0x0001) ? func_get_arg(0): $this->uri->segment(3);
  156. $ID = filter_var(intval($ID),FILTER_VALIDATE_INT) ? $ID : 0;
  157. if($ID == 0x0000) {
  158. header('Location: ' . base_url() . 'campaign');
  159. exit();
  160. }
  161. $p = $this->campaign->getCampaignRow(['id_campaign' => $ID]);
  162. if($this->input->post()) {
  163. if( $this->input->post('input_id_campaign') == $ID) {
  164. if ($this->campaign->deleteCampaign($this->input->post('input_id_campaign')) === FALSE) {
  165. $this->db->trans_rollback();
  166. } else {
  167. redirect(base_url() . 'campaign');
  168. }
  169. }
  170. }
  171. $data['HEADER'] = ['PAGE_TITLE_HEADER' => 'Eliminar Campa&ntilde;a ' . $p->des_name, 'BASE_URL' => base_url()];
  172. $data['BODY'] = $this->parser->parse('deleteForm', ['BASE_URL' => base_url()
  173. ,'BODY_TITLE' => 'Eliminar Campa&ntilde;a - ' . $p->des_name
  174. ,'BODY_SUBTITLE' => 'No se puede deshacer la eliminaci&oacute;n'
  175. ,'BODY_MENU' => '(' . $p->id_campaign . ') ' . $p->des_name
  176. ,'BODY_DESCRIPTION' => 'Confirme eliminacion de la Campa&ntilde;a "' . $p->des_name . '"'
  177. ,'URL_POST' => base_url() . 'campaign/deleteCampaign/' . $ID
  178. ,'INPUT_DELETE_ID' => form_hidden('input_id_campaign', $ID)
  179. ,'BUTTON_SUBMIT' => createSubmitButton('Eliminar', 'btn-blue-alt', 'icon-remove')
  180. ,'BUTTON_CANCEL' => createLink(base_url() . 'campaign', 'btn-danger', 'icon-ban', 'Cancelar', true)
  181. ], TRUE);
  182. renderPage($data);
  183. }
  184. public function getCampaigns() {
  185. $format = (sizeof(func_get_args()) >= 0x0001) ? func_get_arg(0): $this->uri->segment(3);
  186. if( $this->input->post() ) {
  187. $total = 0;
  188. $start = $this->input->post('start');
  189. $limit = $this->input->post('length');
  190. $draw = $this->input->post('draw');
  191. if($this->input->post('search')['value'] != '') {
  192. $search = $this->input->post('search')['value'];
  193. $like = ['des_name' => $search];
  194. $result = $this->campaign->getCampaigns(null, $like);
  195. $total = $this->campaign->getTotalCampaign(null, $like);
  196. } else {
  197. if($this->input->post('q') != '') {
  198. $search = $this->input->post('q');
  199. $like = ['des_name' => $search];
  200. $result = $this->campaign->getCampaigns(null, $like, $start, $limit);
  201. $total = $this->campaign->getTotalCampaign(null, $like);
  202. } else {
  203. $result = $this->campaign->getCampaigns(null, null, $start, $limit);
  204. $total = $this->campaign->getTotalCampaign();
  205. }
  206. }
  207. } else {
  208. $result = $this->campaign->getCampaigns();
  209. }
  210. switch($format) {
  211. case 'object':
  212. return $result;
  213. break;
  214. case 'array':
  215. break;
  216. case 'datatables':
  217. $data = [];
  218. if($this->input->post()) {
  219. $records = [];
  220. foreach($result as $r) {
  221. array_push($records, [
  222. 'id_campaign' => $r->id_campaign
  223. ,'des_name' => $r->des_name
  224. ,'ind_status' => $this->config->item('OPTIONS_SINO')[$r->ind_status]
  225. ]);
  226. }
  227. $data = ['draw' => $draw
  228. ,'recordsTotal' => $total
  229. ,'recordsFiltered' => $total
  230. ,'data' => $records
  231. ];
  232. }
  233. $this->output
  234. ->set_content_type('application/json')
  235. ->set_output(json_encode( $data ));
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. #Agents To Campaign
  242. public function setCampaignAgent() {
  243. if( !acl('ind_add', 'agent_to_campaign') ) {
  244. $this->output
  245. ->set_content_type('application/json')
  246. ->set_output(json_encode(['isError' => true, 'msg' => 'No tiene permisos suficientes.']));
  247. } else {
  248. $format = (sizeof(func_get_args()) >= 0x0001) ? func_get_arg(0): $this->uri->segment(3);
  249. $ID = (sizeof(func_get_args()) >= 0x0002) ? func_get_arg(1): $this->uri->segment(4);
  250. $id_agent = $this->input->post('agent');
  251. $values = ['id_campaign' => $ID, 'id_agent' => $id_agent[0]];
  252. switch($format) {
  253. case 'json':
  254. if($this->campaign->setCampaignAgent($values) === FALSE) {
  255. $this->output
  256. ->set_content_type('application/json')
  257. ->set_output(json_encode(['isError' => true, 'msg' => 'Error al guardar intentelo mas tarde.']));
  258. } else {
  259. regenerateQueues();
  260. $this->output
  261. ->set_content_type('application/json')
  262. ->set_output(json_encode(['isError' => true, 'title' => 'Correcto', 'msg' => 'Agente agregado correctamente.']));
  263. }
  264. break;
  265. }
  266. }
  267. }
  268. public function deleteCampaignAgent() {
  269. if( !acl('ind_del', 'agent_to_campaign') ) {
  270. $this->output
  271. ->set_content_type('application/json')
  272. ->set_output(json_encode(['isError' => true, 'msg' => 'No tiene permisos suficientes.']));
  273. } else {
  274. $format = (sizeof(func_get_args()) >= 0x0001) ? func_get_arg(0): $this->uri->segment(3);
  275. $ID = (sizeof(func_get_args()) >= 0x0002) ? func_get_arg(1): $this->uri->segment(4);
  276. $id_agent = $this->input->post('agent');
  277. $values = ['id_campaign' => $ID, 'id_agent' => $id_agent[0]];
  278. switch($format) {
  279. case 'json':
  280. if($this->campaign->deleteCampaignAgent($values) === FALSE) {
  281. $this->output
  282. ->set_content_type('application/json')
  283. ->set_output(json_encode(['isError' => true, 'msg' => 'Error al eliminar, intentelo mas tarde.']));
  284. } else {
  285. regenerateQueues();
  286. $this->output
  287. ->set_content_type('application/json')
  288. ->set_output(json_encode(['isError' => true, 'title' => 'Correcto', 'msg' => 'Agente eliminado correctamente.']));
  289. }
  290. break;
  291. }
  292. }
  293. }
  294. }