public static function _Process_Recieved_Content($_HTML_CONTENT, $_Cung1, $_Cung2, $_Summary, $_SourceUri, $_LinkId, $_ImageLink) { if ($_HTML_CONTENT != '') { // Create a DOM object require_once Kohana::find_file('classes', 'vendor/simple_html_dom'); $html = new simple_html_dom(); // Load HTML from a string $html->load($_HTML_CONTENT); unset($_HTML_CONTENT); if ($html) { $story = new Model_Horoscope_XungHop(); $ktra = true; if ($_Cung1 == '-' || $_Cung2 == '-') { $ktra = FALSE; } $story->cung_1 = $_Cung1; $story->cung_2 = $_Cung2; $story->alias = $_Cung1 . '_' . $_Cung2; if (self::CheckRecordByAlias($story->alias)) { $story->alias = $_Cung1 . '__' . $_Cung2; } $story->tom_tat = $_Summary; $story->ngay_tao = date("Y-m-d"); $story->url_nguon = $_SourceUri; $story->auto_get = true; //begin find elements #find date post $date = $html->find('div[class="datetime"]', 0); if ($date) { $d = explode(',', $date->plaintext); if (isset($d[1])) { //var_dump($d); //exit; $d1 = explode(' ', trim($d[1])); list($ngay, $thang, $nam) = explode('/', $d1[0]); $story->source_date = date("Y-m-d h:i:s", strtotime($nam . '-' . $thang . '-' . $ngay . ' ' . $d1[1] . ':00')); } else { $story->source_date = date("Y-m-d h:i:s"); } } else { $story->source_date = date("Y-m-d h:i:s"); } //find content $content = $html->find('div[id="content_document"]', 0); if ($content) { $string = $content->innertext; # remove white space $string = str_replace(array("\r\n", "\r", "\n", "\t"), '', $string); $string = preg_replace('/(<!--.+?-->)/s', '', $string); $string = preg_replace('@<a[^>]*>(.*)</a>@ismUx', '$1', $string); $string = preg_replace('/<p[ ]class="pAuthor">.*<\\/p>/ismxU', '', $string); $string = preg_replace('/<p[ ]class="pSource">.*<\\/p>/ismxU', '', $string); $story->noi_dung = $string; $story->kiem_tra = $ktra; $story->save(); if ($story->identifier()) { if ($ktra) { //get image thumb => save to disk => update record in db $path = 'assets/horoscope/xung-hop/' . $story->alias . '/'; $img = Vendor_Crawler::get_file_from_url_by_curl($_ImageLink, $save_to_path = $path, $file_name_to_set = $story->alias . '-thumb'); if ($img) { //check file size, if = 0 -> mean file can't get if (filesize($img) == 0) { @copy('assets/horoscope/thumb_140.jpg', $img); } $story->hinh_anh = '/' . $img; } else { $story->hinh_anh = $_ImageLink; } } else { $story->hinh_anh = $_ImageLink; } if ($ktra != FALSE) { //print_r($img); $html2 = new simple_html_dom(); $html2->load($story->noi_dung); $images = $html2->find('img'); if (count($images) > 0) { for ($i = 0; $i < count($images); $i++) { unset($images[$i]->onclick); $file_name = 'anh_' . $i + 1; $get_file = Vendor_Crawler::get_file_from_url_by_curl($images[$i]->src, $save_to_path = $path, $file_name_to_set = $file_name); if (filesize(ltrim($get_file, '/')) == 0) { unset($images[$i]); } else { $images[$i]->src = '/' . $get_file; } } } $story->noi_dung = $html2->save(); $html2->clear(); unset($html2); } else { $story->hinh_anh = $_ImageLink; } $story->save(); //insert done => update from tmp table Model_Horoscope_XungHopLinkBLL::UpdateRecordStatus($_LinkId); self::_print_to_console('Done: ' . $_SourceUri); } else { self::_print_to_console('Fail:' . $_SourceUri); } } else { self::_print_to_console('-> content not found'); return false; } $html->clear(); unset($html); } else { self::_print_to_console('-> cant load DOM obj'); return false; } } else { self::_print_to_console('-> nothing to do'); return false; } }
/** * create actions */ public function action_create() { $this->template->title = __('Thêm mới bài viết: xung - hợp cung'); $this->template->section_title = __('Thêm mới bài viết: xung - hợp cung'); $data = array(); if (Request::$method == 'POST') { //print_r($_POST); $_story = new Model_Horoscope_XungHop(); #Load the validation rules, filters and callbacks $post = $_story->validate_create($_POST); #Check that all fields are valid. if ($post->check()) { //validate file upload $validate = Validate::factory($_FILES); $validate->rules('hinh_anh', array('Upload::valid' => array(), 'Upload::not_empty' => array(), 'Upload::type' => array('Upload::type' => array('jpg', 'png', 'gif')), 'Upload::size' => array('1M'))); if ($validate->check()) { //begin save $post_values = $post->as_array(); //unset($post); //die(var_dump($post_values)); $_story->alias = $post_values['alias']; $_story->cung_1 = $post_values['cung_1']; $_story->cung_2 = $post_values['cung_2']; //die(print_r($_FILES)); //save(array $file, $filename = NULL, $directory = NULL, $chmod = 0644) $save_dir = 'assets/horoscope/xung-hop/'; if (!file_exists($save_dir)) { @mkdir($save_dir, $mode = 0777, $recursive = true); } $file_name = $_story->alias . '.jpg'; $path = Upload::save($_FILES['hinh_anh'], $_story->alias . '.jpg', $save_dir, 0777); //die(print_r($path)); $_story->hinh_anh = "/" . $save_dir . $file_name; $_story->tom_tat = trim($post_values['tom_tat']); $_story->noi_dung = trim($post_values['noi_dung']); $_story->url_nguon = trim($post_values['url_nguon']); $_story->ngay_tao = date("Y-m-d"); $_story->kiem_tra = 'yes'; $_story->auto_get = false; $_story->source_date = $_story->ngay_tao; //post date format : dd-mm-yyyy (need to convert -> sql style: yyyy-mm-dd) //die(print_r($_story->alias)); $_story->save(); Request::instance()->redirect('admin/horoscope_xunghop/index'); } else { #Repopulate $_POST data $_POST = $post->as_array(); //error $data['errors'] = $validate->errors(); } } else { //die("check done"); #Repopulate $_POST data //$_POST = array_intersect_key($post->as_array(), $_POST); $_POST = $post->as_array(); #Affects errors for further display $data['errors'] = $post->errors(); } } $data['stories'] = Model_Horoscope_CungBLL::GetAllCungForListing(); $this->template->content = View::factory('horoscope/admin/xung-hop/create', $data); }