11<?php
22/**
33 * Status behavior for models.
4- *
5- * @author Veaceslav Medvedev <slavcopost@gmail.com>
6- * @link http://code.google.com/p/yiiext
7- *
4+ * @author Veaceslav Medvedev <slavcopost@gmail.com>
5+ * @link http://code.google.com/p/yiiext
86 * @version 0.6
97 */
108class EStatusBehavior extends CActiveRecordBehavior
@@ -13,31 +11,38 @@ class EStatusBehavior extends CActiveRecordBehavior
1311 * @var string the name of the table field where data is stored.
1412 * Required to set on init behavior. No default.
1513 */
16- public $ statusField = NULL ;
14+ public $ statusField = null ;
1715 /**
1816 * @var array the possible statuses.
19- * Default draft, published, archived
17+ * Default draft, published, archived
2018 * @see setStatuses
2119 */
2220 public $ statuses = array ('draft ' , 'published ' , 'archived ' );
2321
24- protected $ _status = NULL ;
22+ protected $ _status = null ;
2523 protected $ _statusText = 'unknown ' ;
26-
24+
2725 /**
2826 * Check required properties and attaches the behavior object to the component.
27+ *
2928 * @param CComponent owner component.
29+ *
3030 * @throws CException if required properties not set.
3131 */
3232 public function attach ($ owner )
3333 {
3434 // Check required var statusField.
35- if (!is_string ($ this ->statusField ) || empty ($ this ->statusField ))
36- throw new CException (self ::t ('yii ' , 'Property "{class}.{property}" is not defined. ' ,
37- array ('{class} ' => get_class ($ this ), '{property} ' => 'statusField ' )));
35+ if (!is_string ($ this ->statusField ) || empty ($ this ->statusField )) {
36+ throw new CException (self ::t (
37+ 'yii ' ,
38+ 'Property "{class}.{property}" is not defined. ' ,
39+ array ('{class} ' => get_class ($ this ), '{property} ' => 'statusField ' )
40+ ));
41+ }
3842
3943 parent ::attach ($ owner );
4044 }
45+
4146 /**
4247 * @return string the status.
4348 * @see getStatus
@@ -46,66 +51,102 @@ public function __toString()
4651 {
4752 return $ this ->getStatus ();
4853 }
54+
4955 /**
5056 * Init valid statuses values.
57+ *
5158 * @param array valid values for status.
59+ *
5260 * @return CActiveRecord owner model.
5361 */
5462 public function setStatuses ($ statuses )
5563 {
56- $ this ->statuses = is_array ($ statuses ) && !empty ($ statuses ) ? $ statuses : array ('draft ' , 'published ' , 'archived ' );
64+ $ this ->statuses = is_array ($ statuses ) && !empty ($ statuses ) ? $ statuses :
65+ array ('draft ' , 'published ' , 'archived ' );
5766
5867 return $ this ->getOwner ();
5968 }
69+
6070 /**
6171 * @return string status value.
6272 */
6373 public function getStatus ()
6474 {
6575 return $ this ->_status ;
6676 }
77+
6778 /**
6879 * @return string status text.
6980 */
7081 public function getStatusText ()
7182 {
7283 return $ this ->_statusText ;
7384 }
85+
7486 /**
7587 * Set status for model.
88+ *
7689 * @param string status value or status text for model.
90+ *
7791 * @return CActiveRecord owner model.
7892 * @throws CException if status invalid.
7993 */
8094 public function setStatus ($ status )
8195 {
82- if (isset ($ this ->statuses [$ status ]))
96+ if (isset ($ this ->statuses [$ status ])) {
8397 $ this ->_status = $ status ;
84- else if (($ this ->_status = array_search ($ status , $ this ->statuses )) === FALSE )
85- throw new CException (Yii::t ('yiiext ' , 'Status "{status}" is not allowed. ' , array ('{status} ' => $ status )));
98+ } else {
99+ if (($ this ->_status = array_search ($ status , $ this ->statuses )) === false ) {
100+ throw new CException (Yii::t (
101+ 'yiiext ' ,
102+ 'Status "{status}" is not allowed. ' ,
103+ array ('{status} ' => $ status )
104+ ));
105+ }
106+ }
86107
87108 $ this ->_statusText = $ this ->statuses [$ this ->_status ];
88109 $ this ->getOwner ()->setAttribute ($ this ->statusField , $ this ->_status );
89110
90111 return $ this ->getOwner ();
91112 }
113+
92114 /**
93115 * Save status. Will be save only status attribute for model.
94116 * @return boolean whether the saving succeeds.
95117 */
96118 public function saveStatus ()
97119 {
98- return $ this ->getOwner ()->save (TRUE , array ($ this ->statusField ));
120+ return $ this ->getOwner ()->save (true , array ($ this ->statusField ));
99121 }
122+
100123 /**
101124 * Load status after find model.
125+ *
102126 * @param CEvent
103127 */
104128 public function afterFind ($ event )
105129 {
106- $ this ->_status = $ this ->getOwner ()->getAttribute ($ this ->statusField );
130+ $ this ->_status = $ this ->getOwner ()->getAttribute ($ this ->statusField );
107131 $ this ->_statusText = isset ($ this ->statuses [$ this ->_status ]) ? $ this ->statuses [$ this ->_status ] : 'unknown ' ;
108132
109133 parent ::afterFind ($ event );
110134 }
135+
136+ public function hasStatus ($ statuses = array ())
137+ {
138+ if (!is_array ($ statuses )) {
139+ $ statuses = explode (', ' , $ statuses );
140+ }
141+ return in_array ($ this ->getOwner ()->getAttribute ($ this ->statusField ), $ statuses );
142+ }
143+
144+ public function status ($ status )
145+ {
146+ $ criteria = $ this ->owner ->getDbCriteria ();
147+ $ column = $ this ->owner ->getDbConnection ()->quoteColumnName ($ owner ->getTableAlias ().'. ' .$ this ->statusField );
148+ $ criteria ->addCondition ($ column .'= ' .CDbCriteria::PARAM_PREFIX .CDbCriteria::$ paramCount );
149+ $ criteria ->params [CDbCriteria::PARAM_PREFIX .CDbCriteria::$ paramCount ++]=$ status ;
150+ return $ this ->owner ;
151+ }
111152}
0 commit comments