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

/classes/Driver/Content.php

https://github.com/lillem4n/kohana-module-pajas
PHP | 495 lines | 116 code | 41 blank | 338 comment | 15 complexity | 3ff85ebe0f4fdfea3f54e29fdb8e1d99 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. abstract class Driver_Content extends Model
  3. {
  4. public function __construct($check = TRUE)
  5. {
  6. parent::__construct();
  7. if (Kohana::$environment == Kohana::DEVELOPMENT)
  8. {
  9. if ( ! $this->check_db_structure())
  10. {
  11. $this->create_db_structure();
  12. $this->insert_initial_data();
  13. }
  14. }
  15. if ($check)
  16. {
  17. // Add image files that does not exist in database
  18. $tracked_images = $this->get_images(NULL, array(), TRUE);
  19. $cwd = getcwd();
  20. chdir(Kohana::$config->load('user_content.dir').'/images');
  21. $actual_images = glob('{*.jpg,*.png}', GLOB_BRACE);
  22. chdir($cwd);
  23. foreach (array_diff($actual_images, $tracked_images) as $non_tracked_image)
  24. {
  25. $pathinfo = pathinfo($non_tracked_image);
  26. if ($pathinfo['filename'] != URL::title($pathinfo['filename']))
  27. {
  28. // We need to rename the image so it fits the URL
  29. $new_filename = URL::title($pathinfo['filename']).'.'.strtolower($pathinfo['extension']);
  30. $counter = 1;
  31. while ( ! Content_Image::image_name_available($new_filename))
  32. {
  33. $new_filename = URL::title($pathinfo['filename']).'_'.$counter.'.'.strtolower($pathinfo['extension']);
  34. $counter++;
  35. }
  36. if (@rename(Kohana::$config->load('user_content.dir').'/images/'.$non_tracked_image, Kohana::$config->load('user_content.dir').'/images/'.$new_filename))
  37. {
  38. $non_tracked_image = $new_filename;
  39. $pathinfo = pathinfo($non_tracked_image);
  40. }
  41. }
  42. if ($pathinfo['filename'] == URL::title($pathinfo['filename']))
  43. {
  44. if ($pathinfo['extension'] == 'jpg')
  45. @$gd_img_object = ImageCreateFromJpeg(Kohana::$config->load('user_content.dir').'/images/'.$non_tracked_image);
  46. elseif ($pathinfo['extension'] == 'png')
  47. @$gd_img_object = ImageCreateFromPng(Kohana::$config->load('user_content.dir').'/images/'.$non_tracked_image);
  48. if ($gd_img_object)
  49. {
  50. $width = imagesx($gd_img_object);
  51. $height = imagesy($gd_img_object);
  52. $this->new_image($non_tracked_image, array('width'=>$width,'height'=>$height));
  53. }
  54. else
  55. {
  56. // Not valid image
  57. $new_filename = $non_tracked_image.'_broken';
  58. $counter = 1;
  59. while (file_exists(Kohana::$config->load('user_content.dir').'/images/'.$new_filename))
  60. {
  61. $new_filename = $non_tracked_image.'_broken_'.$counter;
  62. $counter++;
  63. }
  64. @rename(Kohana::$config->load('user_content.dir').'/images/'.$non_tracked_image, Kohana::$config->load('user_content.dir').'/images/'.$new_filename);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. /**
  71. * Returns true/false depending on if the db structure exists or not
  72. *
  73. * @author Johnny Karhinen, http://fullkorn.nu, johnny@fullkorn.nu
  74. * @return boolean
  75. */
  76. abstract protected function check_db_structure();
  77. /**
  78. * Create the db structure
  79. *
  80. * @return boolean
  81. */
  82. abstract protected function create_db_structure();
  83. /**
  84. * Insert initial data
  85. *
  86. * @author Johnny Karhinen, http://fullkorn.nu, johnny@fullkorn.nu
  87. * @return boolean
  88. */
  89. protected function insert_initial_data()
  90. {
  91. Tags::add('page_welcome');
  92. Tags::add('Puff 1');
  93. Tags::add('Puff 2');
  94. Tags::add('Puff 3');
  95. $this->new_content('# A First Level Header'."\n\n".'## A Second Level Header'."\n\n".'Some of these words *are emphasized*.'."\n".'Some of these words _are emphasized also_.'."\n\n".'Use two asterisks for **strong emphasis**.'."\n".'Or, if you prefer, __use two underscores instead__.'."\n\n".'Unordered (bulleted) lists use asterisks, pluses, and hyphens (*, +, and -) as list markers. These three markers are interchangable; this:'."\n\n".'* Candy.'."\n".'* Gum.'."\n".'* Booze.'."\n\n".'Ordered (numbered) lists use regular numbers, followed by periods, as list markers:'."\n\n".'1. Red'."\n".'2. Green'."\n".'3. Blue'."\n\n".'More basics at [Daring Fireball](http://daringfireball.net/projects/markdown/basics).', array('page_welcome'));
  96. $this->new_content('### Help'."\n\n".'You can access the admin with [this link](admin).'."\n\n".'See online help [here](http://larvit.se/pajas).'."\n\n".'Wiki [here](https://github.com/lillem4n/kohana-module-pajas/wiki)', array('Puff 1'));
  97. $this->new_content('### Col 2'."\n\n".'Lorem ipsum dolor sit amet, consectetur adipizscing elit. Fusce velit quam, pharetra id, vehicula eu, consectetur ut, orci. Donec odio. Donec non neque. Ut rutrum lectus nec elit. Ut id quam. Cras aliquam erat eu mi. Aliquam orci neque, lobortis a, tempus ut, lacinia sit amet, purus.', array('Puff 2'));
  98. $this->new_content('### Col 3'."\n\n".'Lorem ipsum dolor sit amet, consectetur adipizscing elit. Fusce velit quam, pharetra id, vehicula eu, consectetur ut, orci. Donec odio. Donec non neque. Ut rutrum lectus nec elit. Ut id quam. Cras aliquam erat eu mi. Aliquam orci neque, lobortis a, tempus ut, lacinia sit amet, purus.', array('Puff 3'));
  99. // We set the URI to 'welcome' since that is Kohanas default route
  100. $this->new_page('Hello world!', 'welcome', array(1=>1,2=>2,3=>3,4=>4));
  101. return TRUE;
  102. }
  103. /**
  104. * Get content
  105. *
  106. * @param int $content_id
  107. * @return str
  108. */
  109. abstract public function get_content($content_id);
  110. /**
  111. * Get all contents
  112. *
  113. * @return array - ex array(
  114. * array(
  115. * id => 1,
  116. * content => Lots of content
  117. * tags => array(
  118. * array(
  119. * id => 3,
  120. * name => blog post,
  121. * value => NULL,
  122. * )
  123. * )
  124. * ),
  125. * array(
  126. * id => 2,
  127. * tags => array(
  128. * array(
  129. * id => 4,
  130. * name => Date,
  131. * value => 2011-07-04,
  132. * )
  133. * array(
  134. * id => 5,
  135. * name => RSS post,
  136. * value => NULL,
  137. * )
  138. * )
  139. * content => Lots of content
  140. * ),
  141. * )
  142. */
  143. abstract public function get_contents();
  144. /**
  145. * Get contents by tags
  146. *
  147. * @param $tags - tag name as key, tag values as values
  148. * @param $order_by - 'content', 'id' or array of tag names (tag name as key, order (ASC/DESC) as value)
  149. * @param $limit - integer
  150. * @param $offset - integer (defaults to 0)
  151. * @return array of content ids - ex array(
  152. * array(
  153. * id => 1,
  154. * content => Lots of content
  155. * tags => array(
  156. * date => array('2011-05-30'),
  157. * blogpost => array(NULL)
  158. * location => array('stockholm', 'uppsala')
  159. * )
  160. * ),
  161. * array(
  162. * id => 2,
  163. * content => Lots of content
  164. * tags => array(
  165. * date => array('2011-05-30'),
  166. * blogpost => array(NULL)
  167. * location => array('stockholm', 'uppsala')
  168. * )
  169. * ),
  170. * )
  171. */
  172. abstract public function get_contents_by_tags($tags = FALSE, $order_by = FALSE, $limit = FALSE, $offset = 0);
  173. /**
  174. * Get contents by tag id
  175. *
  176. * @param int $tag_id
  177. * @return array of content ids - ex array(1, 3, 4
  178. * array(
  179. * id => 1,
  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. * array(
  188. * id => 2,
  189. * content => Lots of content
  190. * tags => array(
  191. * date => array('2011-05-30'),
  192. * blogpost => array(NULL)
  193. * location => array('stockholm', 'uppsala')
  194. * )
  195. * ),
  196. * )
  197. */
  198. abstract public function get_contents_by_tag_id($tag_id);
  199. /**
  200. * Get contents by tags
  201. *
  202. * @param $tags - tag name as key, tag values as values
  203. * @param $limit - integer
  204. * @param $offset - integer (defaults to 0)
  205. * @return int amount of contents found
  206. */
  207. abstract public function get_contents_count_by_tags($tags = FALSE, $limit = FALSE, $offset = 0);
  208. /**
  209. * Get images
  210. *
  211. * @param str or array $names - Fetch specific images based on name
  212. * @param arr $tags - Fetch specific images based on tags,
  213. * key as tag name, value as array of
  214. * tag values.
  215. * If value is boolean TRUE, all images
  216. * with this tag will be fetched.
  217. *
  218. * example:
  219. * array(
  220. * 'car' => TRUE,
  221. * 'colors' => array('red', 'green'),
  222. * )
  223. *
  224. * @param bol $names_only - Make this method return an array of image
  225. * names only
  226. * @return arr - array(
  227. * // Image name
  228. * 'foobar' => array(
  229. * // Image tags
  230. * 'date' => array('2011-05-03'),
  231. * 'description' => array('Some description'),
  232. * 'tag' => array('car', 'blue', 'fast'),
  233. * etc...
  234. * ),
  235. * )
  236. */
  237. abstract public function get_images($names = NULL, $tags = array(), $names_only = FALSE);
  238. /**
  239. * Get page data
  240. *
  241. * @param int $id - Page ID
  242. * @return arr - array(
  243. * 'id' => 1
  244. * 'name' => Some page
  245. * 'URI' => some-page
  246. * 'tag_ids' => array(
  247. * template_field_id => tag_ids
  248. * 1 => array(1),
  249. * 2 => array(4, 5, 8),
  250. * 5 => array(2),
  251. * )
  252. * )
  253. */
  254. abstract public function get_page_data($id);
  255. /**
  256. * Get page ID by URI
  257. *
  258. * @param str $URI
  259. * @return int
  260. */
  261. abstract public function get_page_id_by_URI($URI);
  262. /**
  263. * Get pages
  264. *
  265. * @return array - ex array(
  266. * array(
  267. * id => 1,
  268. * name => About,
  269. * URI => about,
  270. * 'tag_ids' => array(
  271. * template_field_id => tag_ids
  272. * 1 => array(1),
  273. * 2 => array(4, 5, 8),
  274. * 5 => array(2),
  275. * )
  276. * ),
  277. * array(
  278. * id => 2,
  279. * name => Contact us,
  280. * URI => contact,
  281. * 'tag_ids' => array(
  282. * template_field_id => tag_ids
  283. * 1 => array(1),
  284. * 2 => array(4, 5, 8),
  285. * 5 => array(2),
  286. * )
  287. * ),
  288. * )
  289. */
  290. abstract public function get_pages();
  291. /**
  292. * Get tag ids by content id
  293. *
  294. * @param int $content_id OPTIONAL
  295. * @return arr - array(
  296. * 1 => array(
  297. * 'id' => 1,
  298. * 'name' => 'location',
  299. * 'values' => array('stockholm', 'uppsala'),
  300. * )
  301. * 3 => array(
  302. * 'id' => 3,
  303. * 'name' => 'blogpost',
  304. * 'values' => array(NULL),
  305. * )
  306. * )
  307. */
  308. abstract public function get_tags_by_content_id($content_id = FALSE);
  309. /**
  310. * Get tag ids by image name
  311. *
  312. * @param str $image_name OPTIONAL
  313. * @return arr - array(
  314. * 1 => array(
  315. * 'name' => 'location',
  316. * 'values' => array('stockholm', 'uppsala'),
  317. * )
  318. * 3 => array(
  319. * 'name' => 'blogpost',
  320. * 'values' => array(NULL),
  321. * )
  322. * )
  323. */
  324. abstract public function get_tags_by_image_name($image_name = FALSE);
  325. /**
  326. * Checks if a image name is available
  327. *
  328. * @param str $name - Image name
  329. * @return boolean
  330. */
  331. abstract public function image_name_available($name);
  332. /**
  333. * New content
  334. *
  335. * @param str $content
  336. * @param arr $tags
  337. * - Tag name as key, value or array of values as value
  338. * set value as NULL if you want a simple tag
  339. * @return int content id
  340. */
  341. abstract public function new_content($content, $tags = FALSE);
  342. /**
  343. * New image
  344. *
  345. * @param str $name - Image name
  346. * @param arr $tags
  347. * - Tag name as key, value or array of values as value
  348. * set value as NULL if you want a simple tag
  349. * @return boolean
  350. */
  351. abstract public function new_image($name, $tags = FALSE);
  352. /**
  353. * Create a new page
  354. *
  355. * @param str $name - Page name
  356. * @param str $URI - Page URL (Defaults to page name, just URL formatted)
  357. * @param str $content - Page content (Defaults to empty string)
  358. * @param arr $tags - Array with template_field_id as key and array of tag_ids as value
  359. * @return int page id
  360. */
  361. abstract public function new_page($name, $URL, $tags = FALSE);
  362. /**
  363. * Checks if a page name is available
  364. *
  365. * @param str $page_name
  366. * @return boolean
  367. */
  368. abstract public function page_name_available($page_name);
  369. /**
  370. * Remove content
  371. *
  372. * @param int $content_id
  373. * @return boolean
  374. */
  375. abstract public function rm_content($content_id);
  376. /**
  377. * Remove image from database
  378. *
  379. * @param str $name
  380. * @return boolean
  381. */
  382. abstract public function rm_image($name);
  383. /**
  384. * Remove a page
  385. *
  386. * @param int $id - Page ID
  387. * @return bool
  388. */
  389. abstract public function rm_page($id);
  390. /**
  391. * Remove a tag
  392. *
  393. * @param int $id - Tag ID
  394. * @return bool
  395. */
  396. abstract public function rm_tag($id);
  397. /**
  398. * Update content
  399. *
  400. * @param int $content_id
  401. * @param str $content OPTIONAL
  402. * @param arr $tags OPTIONAL IMPORTANT! WILL REMOVE PREVIOUS TAGS AND TAG DATA!!!
  403. * - Tag name as key, value or array of values as value
  404. * set value as NULL if you want a simple tag
  405. * @return boolean
  406. */
  407. abstract public function update_content($content_id, $content = FALSE, $tags = FALSE);
  408. /**
  409. * Update image data
  410. * WARNING! Removes all previous data!
  411. *
  412. * @param str $image_name
  413. * @param arr $tags OPTIONAL IMPORTANT! WILL REMOVE PREVIOUS TAGS AND TAG DATA!!!
  414. * - Tag name as key, value or array of values as value
  415. * set value as NULL if you want a simple tag
  416. * @return boolean
  417. */
  418. abstract public function update_image_data($image_name, $tags = FALSE);
  419. /**
  420. * Update image name
  421. *
  422. * @param str $old_image_name
  423. * @param str $new_image_name
  424. * @return boolean
  425. */
  426. abstract public function update_image_name($old_image_name, $new_image_name);
  427. /**
  428. * Rename the physical files, should be called from the driver
  429. *
  430. * @param str $old_image_name
  431. * @param str $new_image_name
  432. * @return boolean
  433. */
  434. protected function rename_image_files($old_image_name, $new_image_name)
  435. {
  436. rename(Kohana::$config->load('user_content.dir').'/images/'.$old_image_name, Kohana::$config->load('user_content.dir').'/images/'.$new_image_name);
  437. $cwd = getcwd();
  438. chdir(Kohana::$config->load('user_content.dir').'/images');
  439. $cached_images = glob($old_image_name.'*');
  440. chdir($cwd);
  441. foreach ($cached_images as $cached_image) unlink(Kohana::$cache_dir.'/user_content/images/'.$cached_image);
  442. return TRUE;
  443. }
  444. /**
  445. * Update page data
  446. *
  447. * @param int $id - Page ID
  448. * @param str $name - Page name OPTIONAL
  449. * @param str $URI - Page URL OPTIONAL
  450. * @param arr $tag_ids - Array with template_field_id as key and array of tag_ids as value OPTIONAL
  451. * @return boolean
  452. */
  453. abstract public function update_page_data($id, $name = FALSE, $URI = FALSE, $tags = FALSE);
  454. }