PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/content/models/Page.php

https://bitbucket.org/graaaf/erso
PHP | 151 lines | 112 code | 39 blank | 0 comment | 6 complexity | 5c06d92d06e6784c50a787d0f78ac21b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. class Page extends ActiveRecordModel
  3. {
  4. const PAGE_SIZE = 10;
  5. public $god;
  6. public function name()
  7. {
  8. return 'Страницы';
  9. }
  10. public static function model($className=__CLASS__)
  11. {
  12. return parent::model($className);
  13. }
  14. public function tableName()
  15. {
  16. return 'pages';
  17. }
  18. public function behaviors()
  19. {
  20. return CMap::mergeArray(parent::behaviors(), array(
  21. 'MetaTag' => array('class' => 'application.components.activeRecordBehaviors.MetaTagBehavior'),
  22. 'FileManager' => array('class' => 'application.components.activeRecordBehaviors.AttachmentBehavior', 'attached_model' => 'FileManager'),
  23. ));
  24. }
  25. public function rules()
  26. {
  27. return array(
  28. array('title, lang', 'required'),
  29. array('is_published', 'numerical', 'integerOnly' => true),
  30. array('url', 'length', 'max' => 250),
  31. array('title', 'length', 'max' => 200),
  32. array('text, url', 'safe'),
  33. array('meta_tags, god', 'safe'),
  34. array('title', 'filter', 'filter' => 'strip_tags'),
  35. array('id, title, url, text, is_published, updated_at', 'safe', 'on' => 'search'),
  36. );
  37. }
  38. public function scope()
  39. {
  40. return array(
  41. 'published' => array(
  42. 'condition' => 'is_published = 1',
  43. 'ord' => 'ord',
  44. ),
  45. );
  46. }
  47. public function relations()
  48. {
  49. return array(
  50. 'language' => array(self::BELONGS_TO, 'Language', 'lang')
  51. );
  52. }
  53. public function in($row, $values, $operator = 'AND')
  54. {
  55. $this->getDbCriteria()->addInCondition($row, $values, $operator);
  56. return $this;
  57. }
  58. public function beforeSave()
  59. {
  60. $this->updated_at = new CDbExpression('NOW()');
  61. return parent::beforeSave();
  62. }
  63. public function search()
  64. {
  65. $criteria = new CDbCriteria;
  66. $criteria->compare('id', $this->id, true);
  67. $criteria->compare('title', $this->title, true);
  68. $criteria->compare('url', $this->url, true);
  69. $criteria->compare('text', $this->text, true);
  70. $criteria->compare('is_published', $this->is_published);
  71. $criteria->compare('updated_at', $this->updated_at, true);
  72. return new ActiveDataProvider(get_class($this), array(
  73. 'criteria' => $criteria,
  74. 'sort' => array(
  75. 'defaultOrder' => 'ord',
  76. ),
  77. ));
  78. }
  79. public function getHref()
  80. {
  81. $url = trim($this->url);
  82. if ($url)
  83. {
  84. if ($url[0] != "/")
  85. $url = "/{$url}";
  86. return $url;
  87. }
  88. else
  89. {
  90. return "/page/" . $this->id;
  91. }
  92. }
  93. public function afterSave()
  94. {
  95. if ($this->url != '')
  96. {
  97. $routes = array_merge(array($this->url => "content/page/view/id/{$this->id}"), include(PROTECTED_PATH . 'config/routes.php'));
  98. file_put_contents(PROTECTED_PATH . 'config/routes.php', "<?php \n return " . var_export($routes, true) . ";");
  99. }
  100. parent::afterSave();
  101. return true;
  102. }
  103. public function getContent()
  104. {
  105. $content = $this->text;
  106. if (RbacModule::isAllow('PageAdmin_Update'))
  107. {
  108. $content.= "<br/><a href='/content/pageAdmin/update/id/{$this->id}' class='admin_link'>Редактировать</a>";
  109. }
  110. return $content;
  111. }
  112. }