PageRenderTime 38ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/application/protected/modules/core/models/Tags.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 173 lines | 91 code | 5 blank | 77 comment | 0 complexity | 3bb9e6cecdf38049d592bb2ae5e7c53a MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * This is the model base class for the table "tags".
  4. *
  5. * Columns in table "tags" available as properties of the model:
  6. * @property integer $id
  7. * @property string $name
  8. * @property integer $frequency
  9. *
  10. * There are no model relations.
  11. */
  12. class Tags extends BaseActiveRecord{
  13. public static function model($className=__CLASS__)
  14. {
  15. return parent::model($className);
  16. }
  17. /**
  18. * Initializes this model.
  19. */
  20. public function init()
  21. {
  22. return parent::init();
  23. }
  24. /**
  25. * This magic method is used for setting a string value for the object. It will be used if the object is used as a string.
  26. * @return string representing the object
  27. */
  28. public function __toString() {
  29. return (string) $this->name;
  30. }
  31. /**
  32. * @return string name of the class table
  33. */
  34. public function tableName()
  35. {
  36. return 'tags';
  37. }
  38. /**
  39. * Define validation rules
  40. */
  41. public function rules()
  42. {
  43. return array(
  44. array('name', 'required'),
  45. array('frequency', 'default', 'setOnEmpty' => true, 'value' => null),
  46. array('frequency', 'numerical', 'integerOnly'=>true),
  47. array('name', 'length', 'max'=>128),
  48. array('name, frequency', 'safe', 'on'=>'search'),
  49. );
  50. }
  51. /**
  52. * Relation to other models
  53. */
  54. public function relations()
  55. {
  56. return array(
  57. );
  58. }
  59. /**
  60. * Attribute labels
  61. */
  62. public function attributeLabels()
  63. {
  64. return array(
  65. 'name' => Yii::t('tags', 'Name'),
  66. 'frequency' => Yii::t('tags', 'Frequency'),
  67. );
  68. }
  69. /**
  70. * Which attribute are safe for search
  71. */
  72. public function search()
  73. {
  74. $criteria=new CDbCriteria;
  75. $criteria->compare('name', $this->name, true);
  76. $criteria->compare('frequency', $this->frequency);
  77. return new CActiveDataProvider(get_class($this), array(
  78. 'criteria'=>$criteria,
  79. ));
  80. }
  81. /**
  82. * Provide default sorting and optional condition
  83. */
  84. public function defaultScope() {
  85. return array(
  86. 'order' => 'name ASC',
  87. );
  88. }
  89. /**
  90. * Run before validate()
  91. */
  92. protected function beforeValidate() {
  93. return parent::beforeValidate();
  94. }
  95. /**
  96. * Run after validate()
  97. */
  98. protected function afterValidate() {
  99. return parent::afterValidate();
  100. }
  101. /**
  102. * Run before save()
  103. */
  104. protected function beforeSave() {
  105. return parent::beforeSave();
  106. }
  107. /**
  108. * Run after save()
  109. */
  110. protected function afterSave() {
  111. return parent::afterSave();
  112. }
  113. /**
  114. * Run before delete()
  115. */
  116. protected function beforeDelete() {
  117. return parent::beforeDelete();
  118. }
  119. /**
  120. * Run after delete()
  121. */
  122. protected function afterDelete() {
  123. return parent::afterDelete();
  124. }
  125. /**
  126. * Configure additional behaviors
  127. *
  128. public function behaviors()
  129. {
  130. return array_merge(
  131. array(
  132. 'BehaviourName' => array(
  133. 'class' => 'CWhateverBehavior'
  134. )
  135. ),
  136. parent::behaviors()
  137. );
  138. }
  139. */
  140. /**
  141. * Helper function for Tags
  142. */
  143. function getLink($method = "view") {
  144. return CHtml::link($this->name, array("/core/tags/view", "id" => $this->id),
  145. array("title" => CHtml::encode($this->name)));
  146. }
  147. /**
  148. * Suggests a list of existing tags matching the specified keyword.
  149. * @param string the keyword to be matched
  150. * @param integer maximum number of tags to be returned
  151. * @return array list of matching tag names
  152. */
  153. public function suggestTags($keyword,$limit=20)
  154. {
  155. $tags=$this->findAll(array(
  156. 'condition'=>'name LIKE :keyword',
  157. 'order'=>'frequency DESC, Name',
  158. 'limit'=>$limit,
  159. 'params'=>array(
  160. ':keyword'=>'%'.strtr($keyword,array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')).'%',
  161. ),
  162. ));
  163. $names=array();
  164. foreach($tags as $tag)
  165. $names[]=$tag->name;
  166. return $names;
  167. }
  168. }