Advertisement
fernandezekiel

Untitled

Oct 28th, 2012
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * This is the model class for table "userGroups_group".
  5. *
  6. * The followings are the available columns in table 'userGroups_group':
  7. * @property string $id
  8. * @property string $groupname
  9. * @property string $level
  10. * @property string $home
  11. * @property string $branch_id :: dagdag ni ezekiel
  12. */
  13. class UserGroupsGroup extends CActiveRecord
  14. {
  15.  
  16. /**
  17. * contains the group access permission's array
  18. * @var array
  19. */
  20. public $access;
  21.  
  22. /**
  23. * Returns the static model of the specified AR class.
  24. * @return UserGroupsGroup the static model class
  25. */
  26. public static function model($className=__CLASS__)
  27. {
  28. return parent::model($className);
  29. }
  30.  
  31. /**
  32. * @return string the associated database table name
  33. */
  34. public function tableName()
  35. {
  36. return Yii::app()->db->tablePrefix.'usergroups_group';
  37. }
  38.  
  39. /**
  40. * @return array validation rules for model attributes.
  41. */
  42. public function rules()
  43. {
  44. // NOTE: you should only define rules for those attributes that
  45. // will receive user inputs.
  46. return array(
  47. array('groupname', 'length', 'max'=>120),
  48. array('groupname', 'unique'),
  49. array('branch_id', 'length', 'max'=>120), // dagdag ni eze
  50. array('level', 'levelCheck'),
  51. array('home', 'safe'),
  52. // rules used on creation
  53. array('groupname', 'required', 'on'=>'admin'),
  54. // The following rule is used by search().
  55. // Please remove those attributes that should not be searched.
  56. array('id, groupname, level, branch_id, relUserGroupsBranch.branchname', 'safe', 'on'=>'search'),
  57. );
  58. }
  59.  
  60. /**
  61. * If a new level value is provided it makes sure that it won't be higher then the
  62. * one of the user updating or creating the group.
  63. * This is the 'authenticate' validator as declared in rules().
  64. */
  65. public function levelCheck($attribute,$params)
  66. {
  67. // skip this check on installation
  68. if ($this->scenario === 'installation')
  69. return true;
  70. if ($this->$attribute >= Yii::app()->user->level)
  71. $this->addError('level', Yii::t('userGroupsModule.admin','You cannot set a Group Level equal or superior to your own'));
  72. else if ($this->$attribute >= UserGroupsUser::ROOT_LEVEL)
  73. $this->addError('level', Yii::t('userGroupsModule.admin','You cannot set a Group Level equal or higher then Root'));
  74. }
  75.  
  76. /**
  77. * @return array relational rules.
  78. */
  79. public function relations()
  80. {
  81. Yii::import('userGroups.models.UserGroupsBranch');
  82. // define basic relation with groups
  83. $relations = array('relUserGroupsBranch' => array(self::BELONGS_TO, 'UserGroupsBranch', 'branch_id')); // dagdag ni eze
  84. // NOTE: you may need to adjust the relation name and the related
  85. // class name for the relations automatically generated below.
  86. return $relations; // edit by eze
  87. }
  88.  
  89. /**
  90. * @return array customized attribute labels (name=>label)
  91. */
  92. public function attributeLabels()
  93. {
  94. return array(
  95. 'id' => 'ID',
  96. 'groupname' => Yii::t('userGroupsModule.general','Group Name'),
  97. 'level' => Yii::t('userGroupsModule.general', 'Level'),
  98. 'branch_id' => 'branch ID',
  99. 'relUserGroupsBranch.branchname' => 'branch Neym'
  100. );
  101. }
  102.  
  103. /**
  104. * Retrieves a list of models based on the current search/filter conditions.
  105. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  106. */
  107. public function search()
  108. {
  109. // Warning: Please modify the following code to remove attributes that
  110. // should not be searched.
  111. Yii::import('userGroups.models.UserGroupsBranch');
  112. $criteria=new CDbCriteria;
  113. /* added by eze */
  114.  
  115. $criteria->with = array('relUserGroupsBranch');
  116. $criteria->select='branchname';
  117. $criteria->together = true;
  118. $criteria->order = 'level DESC';
  119. $criteria->compare('id',$this->id,true);
  120. $criteria->compare('groupname',$this->groupname,true);
  121. $criteria->compare('level',$this->level,true);
  122. $criteria->compare('level <',Yii::app()->user->level -1,false);
  123.  
  124. if(Yii::app()->user->id == UserGroupsUser::ROOT || Yii::app()->user->branch == UserGroupsBranch::ROOT) // if it is the root user
  125. {
  126. //edited by ezekiel un g level dating less than langm, ngaun naging less than or equal
  127. }
  128. else
  129. { //kapag under ka ng isang branch, ang magagawa mo lang na users is under lang din ng branch na kasama ka
  130. $criteria->compare('branch_id',Yii::app()->user->branch,false);//edited by ezekiel un g level dating less than langm, ngaun naging less than or equal
  131. }
  132.  
  133.  
  134. //$criteria->with('relUserGroupsBranch');
  135. return new CActiveDataProvider(get_class($this), array(
  136. 'criteria'=>$criteria,
  137. 'pagination'=>array('pageSize' => 10),
  138. ));
  139. }
  140.  
  141. /**
  142. * parameters preparation after a select is executed
  143. */
  144. public function afterFind()
  145. {
  146. // load the access permissions for the group
  147. $this->access = UserGroupsAccess::findRules(UserGroupsAccess::GROUP, $this->id);
  148. parent::afterFind();
  149. }
  150.  
  151. /**
  152. * return the group array list
  153. * @return Array
  154. */
  155. public static function groupList()
  156. {
  157. $user_branch_id = Yii::app()->user->branch;
  158. $arrayData = array();
  159. $criteria=new CDbCriteria;
  160. $criteria->order = 'level DESC';
  161. $criteria->compare('level <',Yii::app()->user->level -1,false);
  162.  
  163. if(Yii::app()->user->id == UserGroupsUser::ROOT || Yii::app()->user->branch == UserGroupsBranch::ROOT) // if it is the root user
  164. {
  165. //edited by ezekiel un g level dating less than langm, ngaun naging less than or equal
  166. }
  167. else
  168. { //kapag under ka ng isang branch, ang magagawa mo lang na users is under lang din ng branch na kasama ka
  169. $criteria->compare('branch_id',Yii::app()->user->branch,false);//edited by ezekiel un g level dating less than langm, ngaun naging less than or equal
  170. }
  171.  
  172. $result = self::model()->findAll($criteria);
  173. foreach ($result as $r) {
  174. $arrayData[$r->id] = $r->groupname;
  175. }
  176. return $arrayData;
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement