Ejemplo n.º 1
0
 public function ExportAssess_post($type = 'download')
 {
     // Initial Data
     $batch_id = trim($this->input->post('batch_id'));
     $cmp_selected = intval(trim(strtolower($this->input->post('cmp_selected'))));
     $selected_columns = explode(',', trim($this->input->post('checked_columns')));
     $batch_name = $this->input->post('batch_name');
     $catId = $this->input->post('prodcat_id');
     $chronicle_id = $this->input->post('chronicle_id');
     $brand_id = $this->input->post('brand_id');
     $chronicle2 = $this->input->post('chronicle2');
     $intersection = $this->input->post('intersection');
     $mode = $this->input->post('mode');
     if (empty($mode)) {
         $mode = 'export_primary';
     }
     $specialFilters = $this->input->post('specialFilterType');
     $superFilter = $this->input->post('super_filter');
     $batch_summary_combo = $this->input->post('batch_summary_combo');
     // Getting batch
     if ($batch_summary_combo) {
         $this->load->model('batches_summary_combinations');
         $combo_object = $this->batches_summary_combinations->GetComboByChronicles($batch_summary_combo);
         $chronicle_id = json_decode($batch_summary_combo);
         if ($combo_object) {
             $batch_name = $combo_object->name;
             $batch_id = NULL;
         }
     } else {
         $this->load->model('batches_model');
         $batch_object = $this->batches_model->getBatchByChronicle($chronicle_id);
         if ($batch_object) {
             $batch_name = $batch_object->title;
             $batch_id = $batch_object->id;
         }
     }
     // Export summary probably should be other function
     if ($mode == 'export_summary') {
         return $this->_export_summary($batch_name, $chronicle_id);
     }
     // Getting filters
     $filters = $this->input->post('summaryFilterData');
     $summaryFilterData = $filters ? explode(',', $filters) : array();
     // All available lines
     $line = array('created' => 'Date', 'imp_data_id' => 'Item ID', 'product_name' => 'Product Name', 'model' => 'Model', 'url' => 'Url', 'total_description_wc' => 'Prod Desc # words', 'short_description' => 'Short Description', 'short_description_wc' => 'Short Desc # Words', 'long_description' => 'Long Description', 'long_description_wc' => 'Long Desc # Words', 'short_seo_phrases' => ' Short Desc - Found SEO Keywords', 'long_seo_phrases' => ' Short Desc - Found SEO Keywords', 'price_diff' => 'Price', 'column_features' => 'Features', 'column_reviews' => 'Reviews', 'average_review' => 'Avg Review', 'title_seo_phrases' => 'Title Keywords', 'images_cmp' => 'Images', 'links_count' => 'Links', 'murl' => 'Manufacturer Page', 'mimg' => 'Manufacturer Images', 'mvid' => 'Manufacturer Videos', 'snap' => 'Snapshot', 'title_pa' => 'Title', 'price_diff' => 'Price', 'Page_Load_Time' => 'Page Load Time', 'Meta_Keywords' => 'Meta Keywords', 'video_count' => 'Video', 'Custom_Keywords_Short_Description' => 'Custom Keywords Short Description', 'Custom_Keywords_Long_Description' => 'Custom Keywords Long Description', 'Meta_Description' => 'Meta Description', 'Meta_Description_Count' => 'Meta Desc # words', 'H1_Tags' => 'H1 Tags', 'H1_Tags_Count' => 'Chars H1', 'H2_Tags' => 'H2 Tags', 'H2_Tags_Count' => 'Chars H2', 'duplicate_content' => 'Duplicate content', 'column_external_content' => 'Third Party Content', 'gap' => 'Gap Analysis', 'prodcat' => 'Category');
     $notCompare = array('murl' => 1, 'mimg' => 2, 'mvid' => 3);
     // Getting selected columns and checking them in available lines
     $headers = array_flip($selected_columns);
     $line = array_intersect_key($line, $headers);
     if (!$chronicle_id) {
         $chronicle_id = '0';
     }
     // Output empty result if there is no fields
     if (count($line) == 0) {
         $result = array('type' => 'processing_error', 'message' => 'We are sorry, this batch and date can\'t be exported at this time.');
         $this->response($result, 400);
     }
     $res_array = array();
     $this->load->model('assess_results');
     // Getting all results
     // $results = $this->assess_results->getPreloadedResult($chronicle_id, $batch_id, $catId, $cmp_selected, $summaryFilterData, 0, 'all', $chronicle2, $intersection, 0, 0, $specialFilters, TRUE);
     $results = $this->assess_results->getExportResults(array('chronicle' => $chronicle_id, 'category' => $catId, 'summaryFilter' => $summaryFilterData, 'brand' => $brand_id, 'superFilter' => $superFilter), $mode, $line);
     if (!$results) {
         $this->load->model('statistics_new_model');
         $this->load->model('statistics_new_model_archive');
         $params = new stdClass();
         $params->batch_id = $batch_id;
         $default_statistics_model_object = 'statistics_new_model';
         // If we didn`t have right batch_object earlier - trying to get it from received batch_id
         $this->load->model('batches_model');
         if (!$batch_object) {
             $batch_object = $this->batches_model->get($batch_id);
         }
         if ($batch_object->type == $this->batches_model->getTypeId('Subset Batch')) {
             $default_statistics_model_object = 'statistics_new_model_archive';
         }
         $results = $this->{$default_statistics_model_object}->getStatsData($params);
         $cmp = array();
         if (!empty($cmp_selected) && $cmp_selected != 'all') {
             $max_similar_item_count = 1;
             $customer_name = $this->batches_model->getCustomerUrlByBatch($cmp_selected);
             foreach ($results as $val) {
                 $val->Long_Description = $val->long_description;
                 $val->Short_Description = $val->short_description;
                 $similar_items_data = array();
                 if (stripos($val->similar_products_competitors, $customer_name)) {
                     $similar_items = unserialize($val->similar_products_competitors);
                     if (count($similar_items) > 1) {
                         foreach ($similar_items as $key => $item) {
                             if (!empty($customer_name) && !empty($item['customer']) && $this->{$default_statistics_model_object}->if_url_in_batch($item['imported_data_id'], $cmp_selected)) {
                                 $den_for_gap = 0;
                                 $parsed_attributes_unserialize_val = '';
                                 $parsed_meta_unserialize_val = '';
                                 $parsed_meta_unserialize_val_c = '';
                                 $parsed_attributes_column_features_unserialize_val = 0;
                                 $parsed_model_unserialize_val = '';
                                 $parsed_meta_keywords_unserialize_val = '';
                                 $parsed_loaded_in_seconds_unserialize_val = '';
                                 $parsed_average_review_unserialize_val_count = '';
                                 $column_external_content = '';
                                 $parsed_review_count_unserialize_val_count = 0;
                                 $cmpare = $this->{$default_statistics_model_object}->get_compare_item($item['imported_data_id']);
                                 $parsed_attributes_unserialize = unserialize($cmpare->parsed_attributes);
                                 if (isset($parsed_attributes_unserialize['cnetcontent']) || isset($parsed_attributes_unserialize['webcollage'])) {
                                     $column_external_content = $this->column_external_content($parsed_attributes_unserialize['cnetcontent'], $parsed_attributes_unserialize['webcollage']);
                                 }
                                 if (isset($parsed_attributes_unserialize['feature_count'])) {
                                     $parsed_attributes_column_features_unserialize_val = $parsed_attributes_unserialize['feature_count'];
                                 }
                                 //if (isset($parsed_attributes_unserialize['item_id']))
                                 //    $parsed_attributes_unserialize_val = $parsed_attributes_unserialize['item_id'];
                                 if (isset($parsed_attributes_unserialize['model'])) {
                                     $parsed_model_unserialize_val = $parsed_attributes_unserialize['model'];
                                 }
                                 if (isset($parsed_attributes_unserialize['loaded_in_seconds'])) {
                                     $parsed_loaded_in_seconds_unserialize_val = $parsed_attributes_unserialize['loaded_in_seconds'];
                                 }
                                 if (isset($parsed_attributes_unserialize['average_review'])) {
                                     $parsed_average_review_unserialize_val_count = $parsed_attributes_unserialize['average_review'];
                                 }
                                 if (isset($parsed_attributes_unserialize['review_count'])) {
                                     $parsed_review_count_unserialize_val_count = $parsed_attributes_unserialize['review_count'];
                                 }
                                 $parsed_meta_unserialize = unserialize($cmpare->parsed_meta);
                                 if (isset($parsed_meta_unserialize['description'])) {
                                     $parsed_meta_unserialize_val = $parsed_meta_unserialize['description'];
                                     $parsed_meta_unserialize_val_c = count(explode(" ", $parsed_meta_unserialize_val));
                                     if ($parsed_meta_unserialize_val_c != 1) {
                                         $parsed_meta_unserialize_val_count = $parsed_meta_unserialize_val_c;
                                     }
                                 } else {
                                     if (isset($parsed_meta_unserialize['Description'])) {
                                         $parsed_meta_unserialize_val = $parsed_meta_unserialize['Description'];
                                         $parsed_meta_unserialize_val_c = count(explode(" ", $parsed_meta_unserialize_val));
                                         if ($parsed_meta_unserialize_val_c != 1) {
                                             $parsed_meta_unserialize_val_count = $parsed_meta_unserialize_val_c;
                                         }
                                     }
                                 }
                                 if (isset($parsed_meta_unserialize['keywords'])) {
                                     $Meta_Keywords_un = "";
                                     $cnt_meta = explode(',', $parsed_meta_unserialize['keywords']);
                                     $cnt_meta_count = count($cnt_meta);
                                     $_count_meta = 0;
                                     foreach ($cnt_meta as $cnt_m) {
                                         $cnt_m = trim($cnt_m);
                                         if ($cmpare->Short_Description || $cmpare->Long_Description) {
                                             $_count_meta = $this->keywords_appearence($cmpare->Long_Description . $cmpare->Short_Description, $cnt_m);
                                             $_count_meta_num = round($_count_meta * $cnt_meta_count / ($cmpare->long_description_wc + $cmpare->short_description_wc) * 100, 2);
                                             if ($_count_meta_num >= 2) {
                                                 $den_for_gap = $_count_meta_num;
                                             }
                                             $Meta_Keywords_un .= $cnt_m . "  - " . $_count_meta_num . "%, ";
                                         }
                                     }
                                     $parsed_meta_keywords_unserialize_val = $Meta_Keywords_un;
                                 }
                                 //export title keywords compare
                                 $title_seo_prases = array();
                                 if ($cmpare->title_keywords != '' && $cmpare->title_keywords != 'None') {
                                     $title_seo_prases = unserialize($cmpare->title_keywords);
                                 }
                                 if (!empty($title_seo_prases)) {
                                     $title_seo_phrases = '';
                                     foreach ($title_seo_prases as $pras) {
                                         $title_seo_phrases .= $pras['ph'] . ' - ' . $pras['prc'] . '%,  ';
                                     }
                                 }
                                 //$cmpare->item_id = $parsed_attributes_unserialize_val;
                                 $cmpare->model = $parsed_model_unserialize_val;
                                 $cmpare->den_for_gap = $den_for_gap;
                                 $cmpare->Page_Load_Time = $parsed_loaded_in_seconds_unserialize_val;
                                 $cmpare->Meta_Keywords = $parsed_meta_keywords_unserialize_val;
                                 $cmpare->column_features = $parsed_attributes_column_features_unserialize_val;
                                 $cmpare->column_external_content = $column_external_content;
                                 $cmpare->Meta_Description = $parsed_meta_unserialize_val;
                                 $cmpare->Meta_Description_Count = $parsed_meta_unserialize_val_count;
                                 $cmpare->average_review = $parsed_average_review_unserialize_val_count;
                                 $cmpare->review_count = $parsed_review_count_unserialize_val_count;
                                 $cmpare->title_seo_phrases = $title_seo_phrases;
                                 $similar_items_data[] = $cmpare;
                                 $val->similar_items = $similar_items_data;
                                 $cmp[] = $val;
                                 break;
                             }
                         }
                     }
                 }
             }
             $results = $cmp;
         }
         $H1_tag_count = 0;
         $H2_tag_count = 0;
         $H1_tag_count_for_sim = 0;
         $H2_tag_count_for_sim = 0;
         foreach ($results as $key => $row) {
             $sim = $row->similar_items;
             $pars = unserialize($row->parsed_attributes);
             $sim_pars = unserialize($sim[0]->parsed_attributes);
             $title_seo_pr = array();
             $row->title_seo_phrases = 0;
             if (trim($row->title_keywords) && $row->title_keywords != 'None') {
                 $title_seo_pr = unserialize($row->title_keywords);
             }
             $str_title_long_seo = '';
             if (!empty($title_seo_pr)) {
                 foreach ($title_seo_pr as $val) {
                     $row->title_seo_phrases += $val['frq'];
                     $str_title_long_seo .= $val['ph'] . ' - ' . $val['prc'] . '%,  ';
                 }
             }
             $title_seo_pr = array();
             $sim[0]->title_seo_phrases = 0;
             if (trim($sim[0]->title_keywords) && $sim[0]->title_keywords != 'None') {
                 $title_seo_pr = unserialize($sim[0]->title_keywords);
             }
             if (!empty($title_seo_pr)) {
                 foreach ($title_seo_pr as $val) {
                     $sim[0]->title_seo_phrases += $val['frq'];
                 }
             }
             $success_filter_entries = array();
             $row->Short_Description = $row->short_description;
             $row->Long_Description = $row->long_description;
             $short_desc_1 = '';
             if ($row->short_description) {
                 $short_desc_1 = $row->short_description;
             }
             $long_desc_1 = '';
             if ($row->long_description) {
                 $long_desc_1 = $row->long_description;
             }
             $desc_1 = $short_desc_1 . ' ' . $long_desc_1;
             if ($sim[0]->Short_Description) {
                 $short_desc_2 = $sim[0]->Short_Description;
             } else {
                 $short_desc_2 = '';
             }
             if ($sim[0]->Long_Description) {
                 $long_desc_2 = $sim[0]->Long_Description;
             } else {
                 $long_desc_2 = '';
             }
             $desc_2 = $short_desc_2 . ' ' . $long_desc_2;
             if (strcasecmp($desc_1, $desc_2) <= 0) {
                 similar_text($desc_1, $desc_2, $percent);
             } else {
                 similar_text($desc_2, $desc_1, $percent);
             }
             $percent = number_format($percent, 2);
             if ($percent >= 25) {
                 $this->filterBySummaryCriteria('skus_25_duplicate_content', $summaryFilterData, $success_filter_entries);
             }
             if ($percent >= 50) {
                 $this->filterBySummaryCriteria('skus_50_duplicate_content', $summaryFilterData, $success_filter_entries);
             }
             if ($percent >= 75) {
                 $this->filterBySummaryCriteria('skus_75_duplicate_content', $summaryFilterData, $success_filter_entries);
             }
             if (isset($pars['pdf_count']) && $pars['pdf_count']) {
                 $this->filterBySummaryCriteria('skus_pdfs', $summaryFilterData, $success_filter_entries);
             }
             if (isset($sim_pars['pdf_count']) && $sim_pars['pdf_count']) {
                 $this->filterBySummaryCriteria('skus_pdfs_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($pars['feature_count'] < $sim_pars['feature_count']) {
                 $this->filterBySummaryCriteria('skus_fewer_features_than_competitor', $summaryFilterData, $success_filter_entries);
             }
             //if ($pars['review_count'] < $sim_pars['review_count('])
             if ($pars['review_count'] < $sim_pars['review_count']) {
                 $this->filterBySummaryCriteria('skus_fewer_reviews_than_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($pars['feature_count']) {
                 $this->filterBySummaryCriteria('skus_features', $summaryFilterData, $success_filter_entries);
             }
             if ($sim_pars['feature_count']) {
                 $this->filterBySummaryCriteria('skus_features_competitor', $summaryFilterData, $success_filter_entries);
             }
             if (!$pars['review_count']) {
                 $this->filterBySummaryCriteria('skus_zero_reviews', $summaryFilterData, $success_filter_entries);
             }
             if ($pars['review_count'] >= 1 && $pars['review_count'] <= 20) {
                 $this->filterBySummaryCriteria('skus_one_four_reviews', $summaryFilterData, $success_filter_entries);
             }
             if ($pars['review_count'] >= 21) {
                 $this->filterBySummaryCriteria('skus_25plus_reviews', $summaryFilterData, $success_filter_entries);
             }
             /*if ($pars['review_count'] >= 5 && $pars['review_count'] <= 99) {
             		$this->filterBySummaryCriteria('skus_five_ninetynine_reviews', $summaryFilterData, $success_filter_entries);
             		}
             		if ($pars['review_count'] >= 100) {
             		$this->filterBySummaryCriteria('skus_more_than_hundred_reviews', $summaryFilterData, $success_filter_entries);
             		}*/
             if (!$sim_pars['review_count']) {
                 $this->filterBySummaryCriteria('skus_zero_reviews_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($sim_pars['review_count'] >= 1 && $sim_pars['review_count'] <= 20) {
                 $this->filterBySummaryCriteria('skus_one_four_reviews_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($sim_pars['review_count'] >= 21) {
                 $this->filterBySummaryCriteria('skus_25plus_reviews_competitor', $summaryFilterData, $success_filter_entries);
             }
             /*if ($sim_pars['review_count'] >= 5 && $sim_pars['review_count'] <= 99) {
             		$this->filterBySummaryCriteria('skus_five_ninetynine_reviews_competitor', $summaryFilterData, $success_filter_entries);
             		}
             		if ($sim_pars['review_count'] >= 100) {
             		$this->filterBySummaryCriteria('skus_more_than_hundred_reviews_competitor', $summaryFilterData, $success_filter_entries);
             		}*/
             $batch1_filtered_title_percents = $row->title_seo_phrases;
             $batch2_filtered_title_percents = $sim[0]->title_seo_phrases;
             /* if ($batch1_filtered_title_percents < $batch2_filtered_title_percents)
             			{
             			$this->filterBySummaryCriteria('skus_fewer_competitor_optimized_keywords', $summaryFilterData, $success_filter_entries);
             			} */
             if (!$batch1_filtered_title_percents) {
                 $this->filterBySummaryCriteria('skus_zero_optimized_keywords', $summaryFilterData, $success_filter_entries);
             }
             if ($batch1_filtered_title_percents >= 1) {
                 $this->filterBySummaryCriteria('skus_one_optimized_keywords', $summaryFilterData, $success_filter_entries);
             }
             if ($batch1_filtered_title_percents >= 2) {
                 $this->filterBySummaryCriteria('skus_two_optimized_keywords', $summaryFilterData, $success_filter_entries);
             }
             if ($batch1_filtered_title_percents >= 3) {
                 $this->filterBySummaryCriteria('skus_three_optimized_keywords', $summaryFilterData, $success_filter_entries);
             }
             if (!$batch2_filtered_title_percents) {
                 $this->filterBySummaryCriteria('skus_zero_optimized_keywords_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($batch2_filtered_title_percents >= 1) {
                 $this->filterBySummaryCriteria('skus_one_optimized_keywords_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($batch2_filtered_title_percents >= 2) {
                 $this->filterBySummaryCriteria('skus_two_optimized_keywords_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($batch2_filtered_title_percents >= 3) {
                 $this->filterBySummaryCriteria('skus_three_optimized_keywords_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($row->column_external_content) {
                 $this->filterBySummaryCriteria('skus_third_party_content', $summaryFilterData, $success_filter_entries);
             }
             if ($sim[0]->column_external_content) {
                 $this->filterBySummaryCriteria('skus_third_party_content_competitor', $summaryFilterData, $success_filter_entries);
             }
             $first_general_description_size = $row->short_description_wc + $row->long_description_wc;
             $second_general_description_size = $sim[0]->long_description_wc + $sim[0]->short_description_wc;
             if ($first_general_description_size < $second_general_description_size) {
                 $this->filterBySummaryCriteria('skus_shorter_than_competitor_product_content', $summaryFilterData, $success_filter_entries);
             }
             if ($first_general_description_size > $second_general_description_size) {
                 $this->filterBySummaryCriteria('skus_longer_than_competitor_product_content', $summaryFilterData, $success_filter_entries);
             }
             if ($first_general_description_size == $second_general_description_size) {
                 $this->filterBySummaryCriteria('skus_same_competitor_product_content', $summaryFilterData, $success_filter_entries);
             }
             // For Batch 1
             if ($first_general_description_size < 50) {
                 $this->filterBySummaryCriteria('skus_fewer_50_product_content', $summaryFilterData, $success_filter_entries);
             }
             if ($first_general_description_size < 100) {
                 $this->filterBySummaryCriteria('skus_fewer_100_product_content', $summaryFilterData, $success_filter_entries);
             }
             if ($first_general_description_size > 100) {
                 $this->filterBySummaryCriteria('skus_more_100_product_content', $summaryFilterData, $success_filter_entries);
             }
             if ($first_general_description_size < 150) {
                 $this->filterBySummaryCriteria('skus_fewer_150_product_content', $summaryFilterData, $success_filter_entries);
             }
             // For Competitor (Batch 2)
             if ($second_general_description_size < 50) {
                 $this->filterBySummaryCriteria('skus_fewer_50_product_content_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($second_general_description_size < 100) {
                 $this->filterBySummaryCriteria('skus_fewer_100_product_content_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($second_general_description_size > 100) {
                 $this->filterBySummaryCriteria('skus_more_100_product_content_competitor', $summaryFilterData, $success_filter_entries);
             }
             if ($second_general_description_size < 150) {
                 $this->filterBySummaryCriteria('skus_fewer_150_product_content_competitor', $summaryFilterData, $success_filter_entries);
             }
             //checking if row should be added to file
             if ($this->checkSuccessFilterEntries($success_filter_entries, $summaryFilterData)) {
                 $arr = array();
                 $this->load->model('imported_data_parsed_model');
                 $pars_atr = $this->imported_data_parsed_model->getByImId($row->imported_data_id);
                 foreach ($line as $k => $v) {
                     $arr[$k] = $row->{$k};
                 }
                 //Htag_count
                 if (in_array('H1_Tags', $selected_columns) && $pars_atr['HTags']['h1'] && trim($pars_atr['HTags']['h1'])) {
                     $H1 = $pars_atr['HTags']['h1'];
                     if (is_array($H1)) {
                         $h1cnt = count($H1);
                         if ($h1cnt > $H1_tag_count) {
                             $H1_tag_count = $h1cnt;
                         }
                         $i = 0;
                         foreach ($H1 as $k => $h1) {
                             if ($i < 2) {
                                 $arr['H1_Tags' . $k] = $h1;
                                 $arr['H1_Tags_Count' . $k] = strlen($h1);
                                 $i++;
                             } else {
                                 break;
                             }
                         }
                     } else {
                         if ($H1_tag_count == 0) {
                             $H1_tag_count = 1;
                         }
                         $arr['H1_Tags0'] = $H1;
                         $arr['H1_Tags_Count0'] = strlen($H1);
                     }
                     if ($H1_tag_count > 0) {
                         if ($H1_tag_count > 2) {
                             $H1_tag_count = 2;
                         }
                         for ($k = 0; $k < $H1_tag_count; $k++) {
                             if (!$arr['H1_Tags' . $k]) {
                                 $arr['H1_Tags' . $k] = '';
                                 $arr['H1_Tags_Count' . $k] = ' ';
                             }
                         }
                     }
                 }
                 if (in_array('H2_Tags', $selected_columns) && $pars_atr['HTags']['h2'] && trim($pars_atr['HTags']['h2'])) {
                     $H2 = $pars_atr['HTags']['h2'];
                     if (is_array($H2)) {
                         $h2cnt = count($H2);
                         if ($h2cnt > $H2_tag_count) {
                             $H2_tag_count = $h2cnt;
                         }
                         $i = 0;
                         foreach ($H2 as $k => $h2) {
                             if ($i < 2) {
                                 $arr['H2_Tags' . $k] = $h2;
                                 $arr['H2_Tags_Count' . $k] = strlen($h2);
                                 $i++;
                             } else {
                                 break;
                             }
                         }
                     } else {
                         if ($H2_tag_count < 1) {
                             $H2_tag_count = 1;
                         }
                         $arr['H2_Tags0'] = $H2;
                         $arr['H2_Tags_Count0'] = strlen($H2);
                     }
                     if ($H2_tag_count > 0) {
                         if ($H2_tag_count > 2) {
                             $H2_tag_count = 2;
                         }
                         for ($k = 0; $k < $H2_tag_count; $k++) {
                             if (!$arr['H2_Tags' . $k]) {
                                 $arr['H2_Tags' . $k] = '';
                                 $arr['H2_Tags_Count' . $k] = ' ';
                             }
                         }
                     }
                 } else {
                     if ($H2_tag_count > 2) {
                         $H2_tag_count = 2;
                     }
                     for ($k = 0; $k < $H2_tag_count; $k++) {
                         if (!$arr['H2_Tags' . $k]) {
                             $arr['H2_Tags' . $k] = '';
                             $arr['H2_Tags_Count' . $k] = ' ';
                         }
                     }
                 }
                 //Htag_count for similar
                 $pars_atr_for_sim = $this->imported_data_parsed_model->getByImId($sim[0]->imported_data_id);
                 if (in_array('H1_Tags', $selected_columns) && $pars_atr_for_sim['HTags']['h1'] && $pars_atr_for_sim['HTags']['h1'] != '') {
                     $H1 = $pars_atr_for_sim['HTags']['h1'];
                     if (is_array($H1)) {
                         $h1Cnt = count($H1);
                         if ($h1Cnt > $H1_tag_count_for_sim) {
                             $H1_tag_count_for_sim = $h1Cnt;
                         }
                     } else {
                         if ($H1_tag_count_for_sim < 1) {
                             $H1_tag_count_for_sim = 1;
                         }
                     }
                 }
                 if (in_array('H2_Tags', $selected_columns) && $pars_atr_for_sim['HTags']['h2'] && trim($pars_atr_for_sim['HTags']['h2'])) {
                     $H2 = $pars_atr_for_sim['HTags']['h2'];
                     if (is_array($H2)) {
                         $h2Cnt = count($H2);
                         if ($h2Cnt > $H2_tag_count_for_sim) {
                             $H2_tag_count_for_sim = $h2Cnt;
                         }
                     } elseif ($H2_tag_count_for_sim < 1) {
                         $H2_tag_count_for_sim = 1;
                     }
                 }
                 if (trim($arr['short_seo_phrases']) != 'None') {
                     $shortArr = unserialize($arr['short_seo_phrases']);
                     if ($shortArr) {
                         $shortString = '';
                         foreach ($shortArr as $value) {
                             $shortString .= $value['ph'] . "\r\n";
                         }
                         $arr['short_seo_phrases'] = trim($shortString);
                     }
                 }
                 if (trim($arr['long_seo_phrases']) != 'None') {
                     $longArr = unserialize($arr['long_seo_phrases']);
                     if ($longArr) {
                         $longString = '';
                         foreach ($longArr as $value) {
                             $longString .= $value['ph'] . "\r\n";
                         }
                         $arr['long_seo_phrases'] = trim($longString);
                     }
                 }
                 if (in_array('imp_data_id', $selected_columns)) {
                     $arr['imp_data_id'] = $pars_atr['parsed_attributes']['item_id'] ? $pars_atr['parsed_attributes']['item_id'] : ' ';
                 }
                 //model
                 if (in_array('model', $selected_columns)) {
                     $arr['model'] = $pars_atr['parsed_attributes']['model'] ? $pars_atr['parsed_attributes']['model'] : '';
                 }
                 //meta keywords
                 if (in_array('Meta_Keywords', $selected_columns)) {
                     $parsed_meta_keywords_unserialize_val = "";
                     if ($pars_atr['parsed_meta']['keywords']) {
                         $Meta_Keywords_un = "";
                         $cnt_meta = explode(',', $pars_atr['parsed_meta']['keywords']);
                         $cnt_meta_count = count($cnt_meta);
                         $_count_meta = 0;
                         foreach ($cnt_meta as $cnt_m) {
                             $cnt_m = trim($cnt_m);
                             if ($row->Short_Description || $row->Long_Description) {
                                 $_count_meta = $this->keywords_appearence($row->Long_Description . $row->Short_Description, $cnt_m);
                                 $_count_meta_num = round($_count_meta * $cnt_meta_count / ($row->long_description_wc + $row->short_description_wc) * 100, 2) . "%";
                                 $Meta_Keywords_un .= $cnt_m . "  - " . $_count_meta_num . ", ";
                             }
                         }
                         $parsed_meta_keywords_unserialize_val = $Meta_Keywords_un;
                     }
                     $arr['meta_keywords'] = $parsed_meta_keywords_unserialize_val ? $parsed_meta_keywords_unserialize_val : '';
                 }
                 //meta description
                 if (in_array('Meta_Description', $selected_columns)) {
                     if ($pars_atr['parsed_meta']['description'] && $pars_atr['parsed_meta']['description'] != '') {
                         $arr['meta_description'] = $pars_atr['parsed_meta']['description'];
                         $arr['Meta_Description_Count'] = count(explode(" ", $pars_atr['parsed_meta']['description']));
                     } else {
                         if ($pars_atr['parsed_meta']['Description'] && $pars_atr['parsed_meta']['Description'] != '') {
                             $arr['meta_description'] = $pars_atr['parsed_meta']['Description'];
                             $arr['Meta_Description_Count'] = count(explode(" ", $pars_atr['parsed_meta']['Description']));
                         }
                     }
                 }
                 //custom keywords
                 if (in_array('Custom_Keywords_Short_Description', $selected_columns) || in_array('Custom_Keywords_Long_Description', $selected_columns)) {
                     $custom_keywords = $this->custom_keywords($row->imported_data_id, $row->Long_Description, $row->long_description_wc, $row->Short_Description, $row->short_description_wc);
                     if (in_array('Custom_Keywords_Short_Description', $selected_columns)) {
                         $arr['Custom_Keywords_Short_Description'] = $custom_keywords['Custom_Keywords_Short'];
                     }
                     if (in_array('Custom_Keywords_Long_Description', $selected_columns)) {
                         $arr['Custom_Keywords_Long_Description'] = $custom_keywords['Custom_Keywords_Long'];
                     }
                 }
                 //loaded_in_seconds
                 if (in_array('Page_Load_Time', $selected_columns)) {
                     $arr['Page_Load_Time'] = $pars_atr['parsed_attributes']['loaded_in_seconds'] !== false ? $pars_atr['parsed_attributes']['loaded_in_seconds'] : '';
                 }
                 if (in_array('column_external_content', $selected_columns)) {
                     $arr['column_external_content'] = $this->column_external_content($pars_atr['parsed_attributes']['cnetcontent'], $pars_atr['parsed_attributes']['webcollage']);
                 }
                 if (in_array('column_features', $selected_columns)) {
                     $arr['column_features'] = $pars_atr['parsed_attributes']['feature_count'] !== false ? $pars_atr['parsed_attributes']['feature_count'] : 0;
                 }
                 if (in_array('column_reviews', $selected_columns)) {
                     $arr['column_reviews'] = $pars_atr['parsed_attributes']['review_count'] !== false ? $pars_atr['parsed_attributes']['review_count'] : 0;
                 }
                 if (in_array('average_review', $selected_columns)) {
                     $arr['average_review'] = $pars_atr['parsed_attributes']['average_review'] !== false ? $pars_atr['parsed_attributes']['average_review'] : '-';
                 }
                 if (in_array('title_seo_phrases', $selected_columns)) {
                     $arr['title_seo_phrases'] = $str_title_long_seo;
                 }
                 if (in_array('title_seo_phrases_f', $selected_columns)) {
                     $arr['title_seo_phrases'] = $str_title_long_seo;
                 }
                 if (in_array('price_diff', $selected_columns)) {
                     $price_diff = unserialize($row->price_diff);
                     if ($price_diff) {
                         $own_price = floatval($price_diff['own_price']);
                         $own_site = str_replace('www.', '', $price_diff['own_site']);
                         $own_site = str_replace('www1.', '', $own_site);
                         $price_diff_res = $own_site . " - \$" . number_format($price_diff['own_price'], 2);
                         $flag_competitor = false;
                         $lim = count($price_diff['competitor_customer']);
                         for ($i = 0; $i < $lim; $i++) {
                             if ($customer_url["host"] != $price_diff['competitor_customer'][$i]) {
                                 if ($own_price > floatval($price_diff['competitor_price'][$i])) {
                                     $competitor_site = str_replace('www.', '', $price_diff['competitor_customer'][$i]);
                                     $competitor_site = str_replace('www.', '', $competitor_site);
                                     $price_diff_res .= "\r\n" . $competitor_site . " - \$" . number_format($price_diff['competitor_price'][$i], 2);
                                 }
                             }
                         }
                         $arr['price_diff'] = $price_diff_res;
                     } else {
                         $arr['price_diff'] = '';
                     }
                 }
                 //similar items
                 $f_count1 = 0;
                 for ($i = 1; $i <= $max_similar_item_count; $i++) {
                     if ($i < 2) {
                         if (isset($sim[$i - 1]->column_features)) {
                             $f_count1 = $sim[$i - 1]->column_features;
                         } else {
                             $f_count1 = 0;
                         }
                     }
                     if (in_array('gap', $selected_columns)) {
                         $arr['Gap analysis'] = '';
                         if (isset($sim[$i - 1]) && ($sim[$i - 1]->long_description_wc || $sim[$i - 1]->short_description_wc) && $sim[$i - 1]->short_description_wc + $sim[$i - 1]->long_description_wc < 100) {
                             $totoal = $sim[$i - 1]->short_description_wc + $sim[$i - 1]->long_description_wc;
                             $arr['Gap analysis'] .= 'Competitor total product description length only' . $totoal . 'words, ';
                         }
                         if ($arr['column_features'] && $f_count1 && $f_count1 > $arr['column_features']) {
                             $x = $f_count1 - $arr['column_features'];
                             $arr['Gap analysis'] .= 'Competitor has ' . $x . ' features listed, ';
                         }
                         if (!$sim[$i - 1]->den_for_gap) {
                             $arr['Gap analysis'] .= "Competitor is not keyword optimized, ";
                         }
                         $arr['Gap analysis'] = rtrim($arr['Gap analysis'], ", ");
                     }
                     if (in_array('Duplicate_Content', $selected_columns)) {
                         if ($row->Short_Description) {
                             $short_desc_1 = $row->Short_Description;
                         } else {
                             $short_desc_1 = '';
                         }
                         if ($row->Long_Description) {
                             $long_desc_1 = $row->Long_Description;
                         } else {
                             $long_desc_1 = '';
                         }
                         $desc_1 = $short_desc_1 . ' ' . $long_desc_1;
                         if ($row->similar_items[0]->Short_Description) {
                             $short_desc_2 = $row->similar_items[0]->Short_Description;
                         } else {
                             $short_desc_2 = '';
                         }
                         if ($row->similar_items[0]->Long_Description) {
                             $long_desc_2 = $row->similar_items[0]->Long_Description;
                         } else {
                             $long_desc_2 = '';
                         }
                         $desc_2 = $short_desc_2 . ' ' . $long_desc_2;
                         similar_text($desc_1, $desc_2, $percent);
                         $percent = number_format($percent, 2);
                         $arr['Duplicate_Content'] = $percent . ' %';
                     }
                     if (in_array('imp_data_id', $selected_columns)) {
                         if ($i < 2) {
                             $arr['imp_data_id'] = $sim[$i - 1]->imported_data_id ? $sim[$i - 1]->imported_data_id : ' - ';
                         } else {
                             $arr['imp_data_id' . $i] = $sim[$i - 1]->imported_data_id ? $sim[$i - 1]->imported_data_id : ' - ';
                         }
                     }
                     if (in_array('model', $selected_columns)) {
                         $arr['Model (' . $i . ")"] = $sim[$i - 1]->model ? $sim[$i - 1]->model : '';
                     }
                     if (in_array('product_name', $selected_columns)) {
                         $arr['Product Name (' . $i . ")"] = $sim[$i - 1]->product_name ? $sim[$i - 1]->product_name : ' - ';
                     }
                     if (in_array('url', $selected_columns)) {
                         $arr['Url (' . $i . ")"] = $sim[$i - 1]->url ? $sim[$i - 1]->url : '';
                     }
                     if (in_array('Short_Description', $selected_columns)) {
                         $arr['Short Description (' . $i . ")"] = $sim[$i - 1]->Short_Description ? $sim[$i - 1]->Short_Description : '';
                     }
                     if (in_array('short_description_wc', $selected_columns)) {
                         $arr['Short Desc # Words (' . $i . ")"] = $sim[$i - 1]->short_description_wc ? $sim[$i - 1]->short_description_wc : '';
                     }
                     if (in_array('Long_Description', $selected_columns)) {
                         $arr['Long_Description (' . $i . ")"] = $sim[$i - 1]->Long_Description ? $sim[$i - 1]->Long_Description : '';
                     }
                     if (in_array('long_description_wc', $selected_columns)) {
                         $arr['Long Desc # Words (' . $i . ")"] = $sim[$i - 1]->long_description_wc ? $sim[$i - 1]->long_description_wc : '';
                     }
                     if (in_array('Meta_Keywords', $selected_columns)) {
                         $arr['Meta_Keywords(' . $i . ")"] = $sim[$i - 1]->Meta_Keywords ? $sim[$i - 1]->Meta_Keywords : '';
                     }
                     if (in_array('Meta_Description', $selected_columns)) {
                         $arr['Meta_Description (' . $i . ")"] = $sim[$i - 1]->Meta_Description ? $sim[$i - 1]->Meta_Description : '';
                         $arr['Meta Desc Words (' . $i . ")"] = $sim[$i - 1]->Meta_Description_Count ? $sim[$i - 1]->Meta_Description_Count : '';
                     }
                     if (in_array('Page_Load_Time', $selected_columns)) {
                         $arr['Page Load Time (' . $i . ")"] = $sim[$i - 1]->Page_Load_Time ? $sim[$i - 1]->Page_Load_Time : ' - ';
                     }
                     if (in_array('column_features', $selected_columns)) {
                         $arr['column_features(' . $i . ")"] = $sim[$i - 1]->column_features ? $sim[$i - 1]->column_features : 0;
                     }
                     if (in_array('column_external_content', $selected_columns)) {
                         $arr['column_external_content(' . $i . ")"] = $sim[$i - 1]->column_external_content ? $sim[$i - 1]->column_external_content : '';
                     }
                     if (in_array('column_reviews', $selected_columns)) {
                         $arr['column_reviews(' . $i . ")"] = $sim[$i - 1]->review_count ? $sim[$i - 1]->review_count : 0;
                     }
                     if (in_array('average_review', $selected_columns)) {
                         $arr['average_review(' . $i . ")"] = $sim[$i - 1]->average_review ? $sim[$i - 1]->average_review : ' - ';
                     }
                     if (in_array('title_seo_phrases', $selected_columns)) {
                         $arr['title_seo_phrases(' . $i . ")"] = $sim[$i - 1]->title_seo_phrases ? $sim[$i - 1]->title_seo_phrases : ' - ';
                     }
                     if (in_array('title_seo_phrases_f', $selected_columns)) {
                         $arr['title_seo_phrases_f(' . $i . ")"] = $sim[$i - 1]->title_seo_phrases ? $sim[$i - 1]->title_seo_phrases : ' - ';
                     }
                     // HTags for similar
                     $HTags_for_similar = unserialize($sim[$i - 1]->HTags);
                     if (in_array('H1_Tags', $selected_columns) && $HTags_for_similar['h1'] && $HTags_for_similar['h1'] != '') {
                         $H1 = $HTags_for_similar['h1'];
                         if (is_array($H1)) {
                             $j = 0;
                             foreach ($H1 as $k => $h1) {
                                 if ($j < 2) {
                                     $arr['H1_Tags' . $k . '(' . $i . ')'] = $h1;
                                     $arr['H1_Tags_Count' . $k . '(' . $i . ')'] = strlen($h1);
                                     $j++;
                                 }
                             }
                         } else {
                             $arr['H1_Tags0 (' . $i . ')'] = $H1;
                             $arr['H1_Tags_Count0 (' . $i . ')'] = strlen($H1);
                         }
                         if ($H1_tag_count_for_sim > 0) {
                             if ($H1_tag_count_for_sim > 2) {
                                 $H1_tag_count_for_sim = 2;
                             }
                             for ($k = 0; $k < $H1_tag_count_for_sim; $k++) {
                                 if (!$arr['H1_Tags' . $k . '(' . $i . ')']) {
                                     $arr['H1_Tags' . $k . '(' . $i . ')'] = '';
                                     $arr['H1_Tags_Count' . $k . '(' . $i . ')'] = ' ';
                                 }
                             }
                         }
                     }
                     if (in_array('H2_Tags', $selected_columns) && $HTags_for_similar['h2'] && $HTags_for_similar['h2'] != '') {
                         $H2 = $HTags_for_similar['h2'];
                         if (is_array($H2)) {
                             $j = 0;
                             foreach ($H2 as $k => $h2) {
                                 if ($j < 2) {
                                     $arr['H2_Tags' . $k . '(' . $i . ')'] = $h2;
                                     $arr['H2_Tags_Count' . $k . '(' . $i . ')'] = strlen($h2);
                                     $j++;
                                 }
                             }
                         } else {
                             $arr['H2_Tags0 (' . $i . ')'] = $H2;
                             $arr['H2_Tags_Count0 (' . $i . ')'] = strlen($H2);
                         }
                         if ($H2_tag_count_for_sim > 0) {
                             if ($H2_tag_count_for_sim > 2) {
                                 $H2_tag_count_for_sim = 2;
                             }
                             for ($k = 0; $k < $H2_tag_count_for_sim; $k++) {
                                 if (!$arr['H2_Tags' . $k . '(' . $i . ')']) {
                                     $arr['H2_Tags' . $k . '(' . $i . ')'] = '';
                                     $arr['H2_Tags_Count' . $k . '(' . $i . ')'] = ' ';
                                 }
                             }
                         }
                     } else {
                         if ($H2_tag_count_for_sim > 2) {
                             $H2_tag_count_for_sim = 2;
                         }
                         for ($k = 0; $k < $H2_tag_count_for_sim; $k++) {
                             if (!$arr['H2_Tags' . $k . '(' . $i . ')']) {
                                 $arr['H2_Tags' . $k . '(' . $i . ')'] = '';
                                 $arr['H2_Tags_Count' . $k . '(' . $i . ')'] = ' ';
                             }
                         }
                     }
                 }
                 $res_array[] = $arr;
             }
         }
         for ($i = 1; $i <= $max_similar_item_count; $i++) {
             if (in_array('gap', $selected_columns)) {
                 $line[] = 'Gap Analysis';
             }
             if (in_array('Duplicate_Content', $selected_columns)) {
                 $line[] = 'Duplicate Content';
             }
             if (in_array('imp_data_id', $selected_columns)) {
                 $line[] = "Item Id(" . ($i + 1) . ")";
             }
             if (in_array('model', $selected_columns)) {
                 $line[] = "Model(" . ($i + 1) . ")";
             }
             if (in_array('product_name', $selected_columns)) {
                 $line[] = "Product Name(" . ($i + 1) . ")";
             }
             if (in_array('url', $selected_columns)) {
                 $line[] = "Url(" . ($i + 1) . ")";
             }
             if (in_array('Short_Description', $selected_columns)) {
                 $line[] = "Short Description(" . ($i + 1) . ")";
             }
             if (in_array('short_description_wc', $selected_columns)) {
                 $line[] = "Short Desc # Words(" . ($i + 1) . ")";
             }
             if (in_array('Long_Description', $selected_columns)) {
                 $line[] = "Long Description(" . ($i + 1) . ")";
             }
             if (in_array('long_description_wc', $selected_columns)) {
                 $line[] = " Long Desc # Words(" . ($i + 1) . ")";
             }
             if (in_array('Meta_Keywords', $selected_columns)) {
                 $line[] = "Meta Keywords (" . ($i + 1) . ") - Keyword Density";
             }
             if (in_array('Meta_Description', $selected_columns)) {
                 $line[] = "Meta Description(" . ($i + 1) . ")";
                 $line[] = " Meta Desc Words(" . ($i + 1) . ")";
             }
             if (in_array('Page_Load_Time', $selected_columns)) {
                 $line[] = "Page Load Time(" . ($i + 1) . ")";
             }
             if (in_array('column_features', $selected_columns)) {
                 $line[] = "Features(" . ($i + 1) . ")";
             }
             if (in_array('column_external_content', $selected_columns)) {
                 $line[] = "Third Party Content(" . ($i + 1) . ")";
             }
             if (in_array('column_reviews', $selected_columns)) {
                 $line[] = "Reviews(" . ($i + 1) . ")";
             }
             if (in_array('average_review', $selected_columns)) {
                 $line[] = "Avg Review(" . ($i + 1) . ")";
             }
             if (in_array('title_seo_phrases', $selected_columns)) {
                 $line[] = "Title Keywords(" . ($i + 1) . ")";
             }
             if (in_array('title_seo_phrases_f', $selected_columns)) {
                 $line[] = "Title Keywords(" . ($i + 1) . ")";
             }
             if (in_array('H1_Tags', $selected_columns)) {
                 if ($H1_tag_count_for_sim > 0) {
                     for ($k = 1; $k <= $H1_tag_count_for_sim; $k++) {
                         $line[] = "H1_tag ({$k})    (" . ($i + 1) . ")";
                         $line[] = "Chars ({$k})    (" . ($i + 1) . ")";
                     }
                 }
             }
             if (in_array('H2_Tags', $selected_columns)) {
                 if ($H2_tag_count_for_sim > 0) {
                     for ($k = 1; $k <= $H2_tag_count_for_sim; $k++) {
                         $line[] = "H2_tag ({$k})    (" . ($i + 1) . ")";
                         $line[] = "Chars ({$k})    (" . ($i + 1) . ")";
                     }
                 }
             }
         }
         //deleting empty Short_Description, Long_Description, short_descripition_wc, long_description_wc colomns
         //filterBySummaryCriteria
         if (in_array('Short_Description', $selected_columns)) {
             $short_description_count = 0;
             if (isset($max_similar_item_count)) {
                 $short_description_count_1 = 0;
             }
         }
         if (in_array('Long_Description', $selected_columns)) {
             $long_description_count = 0;
             if (isset($max_similar_item_count)) {
                 $long_description_count_1 = 0;
             }
         }
         if (in_array('short_description_wc', $selected_columns)) {
             $short_description_wc_count = 0;
             if (isset($max_similar_item_count)) {
                 $short_description_wc_count_1 = 0;
             }
         }
         if (in_array('long_description_wc', $selected_columns)) {
             $long_description_wc_count = 0;
             if (isset($max_similar_item_count)) {
                 $long_description_wc_count_1 = 0;
             }
         }
         $resArrCnt = count($res_array);
         foreach ($res_array as $key => $value) {
             if (in_array('Short_Description', $selected_columns)) {
                 if (!$value['Short_Description']) {
                     $short_description_count++;
                 }
                 if (isset($max_similar_item_count)) {
                     if (!$value['Short Description (1)']) {
                         $short_description_count_1++;
                     }
                 }
                 if ($short_description_count == $resArrCnt) {
                     unset($res_array[$key]['Short_Description']);
                     unset($line['Short_Description']);
                 }
                 if (isset($max_similar_item_count)) {
                     if ($short_description_count_1 == $resArrCnt) {
                         unset($res_array[$key]['Short Description (1)']);
                         unset($line['18']);
                     }
                 }
             }
             if (in_array('Long_Description', $selected_columns)) {
                 if (!$value['Long_Description']) {
                     $long_description_count++;
                 }
                 if (isset($max_similar_item_count)) {
                     if (!$value['Long_Description (1)']) {
                         $long_description_count_1++;
                     }
                 }
                 if ($long_description_count == $resArrCnt) {
                     unset($res_array[$key]['Long_Description']);
                     unset($line['Long_Description']);
                 }
                 if (isset($max_similar_item_count)) {
                     if ($long_description_count_1 == $resArrCnt) {
                         unset($res_array[$key]['Long_Description (1)']);
                         unset($line['20']);
                     }
                 }
             }
             if (in_array('short_description_wc', $selected_columns)) {
                 if ($value['short_description_wc'] == 0) {
                     $short_description_wc_count++;
                 }
                 if (isset($max_similar_item_count)) {
                     if ($value['Short Desc # Words (1)'] == 0) {
                         $short_description_wc_count_1++;
                     }
                 }
                 if ($short_description_wc_count == $resArrCnt) {
                     unset($res_array[$key]['short_description_wc']);
                     unset($line['short_description_wc']);
                 }
                 if (isset($max_similar_item_count)) {
                     if ($short_description_wc_count_1 == $resArrCnt) {
                         unset($res_array[$key]['Short Desc # Words (1)']);
                         unset($line['19']);
                     }
                 }
             }
             if (in_array('long_description_wc', $selected_columns)) {
                 if ($value['long_description_wc'] == 0) {
                     $long_description_wc_count++;
                 }
                 if (isset($max_similar_item_count)) {
                     if ($value['Long Desc # Words (1)'] == 0) {
                         $long_description_wc_count_1++;
                     }
                 }
                 if ($long_description_wc_count == $resArrCnt) {
                     unset($res_array[$key]['long_description_wc']);
                     unset($line['long_description_wc']);
                 }
                 if (isset($max_similar_item_count)) {
                     if ($long_description_wc_count_1 == $resArrCnt) {
                         unset($res_array[$key]['Long Desc # Words(1)']);
                         unset($line['21']);
                     }
                 }
             }
         }
         $this->load->helper('csv');
         $results['count'] = count($res_array);
         $results['data'] = array_to_csv($res_array);
         unset($res_array);
     }
     // Commmented since we don't need line 'url' as first row
     // $line = array( $line );
     // $results['data'] = array_to_csv($line) . $results['data'];
     if ($type == 'save') {
         $this->number_of_items = ++$results['count'];
         return $results['data'];
     }
     if ($results['count'] == 0) {
         $result = array('type' => 'processing_error', 'message' => 'We are sorry, this batch and date can\'t be exported at this time');
         $this->response($result, 400);
     } else {
         $this->load->helper('assess');
         // Generating unique CSV filename
         $path = 'webroot/temp/';
         $filename = getUniqueFilename($path, $batch_name . "_(" . date("Y-m-d_H-i") . ')', 'csv');
         file_put_contents('./' . $path . $filename, $results['data']);
         unset($results);
         $result = array('status' => 'success', 'link' => base_url($path . $filename));
         // Getting current user data
         $user = $this->ion_auth->get_user_data();
         // Sending email
         $this->load->library('email');
         $this->email->from('*****@*****.**', 'Administrator');
         $this->email->to($user['email']);
         $this->email->subject('Export completed');
         $this->email->message('Your export was completed succesfully. You can download it here ' . base_url($path . $filename));
         $this->email->send();
     }
     $this->response($result);
 }
 /**
  * Download function
  * Downloading file and checking md5 sum. Try up to 3 times if md5 wrong
  */
 public function download($external_file, $external_md5 = FALSE)
 {
     // Checking if file already on current server
     $url = parse_url($external_file);
     if ($url['host'] == $_SERVER['SERVER_NAME']) {
         return $external_file;
     }
     $this->CI->load->helper('assess');
     // Getting internal unique filename
     $internal_file = $this->working_path . getUniqueFilename($this->working_path, 'import ' . date("Y-m-d H-i"), 'exp');
     // Trying to download and check md5 sum
     $try_count = 0;
     do {
         if ($try_count++ >= 3) {
             return FALSE;
         }
         exec('wget -O ' . escapeshellarg($internal_file) . ' ' . escapeshellarg($external_file));
         $internal_md5 = substr(shell_exec('md5sum -b ' . escapeshellarg($internal_file)), 0, 32);
     } while ($external_md5 && $external_md5 != $internal_md5);
     return $internal_file;
 }
Ejemplo n.º 3
0
function getUniqueUploadedFilename($fname)
{
    global $overwriteUploadedTorrents;
    $fname = getUploadsPath() . "/" . $fname;
    return $overwriteUploadedTorrents ? $fname : getUniqueFilename($fname);
}
Ejemplo n.º 4
0
 /**
  * Creating files for each brand/category and filter pair. Returning link to zip file
  */
 public function exportBrandsAndFilters()
 {
     $this->load->model('products_filters');
     $chronicle_str = $this->input->post('chronicle');
     $superFilter = $this->input->post('super_filter');
     $filters = $this->input->post('filters');
     sort($filters);
     $filters_str = implode(',', $filters);
     // If chronicle or filters are empty - returning failure
     if (empty($chronicle_str) || empty($filters)) {
         return $this->output->set_content_type('application/json')->set_output(json_encode(array('success' => FALSE, 'message' => 'Not initialized')));
     }
     $brands = $this->input->post('brands');
     // Brands should be only numeric
     foreach ($brands as $i => &$val) {
         if (!is_numeric($val)) {
             unset($brands[$i]);
         }
     }
     sort($brands);
     $categories = $this->input->post('categories');
     // Categories should be only numeric
     foreach ($categories as $i => &$val) {
         if (!is_numeric($val)) {
             unset($categories[$i]);
         }
     }
     sort($categories);
     // If we don't have category and brands - returning failure
     if (empty($brands) && empty($categories)) {
         $this->output->set_content_type('application/json')->set_output(json_encode(array('success' => FALSE, 'message' => 'Empty data')));
     }
     // Initial data
     $result = array('success' => FALSE);
     $prority_filters = array();
     if ($this->session->userdata['dateformat'] == 1) {
         $date = date('m-d-Y_H-i');
     } elseif ($this->session->userdata['dateformat'] == 2) {
         $date = date('d-m-Y_H-i');
     }
     $path = 'webroot/temp/';
     // Getting name for each filter_id. Instead of 'skus_fewer_50_product_content' we use 'skusFewer50ProductContent'
     $filter_names = array();
     $this->load->model('filters_values');
     foreach ($filters as $filter_key => &$filter_id) {
         if ($filter_id == 'medium_priority_filter' || $filter_id == 'high_priority_filter' || $filter_id == 'skus_medium_priority_filter' || $filter_id == 'skus_high_priority_filter') {
             unset($filters[$filter_key]);
             $prority_filters[$filter_id] = array();
             $filter_name = str_replace(array('skus_', '_filter'), '', $filter_id);
         } else {
             $filter_id = (int) $filter_id;
             $filter_name = $this->products_filters->get_filter_name_by_filter_id($filter_id);
         }
         $filter_names[$filter_id] = str_replace(' ', '', ucwords(str_replace('_', ' ', $filter_name)));
     }
     if (count($prority_filters)) {
         $prority_filters = array_intersect_key(AssessHelper::getPrioityFiltersList($this->ion_auth->get_user_id(), true), $prority_filters);
     }
     // Getting batch information
     $this->load->model('batches_model');
     $batch = $this->batches_model->getBatchByChronicle($chronicle_str);
     // Decoding chronicle if that was json string
     $chronicle = json_decode($chronicle_str);
     sort($chronicle);
     if (is_int($chronicle)) {
         $chronicle = (string) $chronicle;
     }
     // Checking if we have category or brands filters
     $result = array('success' => TRUE);
     $this->load->model('assess_results');
     $this->load->library('zip');
     $this->load->helper('csv');
     $this->load->helper('assess');
     if ($categories || $brands) {
         // Getting URLs for all pairs
         $data = $this->assess_results->getExportBrandsCategoriesFilters($chronicle, $brands, $categories, $filters, $superFilter, $prority_filters);
         // Loading helper with functions "toAscii" and "getUniqueFilename"
         // Creating in memory zip files for each pair
         foreach ($data as $brand => &$val) {
             $datas_for_csv = array();
             $columns_for_csv = array();
             $max_length = 0;
             // In zip russian symbols looks ok in CP866 encoding
             $name = str_replace(array(' ', '_', '-'), '_', iconv('UTF-8', 'CP866', $batch->title) . '_' . $brand . '.csv');
             foreach ($val as $filter_id => &$rows) {
                 array_unshift($rows, $filter_names[$filter_id]);
                 if (count($rows) > $max_length) {
                     $max_length = count($rows);
                 }
                 $columns_for_csv[] = $rows;
             }
             foreach ($columns_for_csv as $key => $value) {
                 for ($i = 0; $i < $max_length; ++$i) {
                     if (!isset($datas_for_csv[$i])) {
                         $datas_for_csv[$i] = array();
                     }
                     $v = isset($value[$i]) ? $value[$i] : '';
                     array_push($datas_for_csv[$i], $v);
                 }
             }
             $this->zip->add_data(iconv('UTF-8', 'CP866', $name), array_to_csv($datas_for_csv, '', false));
         }
     }
     /**
      *for bug 1661
      *@author Hermine Hakobyan
      **/
     $batch_all_data = $this->assess_results->getExportBrandsCategoriesFilters($chronicle, array(), array(), $filters, $superFilter, $prority_filters);
     foreach ($batch_all_data as $brand => &$val) {
         $datas_for_csv = array();
         $columns_for_csv = array();
         $max_length = 0;
         // In zip russian symbols looks ok in CP866 encoding
         $name = str_replace(array(' ', '_', '-'), '_', iconv('UTF-8', 'CP866', $batch->title) . '.csv');
         foreach ($val as $filter_id => &$rows) {
             array_unshift($rows, $filter_names[$filter_id]);
             if (count($rows) > $max_length) {
                 $max_length = count($rows);
             }
             $columns_for_csv[] = $rows;
         }
         foreach ($columns_for_csv as $key => $value) {
             for ($i = 0; $i < $max_length; ++$i) {
                 if (!isset($datas_for_csv[$i])) {
                     $datas_for_csv[$i] = array();
                 }
                 $v = isset($value[$i]) ? $value[$i] : '';
                 array_push($datas_for_csv[$i], $v);
             }
         }
         $this->zip->add_data(iconv('UTF-8', 'CP866', $name), array_to_csv($datas_for_csv, '', false));
     }
     //End of bug 1661
     $zip_file = $path . getUniqueFilename($path, $batch->title . '_' . $date, 'zip');
     // Saving zip on disk
     $this->zip->archive($zip_file);
     // Getting current user data
     $user = $this->ion_auth->get_user_data();
     // Sending email
     $this->load->library('email');
     $this->email->from('*****@*****.**', 'Administrator');
     $this->email->to($user['email']);
     $this->email->subject('Export completed');
     $this->email->message('Your export was completed succesfully. You can download it here ' . base_url($zip_file));
     $this->email->send();
     // Adding zip fullpath to result
     $result['link'] = base_url($zip_file);
     // Zipping files
     $this->output->set_content_type('application/json')->set_output(json_encode($result));
 }