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

/models/post.php

https://github.com/felixding/LonelyThinker
PHP | 69 lines | 36 code | 5 blank | 28 comment | 2 complexity | dc42cf7e79e46f05e3c7c9b6e755aaf6 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* SVN FILE: $Id: post.php 9 2009-04-23 13:45:47Z $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 5
  9. *
  10. * Licensed under The BSD License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @filesource
  14. * @copyright Copyright 2007-2009, Felix Ding (http://dingyu.me)
  15. * @link http://lonelythinker.org Project LonelyThinker
  16. * @package LonelyThinker
  17. * @author $LastChangedBy: $
  18. * @version $Revision: 9 $
  19. * @modifiedby $LastChangedBy: $
  20. * @lastmodified $Date: 2009-04-23 21:45:47 +0800 (Thu, 23 Apr 2009) $
  21. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  22. */
  23. class Post extends AppModel
  24. {
  25. var $name = 'Post';
  26. var $hasMany = array(
  27. 'Comment'=>array(
  28. 'conditions' => array('Comment.status' => 'published')
  29. ),
  30. 'RelatedPost'
  31. );
  32. var $hasAndBelongsToMany = array('Tag');
  33. var $validate = array(
  34. 'title' => array('rule'=>'notEmpty', 'message'=>'You forgot to name a title for the post'),
  35. 'slug' => array(
  36. 'notEmpty'=>array('rule'=>'notEmpty', 'message'=>"You forgot to give a slug for the post"),
  37. 'isUnique'=>array('rule'=>'isUnique', 'message'=>"This slug has already been taken"),
  38. 'custom'=>array('rule' =>array('custom', '/^[a-z0-9(\-)]{3,}$/'), 'message'=>"A valid slug must be 3 characters long at least, and consist of lower case English letters, numbers and '-' inbetween only")
  39. ),
  40. 'body' => array('rule'=>'notEmpty', 'message'=>'Nothing to say?')
  41. );
  42. /**
  43. * callbacks
  44. */
  45. public function beforeValidate()
  46. {
  47. //if the post doesn't belong to any tags, we just invalidate the form
  48. if(empty($this->data['Tag']['Tag']))
  49. {
  50. $this->invalidate('tag');
  51. }
  52. //slugify
  53. $this->data['Post']['slug'] = Inflector::slug($this->data['Post']['slug'], '-');
  54. return true;
  55. }
  56. public function afterSave()
  57. {
  58. //clean the cache
  59. Cache::clear();
  60. //(re)generate related posts' cache
  61. $this->requestAction('/related_posts/index/'.$this->getLastInsertId());
  62. }
  63. }
  64. ?>