Guest User

Untitled

a guest
Jun 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. <?php
  2. class Page extends AppModel {
  3.  
  4. var $name = 'Page';
  5.  
  6. function __construct(){
  7. parent::__construct();
  8.  
  9. $this->validate = array(
  10. 'name' => array(
  11. 'rule' => array('minLength', 1),
  12. 'message' => __('Name is required', true)
  13. ),
  14. 'title' => array(
  15. 'rule' => array('minLength', 1),
  16. 'message' => __('Title is required', true)
  17. ),
  18. 'content' => array(
  19. 'rule' => array('minLength', 1),
  20. 'message' => __('Content is required', true)
  21. ),
  22. 'email' => array(
  23. 'email' => array(
  24. 'rule' => 'email',
  25. 'message' => __('The email address you entered is not valid.', true)
  26. ),
  27. 'minLength' => array(
  28. 'rule' => array('minLength', 1),
  29. 'message' => __('Email address is required.', true)
  30. )
  31. ),
  32. 'message' => array(
  33. 'rule' => array('minLength', 1),
  34. 'message' => __('Enquiry is required', true)
  35. ),
  36. );
  37. }
  38.  
  39. /**
  40. * Override parent before save for slug generation
  41. *
  42. * @return boolean Always true
  43. */
  44. function beforeSave(){
  45. if(!empty($this->data)) {
  46. // Generating slug from page name
  47. if(!empty($this->data['Page']['name'])) {
  48. if(!empty($this->data['Page']['id'])) {
  49. $this->data['Page']['slug'] = $this->generateNiceName($this->data['Page']['name'], $this->data['Page']['id']);
  50. } else {
  51. $this->data['Page']['slug'] = $this->generateNiceName($this->data['Page']['name']);
  52. }
  53. }
  54.  
  55. // the page ordering
  56. if(!empty($this->data['Page']['id'])) {
  57. if(empty($this->data['Page']['top_show'])) {
  58. $this->data['Page']['top_order'] = 0;
  59. }
  60. if(empty($this->data['Page']['bottom_show'])) {
  61. $this->data['Page']['bottom_order'] = 0;
  62. }
  63. } else {
  64. if(!empty($this->data['Page']['top_show'])) {
  65. $this->data['Page']['top_order'] = $this->getLastOrderNumber('top');
  66. }
  67. if(!empty($this->data['Page']['bottom_show'])) {
  68. $this->data['Page']['bottom_order'] = $this->getLastOrderNumber('bottom');
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74.  
  75. /**
  76. * Function to get last order number
  77. *
  78. * @return int Return last order number
  79. */
  80. function getLastOrderNumber($position = null) {
  81. $this->recursive = -1;
  82. $lastItem = $this->find('first', array('conditions' => array($position.'_show' => 1), 'order' => array($position.'_order' => 'desc')));
  83. if(!empty($lastItem)) {
  84. return $lastItem['Page'][$position.'_order'] + 1;
  85. } else {
  86. return 0;
  87. }
  88. }
  89. }
  90. ?>
Add Comment
Please, Sign In to add comment