PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/protected/models/Job.php

https://code.google.com/p/monkeys-job/
PHP | 287 lines | 192 code | 27 blank | 68 comment | 24 complexity | 4ff1e15e8dcaf983cc54158c3c0129f4 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /**
  3. * This is the model class for table "mj_jobs".
  4. *
  5. * The followings are the available columns in table 'mj_jobs':
  6. * @var integer $id
  7. * @var string $title
  8. * @var string $content
  9. * @var string $tags
  10. * @var integer $status
  11. * @var integer $create_time
  12. * @var integer $update_time
  13. * @var integer $author_id
  14. */
  15. class Job extends CActiveRecord
  16. {
  17. const STATUS_PENDING=1;
  18. const STATUS_APPROVED=2;
  19. private $_statuses;
  20. private $_oldTags;
  21. public $email;
  22. public $name;
  23. /**
  24. * Returns the static model of the specified AR class.
  25. * @return Job the static model class
  26. */
  27. public static function model($className=__CLASS__)
  28. {
  29. return parent::model($className);
  30. }
  31. /**
  32. * @return string the associated database table name
  33. */
  34. public function tableName()
  35. {
  36. return '{{jobs}}';
  37. }
  38. /**
  39. * @return array validation rules for model attributes.
  40. */
  41. public function rules()
  42. {
  43. // NOTE: you should only define rules for those attributes that
  44. // will receive user inputs.
  45. if (Yii::app()->user->isGuest) {
  46. return array(
  47. array('title, content, email, name', 'required'),
  48. array('email', 'email'),
  49. array('name', 'length', 'max'=>128),
  50. array('status', 'in', 'range'=>array(1,2)),
  51. array('title', 'length', 'max'=>128),
  52. array('tags', 'length', 'max'=>500),
  53. array('tags', 'normalizeTags'),
  54. array('title, status', 'safe', 'on'=>'search'),
  55. );
  56. } else {
  57. return array(
  58. array('title, content', 'required'),
  59. array('status', 'in', 'range'=>array(1,2)),
  60. array('title', 'length', 'max'=>128),
  61. array('tags', 'length', 'max'=>500),
  62. array('tags', 'normalizeTags'),
  63. array('title, status', 'safe', 'on'=>'search'),
  64. );
  65. }
  66. }
  67. /**
  68. * @return array relational rules.
  69. */
  70. public function relations()
  71. {
  72. // NOTE: you may need to adjust the relation name and the related
  73. // class name for the relations automatically generated below.
  74. return array(
  75. 'author' => array(self::BELONGS_TO, 'User', 'author_id'),
  76. 'requests' => array(self::HAS_MANY, 'JobRequest', 'job_id', 'order'=>'requests.create_time DESC'),
  77. 'requestCount' => array(self::STAT, 'JobRequest', 'job_id'),
  78. );
  79. }
  80. /**
  81. * @return array customized attribute labels (name=>label)
  82. */
  83. public function attributeLabels()
  84. {
  85. return array(
  86. 'id' => 'Id',
  87. 'email' => Yii::t('mj-base', 'Email'),
  88. 'name' => Yii::t('mj-base', 'Name'),
  89. 'title' => Yii::t('mj-base', 'Title'),
  90. 'content' => Yii::t('mj-base', 'Content'),
  91. 'tags' => Yii::t('mj-base', 'Tags'),
  92. 'status' => Yii::t('mj-base', 'Status'),
  93. 'create_time' => Yii::t('mj-base', 'Create Time'),
  94. 'update_time' => Yii::t('mj-base', 'Update Time'),
  95. 'author_id' => Yii::t('mj-base', 'Author'),
  96. );
  97. }
  98. /*
  99. * Return statuses for form
  100. */
  101. public function getStatuses()
  102. {
  103. if ($this->_statuses == null) {
  104. $this->_statuses = array(
  105. Job::STATUS_PENDING=>Yii::t('mj-base', 'Pending'),
  106. Job::STATUS_APPROVED=>Yii::t('mj-base', 'Approved'),
  107. );
  108. }
  109. return $this->_statuses;
  110. }
  111. public function getIsOwner()
  112. {
  113. if (Yii::app()->user->isGuest)
  114. return false;
  115. else
  116. return (Yii::app()->user->id == $this->author_id);
  117. }
  118. public function getShowRequests()
  119. {
  120. return ($this->getIsOwner() or UserModule::isAdmin());
  121. }
  122. /**
  123. * @return string the URL that shows the detail of the post
  124. */
  125. public function getUrl()
  126. {
  127. return Yii::app()->urlManager->createUrl('job/view', array(
  128. 'id'=>$this->id,
  129. 'title'=>$this->title,
  130. ));
  131. }
  132. public function getAbsoluteUrl()
  133. {
  134. return Yii::app()->getRequest()->getHostInfo().$this->getUrl();
  135. }
  136. public function getAdminLinks()
  137. {
  138. $links = '';
  139. $urlManager = Yii::app()->urlManager;
  140. if ($this->status != Job::STATUS_APPROVED) {
  141. $links .= CHtml::ajaxLink(Yii::t('mj-base', 'Approve'),
  142. $urlManager->createUrl('job/approve'),
  143. array('type' =>'POST', 'data'=>array('id'=>$this->id),
  144. 'success'=>'function (){$("#j'.$this->id.'").addClass("item-approved");}'));
  145. }
  146. $links .= CHtml::link(Yii::t('mj-base', 'Remove'),
  147. '#', array('onclick'=>'$("#decline-id").val("'.$this->id.'");$("#moderationDialog").dialog("open"); return false;'));
  148. return $links;
  149. }
  150. /**
  151. * @return array a list of links that point to the post list filtered by every tag of this post
  152. */
  153. public function getTagLinks()
  154. {
  155. $links=array();
  156. foreach(JTag::string2array($this->tags) as $tag)
  157. $links[]=CHtml::link(CHtml::encode($tag), array('job/index', 'tags'=>$tag));
  158. return $links;
  159. }
  160. /**
  161. * Normalizes the user-entered tags.
  162. */
  163. public function normalizeTags($attribute,$params)
  164. {
  165. $this->tags=JTag::array2string(array_unique(JTag::string2array($this->tags)));
  166. }
  167. /**
  168. * Adds a new comment to this post.
  169. * This method will set status and post_id of the comment accordingly.
  170. * @param Comment the comment to be added
  171. * @return boolean whether the comment is saved successfully
  172. */
  173. public function addRequest($request)
  174. {
  175. $request->job_id=$this->id;
  176. return $request->save();
  177. }
  178. /**
  179. * This is invoked when a record is populated with data from a find() call.
  180. */
  181. protected function afterFind()
  182. {
  183. parent::afterFind();
  184. $this->_oldTags=$this->tags;
  185. }
  186. /**
  187. * This is invoked before the record is saved.
  188. * @return boolean whether the record should be saved.
  189. */
  190. protected function beforeSave()
  191. {
  192. if(parent::beforeSave())
  193. {
  194. if($this->isNewRecord)
  195. {
  196. if ($this->author_id == null) {
  197. if (Yii::app()->user->isGuest) {
  198. $user = User::getUser($this->email, $this->name, true);
  199. if ($user->status == User::STATUS_ACTIVE or $user->status == User::STATUS_BANED) {
  200. $this->addError('email', 'Login for access to activated account');
  201. return false;
  202. }
  203. $this->author_id = $user->id;
  204. } else {
  205. if (UserModule::user()->status == User::STATUS_BANED) {
  206. $this->addError('email', 'You are banned. Contact Administrator for more information');
  207. return false;
  208. }
  209. $this->author_id = Yii::app()->user->id;
  210. }
  211. }
  212. if (UserModule::isAdmin() or Yii::app()->params['itemsNeedApproval'] == false) {
  213. $this->status = Job::STATUS_APPROVED;
  214. } else {
  215. $this->status = Job::STATUS_PENDING;
  216. }
  217. if ($this->create_time == null)
  218. $this->create_time=$this->update_time=time();
  219. }
  220. else
  221. $this->update_time=time();
  222. return true;
  223. }
  224. else
  225. return false;
  226. }
  227. /**
  228. * This is invoked after the record is saved.
  229. */
  230. protected function afterSave()
  231. {
  232. parent::afterSave();
  233. JTag::model()->updateFrequency($this->_oldTags, $this->tags);
  234. }
  235. /**
  236. * This is invoked after the record is deleted.
  237. */
  238. protected function afterDelete()
  239. {
  240. parent::afterDelete();
  241. JobRequest::model()->deleteAll('job_id='.$this->id);
  242. JTag::model()->updateFrequency($this->tags, '');
  243. }
  244. /**
  245. * Retrieves the list of posts based on the current search/filter conditions.
  246. * @return CActiveDataProvider the data provider that can return the needed posts.
  247. */
  248. public function search()
  249. {
  250. $criteria=new CDbCriteria;
  251. $criteria->compare('title',$this->title,true);
  252. $criteria->compare('status',$this->status);
  253. return new CActiveDataProvider('Job', array(
  254. 'criteria'=>$criteria,
  255. 'sort'=>array(
  256. 'defaultOrder'=>'status, update_time DESC',
  257. ),
  258. ));
  259. }
  260. }