PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Content/Content.php

https://github.com/lillem4n/kohana-module-pajas
PHP | 343 lines | 155 code | 36 blank | 152 comment | 17 complexity | 7d03f6507442e9a1666262f7efb8acee MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. class Content_Content extends Model
  3. {
  4. /**
  5. * The database driver
  6. *
  7. * @var obj
  8. */
  9. static private $driver;
  10. /**
  11. * Content
  12. *
  13. * @var str
  14. */
  15. private $content;
  16. /**
  17. * Content id
  18. *
  19. * @var int
  20. */
  21. private $content_id;
  22. /**
  23. * Tag IDs connected to this content
  24. *
  25. * @var array - tag name as key, tag values as array value
  26. * example:
  27. * array(
  28. * 'location' => array('stockholm', 'tokyo'),
  29. * 'blogpost' => array(NULL),
  30. * )
  31. */
  32. private $tags;
  33. /**
  34. * Constructor
  35. *
  36. * @param int $id - Content id
  37. */
  38. public function __construct($id = FALSE)
  39. {
  40. parent::__construct(); // Connect to the database
  41. if ($id)
  42. {
  43. $this->content_id = $id;
  44. if ( ! $this->load_content())
  45. {
  46. // This content id does not exist, unset the page id again
  47. $this->content_id = NULL;
  48. }
  49. }
  50. }
  51. /**
  52. * Loads the driver if it has not been loaded yet, then returns it
  53. *
  54. * @return Driver object
  55. * @author Johnny Karhinen, http://fullkorn.nu, johnny@fullkorn.nu
  56. */
  57. public static function driver()
  58. {
  59. if (self::$driver == NULL) self::set_driver();
  60. return self::$driver;
  61. }
  62. /**
  63. * Get content
  64. *
  65. * @return str
  66. */
  67. public function get_content()
  68. {
  69. return $this->content;
  70. }
  71. /**
  72. * Get current content id
  73. *
  74. * @return int
  75. */
  76. public function get_content_id()
  77. {
  78. return $this->content_id;
  79. }
  80. /**
  81. * Get content id by tags
  82. *
  83. * Will return first matching id for these tags or FALSE if none is found
  84. *
  85. * @param $tags - tag name as key, tag values as values
  86. * @return int or FALSE
  87. */
  88. public static function get_content_id_by_tags($tags)
  89. {
  90. $contents = self::driver()->get_contents_by_tags($tags);
  91. if (count($contents))
  92. {
  93. list($content_id) = array_keys($contents);
  94. return $content_id;
  95. }
  96. return FALSE;
  97. }
  98. /**
  99. * Get contents
  100. *
  101. * @return array - ex array(
  102. * array(
  103. * id => 1,
  104. * content => Lots of content
  105. * tags => array(
  106. * array(
  107. * id => 3,
  108. * name => blog post,
  109. * )
  110. * )
  111. * ),
  112. * array(
  113. * id => 2,
  114. * tags => array(
  115. * array(
  116. * id => 4,
  117. * name => News,
  118. * )
  119. * array(
  120. * id => 5,
  121. * name => RSS post,
  122. * )
  123. * )
  124. * content => Lots of content
  125. * ),
  126. * )
  127. */
  128. public static function get_contents()
  129. {
  130. return self::driver()->get_contents();
  131. }
  132. /**
  133. * Get contents by tags
  134. *
  135. * @param $tags - tag name as key, tag values as values
  136. * @param $order_by - 'content', 'id' or array of tag names (tag name as key, order (ASC/DESC) as value)
  137. * @param $limit - integer
  138. * @param $offset - integer (defaults to 0)
  139. * @return array of content ids - ex array(
  140. * array(
  141. * id => 1,
  142. * content => Lots of content
  143. * tags => array(
  144. * date => array('2011-05-30'),
  145. * blogpost => array(NULL)
  146. * location => array('stockholm', 'uppsala')
  147. * )
  148. * ),
  149. * array(
  150. * id => 2,
  151. * content => Lots of content
  152. * tags => array(
  153. * date => array('2011-05-30'),
  154. * blogpost => array(NULL)
  155. * location => array('stockholm', 'uppsala')
  156. * )
  157. * ),
  158. * )
  159. */
  160. public static function get_contents_by_tags($tags = FALSE, $order_by = FALSE, $limit = FALSE, $offset = 0)
  161. {
  162. return self::driver()->get_contents_by_tags($tags, $order_by, $limit, $offset);
  163. }
  164. /**
  165. * Get contents by tag id
  166. *
  167. * @param int $tag_id
  168. * @return array of content ids - ex array(
  169. * array(
  170. * id => 1,
  171. * content => Lots of content
  172. * tags => array(
  173. * date => array('2011-05-30'),
  174. * blogpost => array(NULL)
  175. * location => array('stockholm', 'uppsala')
  176. * )
  177. * ),
  178. * array(
  179. * id => 2,
  180. * content => Lots of content
  181. * tags => array(
  182. * date => array('2011-05-30'),
  183. * blogpost => array(NULL)
  184. * location => array('stockholm', 'uppsala')
  185. * )
  186. * ),
  187. * )
  188. */
  189. public static function get_contents_by_tag_id($tag_id)
  190. {
  191. return self::driver()->get_contents_by_tag_id($tag_id);
  192. }
  193. public static function get_contents_by_tag_value($tag_value)
  194. {
  195. return self::driver()->get_contents_by_tag_value($tag_value);
  196. }
  197. /**
  198. * Get contents by tags
  199. *
  200. * @param $tags - tag name as key, tag values as values
  201. * @param $limit - integer
  202. * @param $offset - integer (defaults to 0)
  203. * @return int amount of contents found
  204. */
  205. public static function get_contents_count_by_tags($tags = FALSE, $limit = FALSE, $offset = 0)
  206. {
  207. return self::driver()->get_contents_count_by_tags($tags, $limit, $offset);
  208. }
  209. public static function get_contents_for_xml($tags = FALSE, $order_by = FALSE, $limit = FALSE, $offset = 0)
  210. {
  211. $contents = self::driver()->get_contents_by_tags($tags, $order_by, $limit, $offset);
  212. foreach ($contents as $nr => $content)
  213. {
  214. $counter = 0;
  215. foreach ($content['tags'] as $tag_name => $tag_values)
  216. {
  217. foreach ($tag_values as $tag_value)
  218. {
  219. $counter++;
  220. $content['tags'][$counter.$tag_name] = array(
  221. '@id' => Tags::get_id_by_name($tag_name),
  222. '$value' => $tag_value
  223. );
  224. }
  225. unset($content['tags'][$tag_name]);
  226. if (count($tag_values) == 0) $content['tags'][$tag_name] = array('@id' => Tags::get_id_by_name($tag_name));
  227. }
  228. $content['@id'] = $content['id'];
  229. $content['content'] = xml::to_array(Markdown::transform($content['content']));
  230. unset($content['id']);
  231. $contents[$nr.'content'] = $content;
  232. unset($contents[$nr]);
  233. }
  234. return $contents;
  235. }
  236. public function get_tag($tag_name)
  237. {
  238. if ($tags = $this->get_tags())
  239. {
  240. foreach ($tags as $tag_id => $tag_data)
  241. {
  242. if ($tag_data['name'] == $tag_name)
  243. {
  244. if (count($tag_data['values']) == 0) return TRUE;
  245. elseif (count($tag_data['values']) == 1) return reset($tag_data['values']);
  246. else return $tag_data['values'];
  247. }
  248. }
  249. }
  250. return FALSE;
  251. }
  252. public function get_tags()
  253. {
  254. return $this->tags;
  255. }
  256. public function load_content()
  257. {
  258. $this->tags = self::driver()->get_tags_by_content_id($this->get_content_id());
  259. $this->content = self::driver()->get_content($this->get_content_id());
  260. return TRUE;
  261. }
  262. public static function new_content($content, $tags = FALSE)
  263. {
  264. return self::driver()->new_content($content, $tags);
  265. }
  266. public function rm_content()
  267. {
  268. if (self::driver()->rm_content($this->get_content_id()))
  269. {
  270. unset($this);
  271. return TRUE;
  272. }
  273. return FALSE;
  274. }
  275. /**
  276. * Set the database driver
  277. *
  278. * @return boolean
  279. */
  280. public static function set_driver()
  281. {
  282. $driver_name = 'Driver_Content_'.ucfirst(Kohana::$config->load('content.driver'));
  283. return (self::$driver = new $driver_name);
  284. }
  285. public function update_content($content, $tags = FALSE)
  286. {
  287. if (self::driver()->update_content($this->get_content_id(), $content, $tags))
  288. {
  289. // We must update the local class content also
  290. $this->load_content();
  291. return TRUE;
  292. }
  293. return FALSE;
  294. }
  295. public static function update_content_by_tags($content_string, $tags, $create_if_not_exists = TRUE)
  296. {
  297. $content_id = self::get_content_id_by_tags($tags);
  298. if ($content_id)
  299. {
  300. $content = new self($content_id);
  301. $content->update_content($content_string);
  302. }
  303. elseif ($create_if_not_exists)
  304. $content_id = self::new_content($content_string, $tags);
  305. return $content_id;
  306. }
  307. }