Files
cm-web/application/helpers/general_helper.php
2020-06-10 06:21:34 +09:00

174 lines
5.4 KiB
PHP
Executable File

<?php
require_once(__DIR__ . '/hoosk_page_helper.php');
function lq($result = '')
{
echo get_instance()->db->last_query();
print_r($result);
exit;
}
function getPostLink($post, $sn)
{
$post = is_object($post) ? $post : (object) $post;
$contentSection = ($sn == 'opinions' ? 'channel' : 'news');
return BASE_URL . '/' . $contentSection . '/' . ($sn == 'opinions' ? $post->pageID : $sn) . '/' . (empty($post->postSeq) ? $post->postID : $post->postSeq);
}
function getChildrenByParentId($parentId)
{
$sql = "SELECT
categoryID
FROM
(SELECT
categoryID
FROM
cm_category
WHERE
parentID = $parentId) a
UNION SELECT
categoryID
FROM
cm_category
WHERE
parentID IN (SELECT
categoryID
FROM
cm_category
WHERE
parentID = $parentId)";
return array_column(get_instance()->db->query($sql)->result(), 'categoryID');
}
function getNewsArticleURL($data, $sn = null)
{
$articles = [];
if (is_null($sn)) {
$subcategories = [];
foreach (NEWS_SECTION_CATEGORY as $sn => $sc) {
$subcategories[$sn] = getChildrenByParentId($sc);
array_push($subcategories[$sn], $sc);
}
foreach (NEWS_SECTION_CATEGORY as $sn => $sc) {
foreach ($data as $post) {
if (array_search($post->categoryID, $subcategories[$sn]) !== false) {
$post->articleURL = getPostLink($post, $sn);
array_push($articles, $post);
}
}
}
} else if (is_numeric($sn)) {
get_instance()->load->model('category_model');
$subcategories = get_instance()->category_model->getSubcategoriesByCategoryId($sn);
} else {
foreach ($data as $k => $p) {
$data[$k]->articleURL = getPostLink($p, $sn);
}
}
return $articles;
}
function getResultWithAuthor(&$data)
{
$userIds = array_column($data, 'userID');
if (count($userIds)) {
$users = get_instance()->db->where_in('cm_user.userID', $userIds)
->get('cm_user')
->result();
}
foreach ($data as $k => $t) {
$data[$k]->author = '';
foreach ($users as $u) {
if ($t->userID == $u->userID) {
$data[$k]->author = $u->name;
}
}
}
}
function r(callable $fn, $cache)
{
if (is_null($cache) || !$data = get_instance()->cache->get($cache['n'])) {
$data = $fn();
if (!is_null($cache) && !is_null($data)) {
$this->cache->save($cache['n'], $data, $cache['t']);
}
}
return $data;
}
function s($task, $duration, $cache)
{
$cache = 'task-' . $cache;
$lastExecutionTime = get_instance()->cache->get($cache);
if (time() - $lastExecutionTime >= $duration) {
get_instance()->cache->save($cache, time(), 60 * 60);
$task();
}
}
function getSessionUser()
{
$user = (object) ['is' => false];
get_instance()->load->library('session');
if (get_instance()->session->userdata('auth')) {
$user = json_decode(get_instance()->session->userdata('auth'));
$user->is = true;
};
return $user;
}
function getSearchTerm()
{
return get_instance()->input->get('term') ? get_instance()->input->get('term') : get_instance()->input->post('term');
}
function getPostComments($postId)
{
get_instance()->load->mode('comment_model');
$comments = get_instance()->comment_model->getByPostId($postId);
return $comments;
}
function getImageUrl($imagePath)
{
// return 'https://crossmap.co.kr' . $imagePath;
return strpos($imagePath, 'thumbs/') === false && strpos($imagePath, 'images/') === false ? 'https://crossmap.co.kr' . $imagePath : $imagePath;
}
function getThumbUrl($imagePath, $postThumb)
{
return 'https://crossmap.co.kr' . str_replace('files/', 'thumbs/', $imagePath);
return $postThumb ? $postThumb : 'https://crossmap.co.kr' . str_replace('files/', 'thumbs/', $imagePath);
}
function isAdminSite()
{
return defined('HOOSK_ADMIN') && HOOSK_ADMIN == 1;
}
function getPostsImageUrl(&$posts)
{
if (is_array($posts)) {
foreach ($posts as $k => $v) {
if (is_array($v)) {
if(isset($posts[$k]['postImage'])){
strpos($posts[$k]['postImage'], '://') === false ? ($posts[$k]['postImage'] = getImageUrl($posts[$k]['postImage'])) : '';
$posts[$k]['postImage'] = str_replace('http://', 'https://', $posts[$k]['postImage']);
}
} else {
if(isset($posts[$k]->postImage)) {
strpos($posts[$k]->postImage, '://') === false ? ($posts[$k]->postImage = getImageUrl($posts[$k]->postImage)) : '';
$posts[$k]->postImage = str_replace('http://', 'https://', $posts[$k]->postImage);
}
}
}
}
}
function getPrayPostUrl($post) {
return implode('/', ['/pray', $post->categorySlug, $post->postSeq]);
}