public function action_sua($story_id) { $this->template->title = __('Sửa bài viết: xung - hợp cung'); $this->template->section_title = __('Sửa bài viết: xung - hợp cung'); $data = array(); $story = Model_Horoscope_XungHopBLL::getInstance()->find($story_id); if ($story) { if (Request::$method == 'POST') { // print_r($_POST); // die(); $post = $story->validate_update($_POST); if ($post->check()) { //begin save $post_values = $post->as_array(); $old_alias = $story->alias; // //alias changed => image changed => directory changed => images in content not get :( $story->hinh_anh = $post_values['hinh_anh']; $story->alias = $post_values['alias']; $story->cung_1 = $post_values['cung_1']; $story->cung_2 = $post_values['cung_2']; $story->tom_tat = trim($post_values['tom_tat']); $story->noi_dung = $post_values['noi_dung']; $story->kiem_tra = true; $story->save(); //print_r($img); // Create a DOM object if ($old_alias != $post_values['alias']) { //remove old folder (if existed when update) @rmdir('assets/horoscope/xung-hop/' . $old_alias . '/'); require_once Kohana::find_file('classes', 'vendor/simple_html_dom'); $html2 = new simple_html_dom(); $html2->load($story->noi_dung); $images = $html2->find('img'); if ($images) { $i = 1; foreach ($images as $image) { unset($image->onclick); $path = 'assets/horoscope/xung-hop/' . $story->alias . '/'; $file_name = 'anh_' . $i; $get_file = Vendor_Crawler::get_file_from_url_by_curl($image->src, $save_to_path = $path, $file_name_to_set = $file_name); if (filesize($get_file) == 0) { unset($image); } else { $image->src = '/' . $get_file; } $i++; } } $story->noi_dung = $html2->save(); $html2->clear(); unset($html2); $story->save(); } Request::instance()->redirect('admin/horoscope_xunghop/index'); } else { $_POST = $post->as_array(); #Affects errors for further display $data['errors'] = $post->errors(); } } $data['story'] = $story->toArray(); $this->template->content = View::factory('horoscope/admin/xung-hop/sua', $data); } else { Request::instance()->redirect('admin/horoscope_xunghop/index'); } }
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; } }
private function _process_tmp_content($_HTML_CONTENT, $_URI, $_IMG, $_TITLE) { 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); //begin find elements $horoscope = new Model_Horoscope(); $summary = $html->find('h2[class="detail_sapo"]', 0); if ($summary) { $sum_tmp = trim($summary->plaintext); $sum_tmp = str_replace(' ', ' ', $sum_tmp); $sum_tmp = str_replace(array('(2sao) -', '(2sao)-', '(2Sao) -', '(2Sao)-'), '', $sum_tmp); $horoscope->summary = trim($sum_tmp); unset($sum_tmp); } else { $horoscope->summary = 'not-set'; } unset($summary); $content = $html->find('div[class="detail_content"]', 0); if ($content) { $content_arr = $content->innertext; //remove word format $content_arr = preg_replace('/(<!--.+?-->)/s', '', $content_arr); //remove any comments //$content_arr = preg_replace('/<script.*>.*<\/script>/s', '', $content_arr); //remove any comments $content_arr = preg_replace('@<a[^>]*>(.*)</a>@ismUx', '$1', $content_arr); //$content_arr = preg_replace('/<meta.*>/i', '', $content_arr); //$content_arr = preg_replace('/<link.*>/i', '', $content_arr); $content_arr = preg_replace('/<style><\\/style>/ismxU', '', $content_arr); $content_arr = str_replace(array("\r\n", "\r", "\n", "\t"), '', $content_arr); //$content_arr = preg_replace('/<link[ ]rel="File-List"[ ]href=".*">/i', '', $content_arr); $content_arr = trim($content_arr); $horoscope->content = $content_arr; } unset($content); $date_create = $html->find('div[class="detail_date"]', 0); if ($date_create) { $d = $date_create->plaintext; //Thứ hai, 13/09/2010 23:25 //get date :D list($thu, $dnt) = explode(',', $d); //$dnt = 13/09/2010 23:25 $dnt = trim($dnt); list($ngay_thang_nam, $gio) = explode(' ', $dnt); list($ngay, $thang, $nam) = explode('/', $ngay_thang_nam); $str_date = $nam . "-" . $thang . "-" . $ngay . " " . $gio . ":00"; $horoscope->date_created = strtotime($str_date); } else { //set to current $horoscope->date_created = time(); } unset($date_create); //get img and save to local $path = 'assets/horoscope/images/'; $r = Vendor_Crawler::get_file_from_url_by_curl($_IMG, $path, $file_name_to_set = ''); if ($r != false) { //$this->_print_to_console(__("DONE: " . $r)); $horoscope->img_thumb = "/" . $r; } else { $horoscope->img_thumb = $_IMG; //$this->_print_to_console(__("<--- CANT GET FILE --->")); } $horoscope->title = $_TITLE; $horoscope->source_uri = $_URI; $horoscope->page_path = Vendor_Crawler::toAscii($_TITLE); $horoscope->meta_keys = implode(',', explode(' ', $_TITLE)); $horoscope->meta_desc = $horoscope->summary; $horoscope->save(); if ($horoscope->identifier()) { if (Model_HoroscopeTempBLL::DeleteByURI($_URI) > 0) { $this->_print_to_console(__("DONE: " . $_URI)); } else { $this->_print_to_console(__("<--- FALSE TO DELETE IN TMP TABLE! --->")); } } else { $this->_print_to_console(__("<--- FALSE TO SAVE NEW RECORD! --->")); } $html->clear(); unset($html); } else { $this->_print_to_console(__("<--- NOTHING TO DO --->")); } }