Created
July 20, 2022 02:47
-
-
Save fuadarradhi/13d5268aaca40c413f992cb9813af962 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Exporter extends CI_Controller { | |
public function __construct(){ | |
parent::__construct(); | |
} | |
public function index(){ | |
// MEDIA | |
$_db_media = $this->db->get('mediamanager')->result(); | |
$_export_media = []; | |
foreach($_db_media as $m){ | |
$detail = unserialize($m->info); | |
$_export_media[$m->name] = [ | |
'old_path' => base_url('uploads/'.$m->name), | |
'new_path' => '/media/'.date_format(date_create($m->upload_at), 'Y.m').'/'.$m->name, | |
'is_image' => $detail['is_image'], | |
'file_ext' => $detail['file_ext'], | |
'file_size' => $detail['file_size'], | |
]; | |
} | |
// NEWS | |
$_db_news = $this->db->query(' | |
SELECT n.news_title, n.news_content, n.news_post_date, n.news_image, n.news_short_desc, nc.category_name | |
FROM news n | |
LEFT JOIN news_category nc ON nc.category_id = n.category_id | |
')->result(); | |
$_export_news = []; | |
foreach($_db_news as $n){ | |
$_export_news[] = [ | |
'title' => $n->news_title, | |
'slug' => $this->slugify($n->news_title), | |
'content' => $this->normalize_content($n->news_content, $_export_media), | |
'date' => date_format(date_create($n->news_post_date), 'Y-m-d'), | |
'thumbnail' => $this->normalize_thumbnail($n->news_image, $_export_media), | |
'excerpt' => $n->news_short_desc, | |
'category_label' => $n->category_name, | |
'category_slug' => $this->slugify($n->category_name), | |
]; | |
} | |
$_db_pages = $this->db->get('page')->result(); | |
$_export_pages = []; | |
foreach($_db_pages as $p){ | |
$_export_pages[] = [ | |
'title' => $p->page_name, | |
'slug' => $this->slugify($p->page_name), | |
'content' => $this->normalize_content($p->page_content, $_export_media), | |
'date' => date_format(date_create($p->page_published_date), 'Y-m-d'), | |
'thumbnail' => $this->normalize_thumbnail($p->page_image, $_export_media), | |
]; | |
} | |
$result = [ | |
'media' => $_export_media, | |
'news' => $_export_news, | |
'page' => $_export_pages, | |
]; | |
echo json_encode($result); | |
} | |
private function normalize_content($content, $medias){ | |
$re = '/<img\s?.*\s?src="([\S]+\/uploads\/([\S]+))"\s.+\/>/m'; | |
preg_match_all($re, $content, $matches, PREG_SET_ORDER, 0); | |
foreach($matches as $match){ | |
if (array_key_exists($match[2], $medias)){ | |
$content = str_replace($match[1], $medias[$match[2]]['new_path'], $content); | |
} | |
} | |
return $content; | |
} | |
private function normalize_thumbnail($thumb, $medias){ | |
if (array_key_exists($thumb, $medias)){ | |
return $medias[$thumb]['new_path']; | |
} | |
return ''; | |
} | |
private function slugify($text) | |
{ | |
$text = preg_replace('~[^\pL\d]+~u', '-', $text); | |
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); | |
$text = preg_replace('~[^-\w]+~', '', $text); | |
$text = trim($text, '-'); | |
$text = preg_replace('~-+~', '-', $text); | |
$text = strtolower($text); | |
if (empty($text)) { | |
return 'n-a'; | |
} | |
return $text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy ke folder /application/controller