/application/models/page_model.php

https://github.com/medieteknik/Medieteknik.nu · PHP · 255 lines · 171 code · 32 blank · 52 comment · 33 complexity · dc3c09184a95345609c0c3b91da410fc MD5 · raw file

  1. <?php
  2. class Page_model extends CI_Model
  3. {
  4. function __construct()
  5. {
  6. // Call the Model constructor
  7. parent::__construct();
  8. }
  9. /**
  10. * Fetches one page by name and returns the content
  11. *
  12. * @return array
  13. */
  14. function get_page_by_name($name)
  15. {
  16. // check if approved to see not approved news
  17. $admin = $this->login->has_privilege('news_editor');
  18. $this->db->select("*");
  19. $this->db->from("page");
  20. $this->db->join("page_content_language", 'page.id = page_content_language.page_id', '');
  21. $this->db->where("name",$name);
  22. $query = $this->db->get();
  23. if ($query->num_rows() == 0)
  24. {
  25. $this->db->select("*");
  26. $this->db->from("page");
  27. $this->db->join("page_content_language", 'page.id = page_content_language.page_id', '');
  28. $this->db->where("name","404");
  29. $query = $this->db->get();
  30. }
  31. return $query->result();
  32. }
  33. /**
  34. * fetches a specific page, admin-style => more data included
  35. *
  36. * @param integer $id The ID of the news item
  37. * @return array
  38. */
  39. function admin_get_page($id)
  40. {
  41. $this->db->select("*");
  42. $this->db->from("page");
  43. $this->db->from("language");
  44. $this->db->join("page_content", 'page_content.page_id = page.id AND page_content.lang_id = language.id', 'left');
  45. $this->db->where("page.id",$id);
  46. $query = $this->db->get();
  47. $translations = $query->result();
  48. $this->db->select("*");
  49. $this->db->from("page");
  50. $this->db->where("page.id",$id);
  51. $this->db->limit(1);
  52. $query = $this->db->get();
  53. $page_array = $query->result();
  54. $page = $page_array[0];
  55. $page->translations = array();
  56. foreach($translations as $t)
  57. {
  58. array_push($page->translations, $t);
  59. }
  60. return $page;
  61. }
  62. /**
  63. * Fetches all pages for the admin overview
  64. *
  65. * @return array
  66. */
  67. function admin_get_all_pages_overview()
  68. {
  69. $this->db->select("*");
  70. $this->db->from("page");
  71. $query = $this->db->get();
  72. return $query->result();
  73. }
  74. /**
  75. * removes a page, must be draft (so no mistake is done)
  76. * @param int $id the page id
  77. * @return [type] [description]
  78. */
  79. function admin_remove_page($id)
  80. {
  81. if($this->is_draft($id))
  82. {
  83. $translations = $this->db->delete('page_content', array('page_id' => $id));
  84. $page = $this->db->delete('page', array('id' => $id, 'published' => 0));
  85. return ($page && $translations);
  86. }
  87. // page is no draft
  88. return false;
  89. }
  90. function is_draft($id)
  91. {
  92. $this->db->where(array('id' => $id, 'published' => 0));
  93. $q = $this->db->get('page');
  94. return $q->num_rows();
  95. }
  96. /**
  97. * Create a new news
  98. *
  99. * @param integer $user_id The ID os the user who creates the news
  100. * @param array $translations All translations of the news item, array("lang_abbr" => "se", "title" => "Inte klistrad!", "text" => "Den här nyheten är inte klistrad eller översatt!")
  101. * @param date $post_date The date of the news item
  102. * @param integer $draft Specify if the news item is a draft, 1 = Draft, 0 = Not draft
  103. * @param integer $approved Specify if the news item is approved, 1 = Approved, 0 = Not approved
  104. * @param integer $group_id The id of the group the user belongs to when posting
  105. * @return The news id
  106. */
  107. function add_page($name, $translations = array(), $published = 0)
  108. {
  109. if(!is_array($translations))
  110. {
  111. return false;
  112. }
  113. $arr_keys = array_keys($translations);
  114. if(!is_numeric($arr_keys[0]))
  115. {
  116. $theTranslations = array($translations);
  117. } else {
  118. $theTranslations = $translations;
  119. }
  120. foreach($theTranslations as &$translation)
  121. {
  122. $arr_keys = array_keys($translation);
  123. if((!in_array("lang_abbr",$arr_keys) && !in_array("lang",$arr_keys)) || !in_array("header",$arr_keys) || !in_array("content",$arr_keys)) {
  124. return false;
  125. }
  126. if(!in_array("lang_abbr",$arr_keys) && in_array("lang",$arr_keys)){
  127. $translation["lang_abbr"] = $translation["lang"];
  128. }
  129. }
  130. $this->db->trans_begin();
  131. $data = array(
  132. 'name' => $name,
  133. 'published' => $published,
  134. );
  135. $this->db->insert('page', $data);
  136. $page_id = $this->db->insert_id();
  137. $success = true;
  138. foreach($theTranslations as &$translation)
  139. {
  140. $lang_abbr = $translation["lang_abbr"];
  141. $header = $translation["header"];
  142. $content = $translation["content"];
  143. $theSuccess = $this->update_page_translation($page_id, $lang_abbr, $header, $content);
  144. if(!$theSuccess)
  145. {
  146. $success = $theSuccess;
  147. }
  148. }
  149. if ($this->db->trans_status() === FALSE || !$success)
  150. {
  151. $this->db->trans_rollback();
  152. return false;
  153. } else {
  154. $this->db->trans_commit();
  155. }
  156. return $page_id;
  157. }
  158. /**
  159. * Update a translation of a specific news item
  160. *
  161. * @param integer $news_id The ID of the news item
  162. * @param string $lang_abbr The language translation abbreviation
  163. * @param string $title The title of the news item translation
  164. * @param string $text The text of the news item translation
  165. * @return bool True or false depending on success or failure
  166. */
  167. function update_page_translation($page_id, $lang_abbr, $header, $content)
  168. {
  169. $theHeader = trim($header);
  170. $theContent = trim($content);
  171. // check if the news exists
  172. $this->db->where('id', $page_id);
  173. $query = $this->db->get('page');
  174. if($query->num_rows != 1)
  175. {
  176. return false;
  177. }
  178. // check if the language exists
  179. $this->db->where('language_abbr', $lang_abbr);
  180. $query = $this->db->get('language');
  181. if($query->num_rows != 1)
  182. {
  183. return false;
  184. }
  185. $lang_id = $query->result(); $lang_id = $lang_id[0]->id;
  186. // if both title and text is null then delete the translation
  187. if($theHeader == '' && $theContent == '')
  188. {
  189. $this->db->delete('page_content', array('page_id' => $page_id, 'lang_id' => $lang_id));
  190. return true;
  191. }
  192. // if one of the title and the text is null then exit
  193. if($theHeader == '' || $theContent == '')
  194. {
  195. return false;
  196. }
  197. $query = $this->db->get_where('page_content', array('page_id' => $page_id, 'lang_id' => $lang_id), 1, 0);
  198. if ($query->num_rows() == 0)
  199. {
  200. // A record does not exist, insert one.
  201. $data = array( 'page_id' => $page_id,
  202. 'lang_id' => $lang_id,
  203. 'header' => $theHeader,
  204. 'content' => $theContent,
  205. 'last_edit' => date("Y-m-d H:i:s", time()),
  206. );
  207. $query = $this->db->insert('page_content', $data);
  208. // Check to see if the query actually performed correctly
  209. if ($this->db->affected_rows() > 0)
  210. {
  211. return TRUE;
  212. }
  213. } else {
  214. // A record does exist, update it.
  215. // update the translation, and if the texts have not been changed then dont update the last_edit field
  216. $theTime = date("Y-m-d H:i:s", time());
  217. $sql = 'UPDATE page_content SET last_edit = IF(STRCMP(header, "'.$theHeader.'") = 0, IF(STRCMP(content, "'.$theContent.'") = 0, last_edit, "'.$theTime.'"), "'.$theTime.'"), header = "'.$theHeader.'", content = "'.$theContent.'" WHERE page_id = "'.$page_id.'" AND lang_id = "'.$lang_id.'" ';
  218. $this->db->query($sql);
  219. return true;
  220. }
  221. return FALSE;
  222. }
  223. }