PHP File Manager
Editing File: News.php
<?php class News extends CI_Model { function get($news_id = null, $options = array()) { $options = array_merge(array( 'alias' => null, 'paginate' => false, 'page' => 1, 'per_page' => 30, 'match_reports' => null, 'upcoming' => null, 'search' => null, 'category' => null, ), $options); $sql = "SELECT n.*," . "t1.name AS match_team_1_name, " . "t2.name AS match_team_2_name, " . "t1.image AS match_team_1_image, " . "t2.image AS match_team_2_image, " . "l.name AS match_location_name " . "FROM news AS n " . "LEFT JOIN team AS t1 ON t1.team_id = n.match_team_1_id " . "LEFT JOIN team AS t2 ON t2.team_id = n.match_team_2_id " . "LEFT JOIN location AS l ON l.location_id = n.match_location_id "; if ($options['category']) { $sql .= "JOIN news_category AS nc ON nc.news_id = n.news_id " . "AND nc.category_id = {$this->db->escape($options['category'])} "; } $sql .= "WHERE n.deleted IS NULL " . "AND n.published <= NOW() "; if ($news_id) { $sql .= "AND n.news_id = {$this->db->escape($news_id)} "; } if ($options['alias']) { $sql .= "AND n.alias = {$this->db->escape($options['alias'])} "; } if ($options['search']) { $sql .= "AND n.body LIKE '%{$this->db->escape_like_str($options['search'])}%' "; } $order_by = array('n.created DESC'); if ($options['match_reports'] !== null) { if ($options['match_reports']) { $sql .= "AND n.match_date IS NOT NULL "; if ($options['upcoming'] === true) { array_unshift($order_by, 'n.match_date ASC'); $sql .= "AND n.match_date >= NOW() "; } elseif ($options['upcoming']) { array_unshift($order_by, 'n.match_date ASC'); $sql .= "AND n.match_date >= '{$options['upcoming']}' "; } elseif ($options['upcoming'] === false) { array_unshift($order_by, 'n.match_date DESC'); $sql .= "AND n.match_date <= NOW() "; } } elseif ($options['match_reports'] === false) { $sql .= "AND n.match_date IS NULL "; } } $sql .= sprintf('ORDER BY %s ', implode(', ', $order_by)); if ($options['paginate']) { $sql .= $this->common->returnOffset($options['page'], $options['per_page']); } $query = $this->db->query($sql); $result = $query->result(); if ($result && ($news_id || $options['alias'])) { return reset($result); } return $result; } function generateAlias($entry_info) { $alias = $test_alias = url_title(nullify(url_title($entry_info['title'])) ? url_title($entry_info['title'], null, true) : generate_unique_identifier()); $sql = "SELECT news_id FROM news WHERE deleted IS NULL "; if (isset($entry_info['news_id']) && $entry_info['news_id']) { $sql .= "AND news_id != {$this->db->escape($entry_info['news_id'])} "; } $offset = null; $conflict = true; while ($conflict) { $test_alias = $offset ? "$alias-$offset" : $alias; $query = $this->db->query($sql . "AND alias = {$this->db->escape($test_alias)} "); if (!$query->num_rows()) { $conflict = false; } $offset++; } return $test_alias; } function save($data) { $save_data = array( 'title' => $data['title'], 'alias' => nullify($data['alias']) ? $data['alias'] : $this->generateAlias($data), 'summary' => nullify($data['summary']), 'body' => $data['body'], 'published' => date('Y-m-d H:i:s', ($data['published'] ? strtotime(str_replace('/', '-', $data['published'])) : time())), 'match_date' => (nullify($data['match_date']) ? date('Y-m-d H:i', strtotime(str_replace('/', '-', $data['match_date']))) : null), 'match_team_1_id' => nullify($data['match_team_1_id']), 'match_team_2_id' => nullify($data['match_team_2_id']), 'match_team_1_score' => nullify($data['match_team_1_score']), 'match_team_2_score' => nullify($data['match_team_2_score']), 'match_location_id' => nullify($data['match_location_id']), ); if (isset($data['news_id'])) { $this->db->where('news_id', $data['news_id']); $this->db->update('news', $save_data); $news_id = $data['news_id']; } else { $save_data['created'] = date('Y-m-d H:i:s'); $save_data['creator'] = $this->users->user_id; $this->db->insert('news', $save_data); $news_id = $this->db->insert_id(); } if (isset($data['categories'])) { $current_news_categories = $this->getCategoriesComparisonData($this->getCategories(null, $news_id)); $categories_to_delete = array_diff($current_news_categories, $data['categories']); $categories_to_insert = array_diff($data['categories'], $current_news_categories); foreach ($categories_to_delete as $category_to_delete) { $this->db->query("DELETE FROM news_category WHERE news_id = $news_id AND category_id = $category_to_delete "); } foreach ($categories_to_insert as $category_to_insert) { $this->db->query("INSERT INTO news_category (news_id, category_id) VALUES ($news_id, $category_to_insert) "); } } return $news_id; } function getCategories($category_id = null, $news_id = null) { $sql = "SELECT cat.* " . "FROM pages AS cat " . "LEFT JOIN news_category AS news_cat ON news_cat.category_id = cat.page_id " . "WHERE news_cat.deleted IS NULL "; if ($category_id) { $sql .= sprintf('AND cat.page_id = %s ', $this->db->escape($category_id)); } if ($news_id) { $sql .= sprintf('AND news_id = %s ', $this->db->escape($news_id)); } $result = $this->db->query($sql); if ($result->num_rows()) { $rows = $result->result(); $result->free_result(); if ($category_id) { return reset($rows); } return $rows; } return array(); } function saveCategories($data) { foreach ($data['cat'] as $category_id => $category) { $save_data = array( 'name' => $category['name'], 'deleted' => (isset($category['deleted']) && $category['deleted'] ? date('Y-m-d H:i:s') : null), ); $this->db->query($this->db->update_string('category', $save_data, "category_id = {$this->db->escape($category_id)}")); } if ($new_name = trim($data['new']['name'])) { $save_data = array('name' => $new_name); $this->db->query($this->db->insert_string('category', $save_data)); } } function getCategoriesDropdownData(array $categories) { $ret = array(); foreach ($categories as $category) { $ret[$category->page_id] = $category->menu_title; } return $ret; } function categoriesDropdown($name, array $categories, array $selected) { $this->load->helper('form'); return form_multiselect( $name, $this->getCategoriesDropdownData($categories), $this->getCategoriesComparisonData($selected), 'class="form-control enhanced"' ); } function getCategoriesComparisonData(array $categories) { return array_keys($this->getCategoriesDropdownData($categories)); } function getSummary($page_info) { if (isset($page_info->summary) && trim(strip_tags($page_info->summary))) { return $page_info->summary; } if (isset($page_info->body) && trim(strip_tags($page_info->body))) { return $this->tokenTruncate($page_info->body, 300); } } function delete($news_id) { return $this->db->query($this->db->update_string('news', array('deleted' => date('Y-m-d H:i:s')), "news_id = {$this->db->escape($news_id)}")); } private function tokenTruncate($string, $your_desired_width) { $parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE); $parts_count = count($parts); $length = 0; $last_part = 0; for (; $last_part < $parts_count; ++$last_part) { $length += strlen($parts[$last_part]); if ($length > $your_desired_width) { break; } } return implode(array_slice($parts, 0, $last_part)); } }
Cancel