예제 #1
0
 /**
  * Add new keyword
  * @return json
  * @author Ruslan Ushakov
  */
 function addKeyword()
 {
     $keyword = $this->input->post('keyword');
     $group = (int) $this->input->post('group');
     $res = array();
     $keyword_arr = explode(',', $keyword);
     if (!empty($keyword_arr)) {
         $this->load->model('ranking_model');
         if ($group == -1) {
             $group = $this->ranking_model->selectOrIKeywordGroup('Dont show');
             $this->ranking_model->deleteSearchTermsByGroupId($group);
             $this->ranking_model->addKeywordGroupCronJob($group, $this->ranking_model->CRON_PERIOD_DAY);
         }
         foreach ($keyword_arr as &$kword) {
             $kword = trim($kword);
             if ($kword) {
                 $res[] = $this->ranking_model->selectOrInsertIdSearchTerms($kword, $group);
             }
         }
     }
     $this->output->set_content_type('application/json')->set_output(json_encode(array('status' => 'ok', 'k_ids' => $res, 'group' => $group)));
 }
예제 #2
0
 /**
  * Import Search terms groups (including search terms), 'search terms groups - sites'
  * relations as json file
  */
 public function search_terms_groups_import()
 {
     session_id() || session_start();
     $result = array();
     $this->load->model('ranking_model');
     $this->load->model('alert_model');
     $file_to_get = null;
     if (isset($_FILES['file'], $_FILES['file']['error'])) {
         // uploading file
         if ($_FILES['file']['error'] == 0) {
             // resave it, to keep after this script finishes
             if ($temp_file_name = tempnam(sys_get_temp_dir(), '')) {
                 if (copy($_FILES['file']['tmp_name'], $temp_file_name)) {
                     $_SESSION['stgs_imp_tmp_file'] = $temp_file_name;
                     unset($_SESSION['stgs_imp_answers']);
                     unset($_SESSION['stgs_imp_answers_others']);
                     $file_to_get = $temp_file_name;
                 } else {
                     $result['error'] = 'Can\'t save file (writing)';
                 }
             } else {
                 $result['error'] = 'Can\'t save file (creating)';
             }
         } else {
             $this->lang->load('upload');
             $_FILES['file']['error'] = isset($_FILES['file']['error']) ? $_FILES['file']['error'] : 4;
             switch ($_FILES['file']['error']) {
                 case 1:
                     // UPLOAD_ERR_INI_SIZE
                     $result['error'] = $this->lang->line('upload_file_exceeds_limit');
                     break;
                 case 2:
                     // UPLOAD_ERR_FORM_SIZE
                     $result['error'] = $this->lang->line('upload_file_exceeds_form_limit');
                     break;
                 case 3:
                     // UPLOAD_ERR_PARTIAL
                     $result['error'] = $this->lang->line('upload_file_partial');
                     break;
                 case 4:
                     // UPLOAD_ERR_NO_FILE
                     $result['error'] = $this->lang->line('upload_no_file_selected');
                     break;
                 case 6:
                     // UPLOAD_ERR_NO_TMP_DIR
                     $result['error'] = $this->lang->line('upload_no_temp_directory');
                     break;
                 case 7:
                     // UPLOAD_ERR_CANT_WRITE
                     $result['error'] = $this->lang->line('upload_unable_to_write_file');
                     break;
                 case 8:
                     // UPLOAD_ERR_EXTENSION
                     $result['error'] = $this->lang->line('upload_stopped_by_extension');
                     break;
                 default:
                     $result['error'] = "Unknown error";
                     break;
             }
         }
     } elseif (isset($_SESSION['stgs_imp_tmp_file'])) {
         // get saved filename
         if (is_file($_SESSION['stgs_imp_tmp_file'])) {
             $file_to_get = $_SESSION['stgs_imp_tmp_file'];
         } else {
             // wipe all data, as it got outdated
             unset($_SESSION['stgs_imp_tmp_file']);
             unset($_SESSION['stgs_imp_answers']);
             unset($_SESSION['stgs_imp_answers_others']);
         }
     }
     if ($file_to_get) {
         if (($stgs_imp = json_decode(file_get_contents($file_to_get), true)) && is_array($stgs_imp)) {
             if (($answer = $this->input->post('answer')) && isset($answer['group_name'], $answer['action']) && in_array($answer['action'], array('overwrite', 'add'))) {
                 // get user's answers
                 $_SESSION['stgs_imp_answers'][strtolower($answer['group_name'])] = $answer['action'];
                 if (isset($answer['others']) && $answer['others']) {
                     $_SESSION['stgs_imp_answers_others'] = $answer['action'];
                 }
             }
             $stgs_imp_answers = isset($_SESSION['stgs_imp_answers']) ? $_SESSION['stgs_imp_answers'] : array();
             $stgs_imp_answers_others = isset($_SESSION['stgs_imp_answers_others']) ? $_SESSION['stgs_imp_answers_others'] : null;
             $entites = array();
             $stgs_cmp = $this->get_search_terms_groups_for_export($entites);
             // search_terms_groups for comparison
             $stgs_cmp_names = array_combine(array_map('strtolower', array_keys($stgs_cmp)), array_keys($stgs_cmp));
             $stgs_to_create = array();
             $stgs_to_overwrite = array();
             $stgs_to_add = array();
             $stgs_to_populate = array();
             /**
              * find entity by params
              * @param string $entity_name entity type (search_terms_groups, sites, groups_sites)
              * @param array $by array of params to search by
              * @param bool $cs whether search should be case sensitive
              * @param string $trim characters that need to be stripped
              */
             $find_entity = function ($entity_type, $by, $cs = false, $trim = '') use($entites) {
                 if (isset($entites[$entity_type])) {
                     foreach ($entites[$entity_type] as $entity) {
                         $it_is = null;
                         foreach ($by as $key => $value) {
                             if (!isset($entity[$key])) {
                                 return null;
                             } else {
                                 $val1 = trim($entity[$key], $trim);
                                 $val2 = trim($value, $trim);
                                 $cmp_f = $cs ? 'strcmp' : 'strcasecmp';
                                 $it_is = (is_null($it_is) ? true : $it_is) && $cmp_f($val1, $val2) === 0;
                             }
                         }
                         if ($it_is) {
                             return $entity;
                         }
                     }
                 }
                 return null;
             };
             foreach ($stgs_imp as $stg_imp_name => $stg_imp) {
                 $stg_imp_name_low = strtolower($stg_imp_name);
                 if (!isset($stgs_cmp_names[$stg_imp_name_low])) {
                     $stgs_to_create[$stg_imp_name] = $stg_imp;
                 } else {
                     $stg_cmp = $stgs_cmp[$stgs_cmp_names[$stg_imp_name_low]];
                     if ($this->search_terms_groups_compare($stg_imp, $stg_cmp)) {
                         continue;
                     }
                     if (isset($stgs_imp_answers[$stg_imp_name_low]) || $stgs_imp_answers_others) {
                         $action = isset($stgs_imp_answers[$stg_imp_name_low]) ? $stgs_imp_answers[$stg_imp_name_low] : $stgs_imp_answers_others;
                         if ($action == 'overwrite') {
                             // check whether existing search terms are used in alerts
                             // get search terms ids
                             $search_terms_ids = array();
                             foreach ($stg_imp['search_terms'] as $search_term_title) {
                                 $search_term = $find_entity('search_terms', array('title' => $search_term_title, 'group_name' => $stg_imp_name));
                                 if (!empty($search_term['id'])) {
                                     $search_terms_ids[] = $search_term['id'];
                                 }
                             }
                             if (!empty($search_terms_ids)) {
                                 $will_break_alert = false;
                                 $alerts = $this->alert_model->getRecords();
                                 foreach ($alerts as $alert) {
                                     $alert->options = unserialize($alert->options);
                                     $alert_search_terms_ids = array();
                                     if (!empty($alert->options['rank_term']['all_checked_checkboxes'])) {
                                         foreach ($alert->options['rank_term']['all_checked_checkboxes'] as $gr_id => $gr) {
                                             $gr_search_terms_ids = empty($gr['id']) ? array() : $gr['id'];
                                             $alert_search_terms_ids = array_merge($alert_search_terms_ids, $gr_search_terms_ids);
                                         }
                                     }
                                     $array_intersect = array_intersect($search_terms_ids, $alert_search_terms_ids);
                                     if (!empty($array_intersect)) {
                                         $will_break_alert = $alert->name;
                                         break;
                                     }
                                 }
                                 if ($will_break_alert) {
                                     $result['question'] = array('alert_name' => $will_break_alert, 'group_name' => $stg_imp_name);
                                 }
                             }
                             $stgs_to_overwrite[$stg_imp_name] = $stg_imp;
                         } elseif ($action == 'add') {
                             $stgs_to_add[$stg_imp_name] = $stg_imp;
                         }
                     } else {
                         $result['question'] = array('group_name' => $stg_imp_name);
                         break;
                     }
                 }
             }
             if (!isset($result['question'])) {
                 // make import
                 foreach ($stgs_to_add as $stg_name => $stg) {
                     // find data to be added
                     if ($stg_entity = $find_entity('search_terms_groups', array('name' => $stg_name))) {
                         $stg_id = $stg_entity['id'];
                         $stg_cmp = $stgs_cmp[$stgs_cmp_names[strtolower($stg_name)]];
                         $search_terms_batch = array();
                         $stg_search_terms = array_combine(array_map('strtolower', $stg['search_terms']), $stg['search_terms']);
                         $stg_cmp_search_terms = array_combine(array_map('strtolower', $stg_cmp['search_terms']), $stg_cmp['search_terms']);
                         foreach (array_diff_key($stg_search_terms, $stg_cmp_search_terms) as $search_term) {
                             $search_terms_batch[] = array('title' => $search_term, 'group_id' => $stg_id);
                         }
                         if ($search_terms_batch) {
                             $this->ranking_model->create_batch_('search_terms', $search_terms_batch);
                         }
                         $prepare_sites = function ($site) {
                             $site['url'] = trim($site['url'], '/');
                             return strtolower(serialize($site));
                         };
                         $stg_sites = array_combine(array_map($prepare_sites, $stg['sites']), $stg['sites']);
                         $stg_cmp_sites = array_combine(array_map($prepare_sites, $stg_cmp['sites']), $stg_cmp['sites']);
                         $sites_to_add = array_diff_key($stg_sites, $stg_cmp_sites);
                         $groups_sites_batch = array();
                         foreach ($sites_to_add as $site_to_add) {
                             if ($site_entity = $find_entity('sites', $site_to_add, false, ' /')) {
                                 $site_id = $site_entity['id'];
                             } else {
                                 $site_data = array('name' => '', 'url' => '', 'image_url' => '', 'site_type' => 0);
                                 $site_data = array_merge($site_data, array_intersect_key($site_to_add, $site_data));
                                 $site_id = $this->ranking_model->create_('sites', $site_data);
                             }
                             $groups_sites_batch[] = array('group_id' => $stg_id, 'site_id' => $site_id);
                         }
                         if ($groups_sites_batch) {
                             $this->ranking_model->create_batch_('groups_sites', $groups_sites_batch);
                         }
                     }
                 }
                 foreach ($stgs_to_overwrite as $stg_name => $stg) {
                     // delete current stg's all corresponding data
                     if ($stg_entity = $find_entity('search_terms_groups', array('name' => $stg_name))) {
                         $stg['id'] = $stg_entity['id'];
                         $this->ranking_model->delete_('search_terms', array('group_id' => $stg['id']));
                         $this->ranking_model->delete_('groups_sites', array('group_id' => $stg['id']));
                         $stgs_to_populate[$stg_name] = $stg;
                     }
                 }
                 foreach ($stgs_to_create as $stg_name => $stg) {
                     // create stg & set it up for fetching keyword data by cron
                     if ($stg['id'] = $this->ranking_model->create_('search_terms_groups', array('name' => $stg_name))) {
                         $this->ranking_model->addKeywordGroupCronJob($stg['id'], $this->ranking_model->CRON_PERIOD_DAY);
                     }
                     $stgs_to_populate[$stg_name] = $stg;
                 }
                 foreach ($stgs_to_populate as $stg_name => $stg) {
                     // populate stg with all corresponding data
                     if (!isset($stg['id'])) {
                         continue;
                     }
                     $search_terms_batch = array();
                     foreach ($stg['search_terms'] as $search_term) {
                         $search_terms_batch[] = array('title' => $search_term, 'group_id' => $stg['id']);
                     }
                     if ($search_terms_batch) {
                         $this->ranking_model->create_batch_('search_terms', $search_terms_batch);
                     }
                     $groups_sites_batch = array();
                     foreach ($stg['sites'] as $site) {
                         if ($site_entity = $find_entity('sites', $site, false, ' /')) {
                             $site_id = $site_entity['id'];
                         } else {
                             $site_data = array('name' => '', 'url' => '', 'image_url' => '', 'site_type' => 0);
                             $site_data = array_merge($site_data, array_intersect_key($site, $site_data));
                             $site_id = $this->ranking_model->create_('sites', $site_data);
                         }
                         $groups_sites_batch[] = array('group_id' => $stg['id'], 'site_id' => $site_id);
                     }
                     if ($groups_sites_batch) {
                         $this->ranking_model->create_batch_('groups_sites', $groups_sites_batch);
                     }
                 }
                 $result['result'] = 'Search terms groups were inported successfully';
             }
         } else {
             $result['error'] = json_last_error_msg() ? 'JSON error: ' . json_last_error_msg() : 'Wrong data format (not an array)';
         }
     } else {
         $result['error'] = 'No file';
     }
     $this->output->set_content_type('application/json')->set_output(json_encode($result));
 }