Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| MySqlGroup | |
0.00% |
0 / 75 |
|
0.00% |
0 / 8 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| fresh | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getUsers | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| fetchUsers | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| addUser | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| removeUser | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| store | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| delete | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace UserFrosting; |
| 4 | |
| 5 | class MySqlGroup extends MySqlDatabaseObject implements GroupObjectInterface { |
| 6 | |
| 7 | protected $_users; |
| 8 | |
| 9 | public function __construct($properties, $id = null) { |
| 10 | $this->_table = static::getTable('group'); |
| 11 | parent::__construct($properties, $id); |
| 12 | } |
| 13 | public function fresh(){ |
| 14 | // Update table and column info, in case it has changed |
| 15 | $this->_table = static::getTable('group'); |
| 16 | $group = new Group(parent::fresh(), $this->_id); |
| 17 | $group->_groups = $this->fetchUsers(); |
| 18 | return $group; |
| 19 | } |
| 20 | |
| 21 | public function getUsers(){ |
| 22 | if(!isset($this->_users)) |
| 23 | $this->_users = $this->fetchUsers(); |
| 24 | return $this->_users; |
| 25 | } |
| 26 | // Fetch a collection of Users which belong to this group. |
| 27 | public function fetchUsers() { |
| 28 | //Get connected and load the group_user table |
| 29 | $db = static::connection(); |
| 30 | $link_table = static::getTable('group_user')->name; |
| 31 | |
| 32 | $sqlVars[":id"] = $this->_id; |
| 33 | |
| 34 | $query = " |
| 35 | SELECT user_id FROM `$link_table` |
| 36 | WHERE group_id = :id"; |
| 37 | |
| 38 | $stmt = $db->prepare($query); |
| 39 | $stmt->execute($sqlVars); |
| 40 | |
| 41 | //Get the array of users in this group |
| 42 | $users_array= []; |
| 43 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
| 44 | $user = UserLoader::fetch($row['user_id']); |
| 45 | // Only add valid user objects, skip false values |
| 46 | if ($user !== false) { |
| 47 | $users_array[] = $user; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return $users_array; |
| 52 | } |
| 53 | //Add a specified user to this group. Won't be stored in DB until store() is called. |
| 54 | public function addUser($user_id) { |
| 55 | |
| 56 | $this->getUsers(); |
| 57 | |
| 58 | //Check if User already exists in this group |
| 59 | if(isset($this->_users[$user_id])) |
| 60 | return $this; |
| 61 | |
| 62 | //Check that the user actually exists |
| 63 | if(!UserLoader::exists($user_id)) |
| 64 | throw new \Exception("The specified user_id ($user_id) does not exist."); |
| 65 | |
| 66 | $this->_users[$user_id] = UserLoader::fetch($user_id); |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | //Remove a specified user from this group. Won't be stored in DB until store() is called. |
| 71 | public function removeUser($user_id) { |
| 72 | |
| 73 | $this->getUsers(); |
| 74 | |
| 75 | //Check to see if the user exists in this group |
| 76 | if(!isset($this->_users[$user_id])) |
| 77 | return $this; |
| 78 | |
| 79 | //Go ahead and remove the user from this group |
| 80 | unset($this->_users[$user_id]); |
| 81 | |
| 82 | |
| 83 | return $this; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | public function store(){ |
| 88 | // If this is being set as the default primary group, then any other group must be demoted to default group |
| 89 | if ($this->is_default == GROUP_DEFAULT_PRIMARY){ |
| 90 | $db = static::connection(); |
| 91 | |
| 92 | $query = " |
| 93 | UPDATE `{$this->_table->name}` |
| 94 | SET is_default = " . GROUP_DEFAULT . |
| 95 | " WHERE is_default = " . GROUP_DEFAULT_PRIMARY . |
| 96 | ";"; |
| 97 | |
| 98 | $stmt = $db->prepare($query); |
| 99 | $stmt->execute(); |
| 100 | } |
| 101 | |
| 102 | // Now store this group |
| 103 | parent::store(); |
| 104 | |
| 105 | // Store function should always return the id of the object |
| 106 | return $this->_id; |
| 107 | } |
| 108 | |
| 109 | /*** Delete this group from the database, along with any linked user and authorization rules |
| 110 | ***/ |
| 111 | public function delete(){ |
| 112 | // Can only delete an object where `id` is set |
| 113 | if (!$this->_id) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | $result = parent::delete(); |
| 118 | |
| 119 | // Get connection |
| 120 | $db = static::connection(); |
| 121 | $link_table = static::getTable('group_user')->name; |
| 122 | $auth_table = static::getTable('authorize_group')->name; |
| 123 | |
| 124 | $sqlVars[":id"] = $this->_id; |
| 125 | |
| 126 | $query = " |
| 127 | DELETE FROM `$link_table` |
| 128 | WHERE group_id = :id"; |
| 129 | |
| 130 | $stmt = $db->prepare($query); |
| 131 | $stmt->execute($sqlVars); |
| 132 | |
| 133 | $query = " |
| 134 | DELETE FROM `$auth_table` |
| 135 | WHERE group_id = :id"; |
| 136 | |
| 137 | $stmt = $db->prepare($query); |
| 138 | $stmt->execute($sqlVars); |
| 139 | |
| 140 | // Reassign any primary users to the current default primary group |
| 141 | $default_primary_group = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, 'is_default'); |
| 142 | |
| 143 | $user_table = static::getTable('user')->name; |
| 144 | |
| 145 | $query = " |
| 146 | UPDATE `$user_table` |
| 147 | SET primary_group_id = :primary_group_id |
| 148 | WHERE primary_group_id = :current_id;"; |
| 149 | |
| 150 | $sqlVars = [ |
| 151 | ":primary_group_id" => $default_primary_group->id, |
| 152 | ":current_id" => $this->_id |
| 153 | ]; |
| 154 | |
| 155 | $stmt = $db->prepare($query); |
| 156 | $stmt->execute($sqlVars); |
| 157 | |
| 158 | // TODO: assign user to the default primary group as well? |
| 159 | |
| 160 | return $result; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | ?> |