PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/arlicle/modules/arlicle/models/post.php

http://arlicle.googlecode.com/
PHP | 171 lines | 146 code | 15 blank | 10 comment | 49 complexity | 5f74e007185dd47ce065b580b98d2e11 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. class Post_Model extends ORM {
  3. protected $has_one = array('user', 'diagram');
  4. protected $has_many = array('attachments', 'customvalues', 'comments');
  5. protected $sorting = array('date' => 'desc');
  6. public static $posts = array();
  7. public function __construct($id = NULL) {
  8. // load database library into $this->db (can be omitted if not required)
  9. parent::__construct($id);
  10. }
  11. public function __set($key, $value) {
  12. if ($key == 'date') {
  13. $value = strtotime($value);
  14. }
  15. if ($key == 'uri') {
  16. $value = $this->unique_uri($value);
  17. }
  18. parent::__set($key, $value);
  19. }
  20. // do the post template pre
  21. public function __get($key) {
  22. $customvalues = parent::__get('customvalues');
  23. foreach($customvalues as $customvalue) {
  24. if ($key == $customvalue->key) {
  25. if ($customvalue->customfield->type == 'multiple') {
  26. return unserialize($customvalue->value);
  27. } else if ($customvalue->customfield->type == 'upload') {
  28. if (defined('IN_TEMPLATE')) {
  29. return url::base() . $customvalue->value;
  30. } else {
  31. return $customvalue->value;
  32. }
  33. } else {
  34. return $customvalue->value;
  35. }
  36. }
  37. }
  38. $value = '';
  39. switch($key) {
  40. case 'content':
  41. $value = parent::__get($key);
  42. // get the right upload file path
  43. $value = Arlicle::upload_file_filter($value);
  44. // if in template use
  45. if (defined('IN_TEMPLATE')) {
  46. Event::run('arlicle.post.content_output_pre', $value);
  47. $value = str_replace(']]>', ']]&gt;', $value);
  48. //show admin edit in the web
  49. if (parent::__get('id') && Auth::instance()->logged_in()) {
  50. if (parent::__get('status') and parent::__get('diagram_id')) {
  51. $value .= '<p>' . html::admin_anchor("/post_new/$this->id/edit?redirect_uri=" . Router::$complete_uri, T::_('Edit'), array('style'=>'color:#ff0000;')) . '</p>';
  52. }
  53. }
  54. }
  55. break;
  56. case 'thumb':
  57. case 'thumb_original':
  58. $value = '';
  59. $attachments = parent::__get('attachments');
  60. foreach($attachments as $attach) {
  61. if ($attach->is_thumb) {
  62. $value = $attach->filename;
  63. }
  64. }
  65. if (defined('IN_TEMPLATE') and $key != 'thumb_original' and !empty($value)) {
  66. $value = url::file($value);
  67. }
  68. break;
  69. case 'link':
  70. $value = parent::__get('uri');
  71. $key = Kohana::config('arlicle.uri_optimize');
  72. if ($key == 'id') {
  73. $value = parent::__get('id');
  74. }
  75. $value = Kohana::config('arlicle.post_uri') . '/' . $value;
  76. break;
  77. default:
  78. try {
  79. $value = parent::__get($key);
  80. } catch( Exception $e ) {
  81. $value = '';
  82. }
  83. break;
  84. }
  85. return $value;
  86. }
  87. public function unique_uri($uri, $count = 1) {
  88. // for the second recurssion
  89. if ($count != 1) {
  90. $uri_2 = sprintf($uri, $count);
  91. } else {
  92. $uri_2 = $uri;
  93. }
  94. // check if add new or edit
  95. if (!empty($this->id)) {
  96. $post_count = $this->db->where(array('uri' => $uri_2, 'id !=' => $this->id))->count_records($this->table_name); // edit
  97. } else {
  98. $post_count = $this->db->where(array('uri' => $uri_2))->count_records($this->table_name); // add new
  99. }
  100. // if no post record use this uri, return it
  101. if (empty($post_count)) {
  102. return $uri_2;
  103. } else {
  104. // if it was been use, add_$count on it
  105. if ($count == 1) {
  106. $uri = "{$uri}-%s";
  107. }
  108. $count++;
  109. return $this->unique_uri($uri, $count);
  110. }
  111. }
  112. public static function post($args = array()) {
  113. $default_args = array('uri'=>'', 'id'=>0);
  114. if (empty($args['id']) and empty($args['uri'])) {
  115. // get current post
  116. $key = trim(URI::segment(2));
  117. if (strtolower(URI::segment(1)) != Kohana::config('arlicle.post_uri') or empty($key)) {
  118. return false;
  119. }
  120. $uri_optimize = Kohana::config('arlicle.uri_optimize');
  121. if ($uri_optimize == 'uri') {
  122. $condition = "p.uri='$key'";
  123. } else if ($uri_optimize == 'id') {
  124. $key = (int)$key;
  125. $condition = "p.id='$key'";
  126. }
  127. } else {
  128. if (!empty($args['id'])) {
  129. $key = (int)$args['id'];
  130. $condition = "p.id='$key'";
  131. } else if (!empty($args['uri'])) {
  132. $key = $args['uri'];
  133. $condition = "p.uri='$key'";
  134. }
  135. }
  136. if (empty(self::$posts[$key])) {
  137. $db = new Database();
  138. $databas_info = Kohana::config('database.default');
  139. $prefix = $databas_info['table_prefix'];
  140. $posts = $db->query("select p.*,d.type as diagram_type,d.title as diagram_title, d.content as diagram_content,d.parent_id as diagram_parent_id, d.uri as diagram_uri, d.template as diagram_template, d.metavalue as diagram_metavalue, d.date as diagram_date, d.order as diagram_order, d.status as diagram_status from {$prefix}posts as p left join {$prefix}diagrams as d on p.diagram_id=d.id where $condition limit 1");
  141. if (isset($posts[0])) {
  142. self::$posts[$key] = $posts[0];
  143. self::$posts[$key]->diagram_post_template = '';
  144. if (!empty(self::$posts[$key]->diagram_metavalue)) {
  145. $metavalue = unserialize(self::$posts[$key]->diagram_metavalue);
  146. if (isset($metavalue['post_template'])) {
  147. self::$posts[$key]->diagram_post_template = $metavalue['post_template'];
  148. }
  149. }
  150. } else {
  151. self::$posts[$key] = true;
  152. }
  153. }
  154. return self::$posts[$key];
  155. }
  156. }