PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/application/models/Article.php

https://github.com/akashprojects/PHP-CMS
PHP | 107 lines | 83 code | 16 blank | 8 comment | 10 complexity | 37579e0ba0b8e9978742c77ef1110568 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: dl717
  5. * Date: 6/28/12
  6. * Time: 9:35 AM
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. class Article extends Eloquent
  10. {
  11. public static $timestamps = true;
  12. public static $table = 'articles';
  13. public function User()
  14. {
  15. return $this->belongs_to("User",'author_id');
  16. }
  17. public function Category()
  18. {
  19. return $this->belongs_to("Category",'category_id');
  20. }
  21. public function Comments()
  22. {
  23. return $this->has_many('Comment',"article_id");
  24. }
  25. public function Tags()
  26. {
  27. return $this->has_many_and_belongs_to('Tag','Articles_Tags');
  28. }
  29. public function getAdjustedArticleContent()
  30. {
  31. $trim = Setting::find(1)->value;
  32. if($trim != 0 && strlen($this->content) > $trim )
  33. $text = (substr($this->content,0,$trim)).'...';
  34. else
  35. $text = ($this->content);
  36. $tidy = new tidy();
  37. $tidy->parseString($text,array('show-body-only'=>true),'utf8');
  38. $tidy->cleanRepair();
  39. return $tidy.'<a href="'.$this->getArticleUrl().'">[ Continue reading ]</a>';
  40. }
  41. public function postedDate()
  42. {
  43. $datetime = strtotime($this->created_at);
  44. $mysqldate = date("F j, Y", $datetime);
  45. return $mysqldate;
  46. }
  47. public function getTagItems()
  48. {
  49. if(is_null($this->id)) return array();
  50. $tagArray = array();
  51. foreach($this->Tags()->get() as $tag)
  52. $tagArray[] = $tag->tname;
  53. return $tagArray;
  54. }
  55. public function getChildCategory()
  56. {
  57. if(is_null($this->id)) return null;
  58. $category = Category::find($this->category_id);
  59. $this->parentCategory = $category;
  60. if( !is_null($category->parent_id))
  61. return $category;
  62. else
  63. return null;
  64. }
  65. public function getParentCategory()
  66. {
  67. if(is_null($this->id)) return null;
  68. $category = Category::find($this->category_id);
  69. if( !is_null($category->parent_id))
  70. return Category::find($category->parent_id);
  71. else
  72. return $category;
  73. }
  74. public function getArticleUrl()
  75. {
  76. if(is_null($this->id)) return null;
  77. $category = Category::find($this->category_id);
  78. $url = "";
  79. $categoryUrl = null;
  80. //Its a article within a parent category
  81. if( is_null($category->parent_id))
  82. $categoryUrl = $category->curl;
  83. else
  84. {
  85. $parentCategory = Category::find($category->parent_id);
  86. $categoryUrl = $parentCategory->curl.'/'.$category->curl;
  87. }
  88. $url=$url.'/'.$categoryUrl.'/'.(is_null($this->url)?str_replace(' ','-',$this->title):$this->url).'/'.$this->id;
  89. return $url;
  90. }
  91. }