<?php
defined('BASEPATH') or exit('No direct script access allowed');

class ArticleModel extends MY_Model
{
	private $categories;
	const PRESS_ID = 29;

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	public function save($articles)
	{
		$inserted = [];
		$this->categories = $this->categoryModel->findAll();
		log_message('info', count($articles). ' articles');
		foreach ($articles as $a) {
			$arTimestamp = date('Y-m-d H:i:s', $a->ar_timestamp);
			$a = (object) array_map(function ($val) {
				return is_null($val) ? "" : $val;
			}, (array) $a);
			$article = [
				'category_code' => $this->categoryModel->findByName($a->categoryTitle),
				'news_type' => 1,
				'title' => $a->ar_title,
				'sub_title' => $a->ar_subtitle,
				'main_img' => 'https://images.christiantoday.co.kr/data/images/full/' . $a->im_id . '/image.'. $a->im_filetype,
				'video_url' => '',
				'video_playtime' => '',
				'press_idx' => self::PRESS_ID,
				'press_name' => '크리스천투데이',
				'reporter_name' => $a->reporter,
				'reporter_email' => $a->reporter_email,
				'content' => $a->ar_content,
				'org_url' => $a->ar_originlink,
				'is_auto_setting' => 'Y',
				'tag' => $a->ta_title,
				'is_del' => 'N',
				'is_display' => 'Y',
				'display_date' => date('Y-m-d H:i:s'),
				'review_count1' => 0,
				'review_count2' => 0,
				'review_count3' => 0,
				'review_count4' => 0,
				'review_count5' => 0,
				'hit_count' => 0,
				'like_count' => 0,
				'comment_count' => 0,
				'share_count' => 0,
				'popular_count' => 0,
				'write_id' => $a->mb_no,
				'write_name' => $a->mb_name,
				'reg_id' => 'bot.ctnews.' . $a->ar_id,
				'reg_date' => $arTimestamp,
				'update_id' => 'developer',
				'update_date' => $arTimestamp
			];
			// $article['content'] = '<p style="line-height: 1.8;"><span style="font-family: 나눔고딕코딩, NanumGothicCoding, sans-serif; font-size: 9pt;"><img src="' . $article['main_img'] . '" /></span></p><br style="clear:both;">' . $article['content'];
			if (!$this->db->where('reg_id',  $article['reg_id'])->get('news')->num_rows()) {
				$article['content'] = preg_replace("{<img\\s*(.*?)src=('.*?'|\".*?\"|[^\\s]+)(.*?)\\s*/?>}ims", '<div style="text-align:center"><img $1src=$2 $3/></div>', $article['content']);
				$article['content'] = str_replace('class="dimg"', 'style="color: #666;font-size: 12px;line-height: 16px;padding: 10px 4px 4px;letter-spacing: 0px;"', $article['content']);
				$article['content'] = str_replace('class="imgtbl"', 'style="max-width: 650px;margin-bottom: 20px;margin-right:auto;margin-left:auto;"', $article['content']);
				$article['content'] = str_replace('class="imgtbl"', 'style="max-width: 650px;margin-bottom: 20px;margin-right:auto;margin-left:auto;"', $article['content']);
				
				$article['content'] = str_replace('<p>', '<p style="margin-bottom:10px">', $article['content']);

				$article['content'] .= '<p style="line-height: 1.8;"><span style="font-family: 나눔고딕코딩, NanumGothicCoding, sans-serif; font-size: 11pt; color: rgb(172, 172, 172);">&lt;저작권자 ⓒ \'종교 신문 1위\' 크리스천투데이(<a href="http://www.chtoday.co.kr">www.chtoday.co.kr</a>), 무단전재 및 재배포 금지&gt;</span><span style="font-family: 나눔고딕코딩, NanumGothicCoding, sans-serif; font-size: 11pt; color: rgb(172, 172, 172);">​</span>&nbsp;</p>';					
				$article['content'] = '<div style="font-size: 16px;line-height: 28px;letter-spacing: -1px;font-family: 나눔고딕코딩, NanumGothicCoding, sans-serif;">' . $article['content'] . '</div>';
				$this->db->insert('news', $article);
				array_push($inserted, $this->db->insert_id());
			}
		}
		return $inserted;
	}
}
