Esempio n. 1
0
 /**
  * Add or Update Group
  * @return json
  */
 function addOrEditeGroup()
 {
     $group_id = (int) $this->input->post('group_id');
     $group_name = trim($this->input->post('group_name'));
     $terms_keywords = $this->input->post('terms_keywords');
     $retailers_sites = $this->input->post('retailers_sites');
     $group_enabled = $this->input->post('group_enabled');
     if (empty($group_enabled) || $group_enabled != 'false') {
         $group_enabled = 'true';
     }
     $error = '';
     $success = '';
     $find_entity = function (&$entites, $by) {
         if (is_array($entites)) {
             foreach ($entites as $entity) {
                 $it_is = null;
                 foreach ($by as $key => $value) {
                     $it_is = (is_null($it_is) ? true : $it_is) && (isset($entity->{$key}) && $entity->{$key} == $value);
                 }
                 if ($it_is) {
                     return $entity;
                 }
             }
         }
         return null;
     };
     if (!$group_name) {
         $status = 'error';
         $error = 'Group Name filed can not be empty';
     } else {
         $status = 'ok';
         $success = 'Success';
     }
     if ($status == 'ok') {
         $this->load->model('ranking_model');
         if (!$group_id) {
             $group_id = $this->ranking_model->selectOrIKeywordGroup($group_name, false, $group_enabled);
         } else {
             $this->ranking_model->updateKeywordGroup($group_id, $group_name, $group_enabled);
         }
         if (!empty($group_id)) {
             if (!empty($terms_keywords) && is_array($terms_keywords)) {
                 $terms_keywords = array_map('trim', $terms_keywords);
                 $search_terms = $this->ranking_model->get_('search_terms', array('group_id' => $group_id));
                 // creation
                 $search_terms_added = false;
                 foreach ($terms_keywords as $kword) {
                     $kword_data = array('title' => $kword, 'group_id' => $group_id);
                     $kword_entity = $find_entity($search_terms, $kword_data);
                     if (!$kword_entity) {
                         $kword_id = $this->ranking_model->create_('search_terms', $kword_data);
                         if (!empty($kword_id)) {
                             $search_terms[] = (object) array_merge(array('id' => $kword_id), $kword_data);
                             $search_terms_added = true;
                         }
                     }
                 }
                 if ($search_terms_added) {
                     // update keyword_cron_jobs, to crawl new added search terms
                     $keyword_cron_jobs = $this->ranking_model->getParentCronJobsByGroup($group_id);
                     $keyword_cron_jobs_ids = array_map(function ($cron_job) {
                         return $cron_job->id;
                     }, $keyword_cron_jobs);
                     if (!empty($keyword_cron_jobs_ids)) {
                         // crawl/keyword_cron_job will recheck these groups, and create new cron jobs
                         $this->ranking_model->changeCronJobStatus($keyword_cron_jobs_ids, $this->ranking_model->CRON_STATUS_WAITING);
                     }
                 }
                 // deletion
                 foreach ($search_terms as $search_term_obj) {
                     if (!in_array($search_term_obj->title, $terms_keywords)) {
                         $this->ranking_model->delete_('search_terms', array('id' => $search_term_obj->id));
                     }
                 }
             }
             if (!empty($retailers_sites) && is_array($retailers_sites)) {
                 $groups_sites = $this->ranking_model->get_('groups_sites', array('group_id' => $group_id));
                 // creation
                 $user_id = $this->session->userdata('user_id');
                 foreach ($retailers_sites as $site_id) {
                     $groups_sites_data = array('group_id' => $group_id, 'site_id' => $site_id);
                     $groups_sites_entity = $find_entity($groups_sites, $groups_sites_data);
                     if (!$groups_sites_entity) {
                         $groups_sites_id = $this->ranking_model->create_('groups_sites', $groups_sites_data);
                         if (!empty($groups_sites_id)) {
                             $groups_sites[] = (object) array_merge(array('id' => $groups_sites_id), $groups_sites_data);
                             if (!empty($search_terms)) {
                                 $this->ranking_model->addCronJob($group_id, $site_id, '', 0, $user_id);
                             }
                         }
                     }
                 }
                 // deletion
                 foreach ($groups_sites as $groups_sites_obj) {
                     if (!in_array($groups_sites_obj->site_id, $retailers_sites)) {
                         $this->ranking_model->delete_('groups_sites', array('id' => $groups_sites_obj->id));
                     }
                 }
             }
         }
     }
     $this->output->set_content_type('application/json')->set_output(json_encode(array('status' => $status, 'error' => $error, 'success' => $success)));
 }