/objects/userGroups.php

https://github.com/DanielnetoDotCom/YouPHPTube · PHP · 380 lines · 329 code · 44 blank · 7 comment · 53 complexity · 41bbf4d13be0b2d336af8dc4f49b121c MD5 · raw file

  1. <?php
  2. if (empty($global['systemRootPath'])) {
  3. $global['systemRootPath'] = '../';
  4. }
  5. require_once $global['systemRootPath'] . 'videos/configuration.php';
  6. require_once $global['systemRootPath'] . 'objects/bootGrid.php';
  7. require_once $global['systemRootPath'] . 'objects/user.php';
  8. class UserGroups {
  9. private $id;
  10. private $group_name;
  11. function __construct($id, $group_name = "") {
  12. if (empty($id)) {
  13. $group_name = _substr($group_name, 0, 255);
  14. // get the category data from category and pass
  15. $this->group_name = $group_name;
  16. } else {
  17. // get data from id
  18. $this->load($id);
  19. }
  20. }
  21. private function load($id) {
  22. $user = self::getUserGroupsDb($id);
  23. if (empty($user))
  24. return false;
  25. foreach ($user as $key => $value) {
  26. $this->$key = $value;
  27. }
  28. }
  29. static private function getUserGroupsDb($id) {
  30. global $global;
  31. $id = intval($id);
  32. $sql = "SELECT * FROM users_groups WHERE id = ? LIMIT 1";
  33. $res = sqlDAL::readSql($sql, "i", array($id));
  34. $data = sqlDAL::fetchAssoc($res);
  35. sqlDAL::close($res);
  36. if (!empty($data)) {
  37. $user = $data;
  38. } else {
  39. $user = false;
  40. }
  41. return $user;
  42. }
  43. function save() {
  44. global $global;
  45. if (empty($this->isAdmin)) {
  46. $this->isAdmin = "false";
  47. }
  48. $formats = "";
  49. $values = array();
  50. $this->group_name = _substr($this->group_name, 0, 255);
  51. if (!empty($this->id)) {
  52. $sql = "UPDATE users_groups SET group_name = ?, modified = now() WHERE id = ?";
  53. $formats = "si";
  54. $values = array($this->group_name,$this->id);
  55. } else {
  56. $sql = "INSERT INTO users_groups ( group_name, created, modified) VALUES (?,now(), now())";
  57. $formats = "s";
  58. $values = array($this->group_name);
  59. }
  60. return sqlDAL::writeSql($sql,$formats,$values);
  61. }
  62. function delete() {
  63. if (!User::isAdmin()) {
  64. return false;
  65. }
  66. global $global;
  67. if (!empty($this->id)) {
  68. $sql = "DELETE FROM users_groups WHERE id = ?";
  69. } else {
  70. return false;
  71. }
  72. return sqlDAL::writeSql($sql,"i",array($this->id));
  73. }
  74. private function getUserGroup($id) {
  75. global $global;
  76. $id = intval($id);
  77. $sql = "SELECT * FROM users_groups WHERE id = ? LIMIT 1";
  78. $res = sqlDAL::readSql($sql, "i", array($id));
  79. $data = sqlDAL::fetchAssoc($res);
  80. sqlDAL::close($res);
  81. if (!empty($data)) {
  82. $category = $data;
  83. } else {
  84. $category = false;
  85. }
  86. return $category;
  87. }
  88. static function getAllUsersGroups() {
  89. global $global;
  90. $sql = "SELECT *,"
  91. . " (SELECT COUNT(*) FROM videos_group_view WHERE users_groups_id = ug.id ) as total_videos, "
  92. . " (SELECT COUNT(*) FROM users_has_users_groups WHERE users_groups_id = ug.id ) as total_users "
  93. . " FROM users_groups as ug WHERE 1=1 ";
  94. $sql .= BootGrid::getSqlFromPost(array('group_name'));
  95. $res = sqlDAL::readSql($sql);
  96. $fullData = sqlDAL::fetchAllAssoc($res);
  97. sqlDAL::close($res);
  98. $arr = array();
  99. if ($res!=false) {
  100. foreach ($fullData as $row) {
  101. $arr[] = $row;
  102. }
  103. //$category = $res->fetch_all(MYSQLI_ASSOC);
  104. } else {
  105. $arr = false;
  106. die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
  107. }
  108. return $arr;
  109. }
  110. static function getAllUsersGroupsArray() {
  111. global $global;
  112. $sql = "SELECT * FROM users_groups as ug WHERE 1=1 ";
  113. $res = sqlDAL::readSql($sql);
  114. $fullData = sqlDAL::fetchAllAssoc($res);
  115. sqlDAL::close($res);
  116. $arr = array();
  117. if ($res!=false) {
  118. foreach ($fullData as $row) {
  119. $arr[$row['id']] = $row['group_name'];
  120. }
  121. //$category = $res->fetch_all(MYSQLI_ASSOC);
  122. } else {
  123. $arr = false;
  124. die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
  125. }
  126. return $arr;
  127. }
  128. static function getTotalUsersGroups() {
  129. global $global;
  130. $sql = "SELECT id FROM users_groups WHERE 1=1 ";
  131. $sql .= BootGrid::getSqlSearchFromPost(array('group_name'));
  132. $res = sqlDAL::readSql($sql);
  133. $numRows = sqlDAL::num_rows($res);
  134. sqlDAL::close($res);
  135. return $numRows;
  136. }
  137. function getGroup_name() {
  138. return $this->group_name;
  139. }
  140. function setGroup_name($group_name) {
  141. $this->group_name = $group_name;
  142. }
  143. static function getUserGroupByName($group_name, $refreshCache = false) {
  144. global $global;
  145. $sql = "SELECT * FROM users_groups WHERE group_name = ? LIMIT 1";
  146. $res = sqlDAL::readSql($sql, "s", array($group_name),$refreshCache);
  147. $data = sqlDAL::fetchAssoc($res);
  148. sqlDAL::close($res);
  149. if (!empty($data)) {
  150. $category = $data;
  151. } else {
  152. $category = false;
  153. }
  154. return $category;
  155. }
  156. static function getOrCreateUserGroups($group_name){
  157. $group_name = trim($group_name);
  158. $group_name = _substr($group_name, 0, 255);
  159. if(empty($group_name)){
  160. return false;
  161. }
  162. $group = self::getUserGroupByName($group_name, true);
  163. if(empty($group)){
  164. $g = new UserGroups(0, $group_name);
  165. return $g->save();
  166. }else{
  167. return $group['id'];
  168. }
  169. }
  170. // for users
  171. static function updateUserGroups($users_id, $array_groups_id, $byPassAdmin=false){
  172. if (!$byPassAdmin && !User::isAdmin()) {
  173. return false;
  174. }
  175. if (!is_array($array_groups_id)) {
  176. return false;
  177. }
  178. self::deleteGroupsFromUser($users_id, $byPassAdmin);
  179. global $global;
  180. $array_groups_id = array_unique($array_groups_id);
  181. $sql = "INSERT INTO users_has_users_groups ( users_id, users_groups_id) VALUES (?,?)";
  182. foreach ($array_groups_id as $value) {
  183. $value = intval($value);
  184. sqlDAL::writeSql($sql,"ii",array($users_id,$value));
  185. }
  186. return true;
  187. }
  188. static function getUserGroups($users_id) {
  189. global $global;
  190. $res = sqlDAL::readSql("SHOW TABLES LIKE 'users_has_users_groups'");
  191. $result = sqlDAL::num_rows($res);
  192. sqlDAL::close($res);
  193. if (empty($result)) {
  194. $_GET['error'] = "You need to <a href='{$global['webSiteRootURL']}update'>update your system to ver 2.3</a>";
  195. return array();
  196. }
  197. if (empty($users_id)) {
  198. return array();
  199. }
  200. $sql = "SELECT uug.*, ug.* FROM users_groups ug"
  201. . " LEFT JOIN users_has_users_groups uug ON users_groups_id = ug.id WHERE users_id = ? ";
  202. $ids = AVideoPlugin::getDynamicUserGroupsId($users_id);
  203. if(!empty($ids) && is_array($ids)){
  204. $ids = array_unique($ids);
  205. $sql .= " OR ug.id IN ('". implode("','", $ids)."') ";
  206. }
  207. //var_dump($ids);echo $sql;exit;
  208. $res = sqlDAL::readSql($sql,"i",array($users_id));
  209. $fullData = sqlDal::fetchAllAssoc($res);
  210. sqlDAL::close($res);
  211. $arr = array();
  212. $doNotRepeat = array();
  213. if ($res!=false) {
  214. foreach ($fullData as $row) {
  215. if(in_array($row['id'], $doNotRepeat)){
  216. continue;
  217. }
  218. $doNotRepeat[] = $row['id'];
  219. $arr[] = $row;
  220. }
  221. } else {
  222. $arr = false;
  223. die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
  224. }
  225. return $arr;
  226. }
  227. static private function deleteGroupsFromUser($users_id, $byPassAdmin=false){
  228. if (!$byPassAdmin && !User::isAdmin()) {
  229. return false;
  230. }
  231. global $global;
  232. if (!empty($users_id)) {
  233. $sql = "DELETE FROM users_has_users_groups WHERE users_id = ?";
  234. } else {
  235. return false;
  236. }
  237. return sqlDAL::writeSql($sql,"i",array($users_id));
  238. }
  239. static function getVideoGroupsViewId($videos_id, $users_groups_id) {
  240. if(empty($videos_id)){
  241. return false;
  242. }
  243. if(empty($users_groups_id)){
  244. return false;
  245. }
  246. global $global;
  247. $sql = "SELECT id FROM videos_group_view WHERE videos_id = ? AND users_groups_id = ? LIMIT 1 ";
  248. $res = sqlDAL::readSql($sql,"ii",array($videos_id, $users_groups_id));
  249. $data = sqlDAL::fetchAssoc($res);
  250. sqlDAL::close($res);
  251. if (!empty($data)) {
  252. return $data['id'];
  253. } else {
  254. return 0;
  255. }
  256. }
  257. static function addVideoGroups($videos_id, $users_groups_id) {
  258. if (!User::canUpload()) {
  259. return false;
  260. }
  261. global $global;
  262. if(self::getVideoGroupsViewId($videos_id, $users_groups_id)){
  263. return false;
  264. }
  265. $sql = "INSERT INTO videos_group_view ( videos_id, users_groups_id) VALUES (?,?)";
  266. $value = intval($value);
  267. sqlDAL::writeSql($sql,"ii",array($videos_id,$users_groups_id));
  268. return true;
  269. }
  270. static function deleteVideoGroups($videos_id, $users_groups_id) {
  271. if (!User::canUpload()) {
  272. return false;
  273. }
  274. $sql = "DELETE FROM videos_group_view WHERE videos_id = ? AND users_groups_id = ?";
  275. return sqlDAL::writeSql($sql,"ii",array($videos_id, $users_groups_id));
  276. }
  277. static function updateVideoGroups($videos_id, $array_groups_id) {
  278. if (!User::canUpload()) {
  279. return false;
  280. }
  281. if (!is_array($array_groups_id)) {
  282. return false;
  283. }
  284. self::deleteGroupsFromVideo($videos_id);
  285. global $global;
  286. $sql = "INSERT INTO videos_group_view ( videos_id, users_groups_id) VALUES (?,?)";
  287. foreach ($array_groups_id as $value) {
  288. $value = intval($value);
  289. sqlDAL::writeSql($sql,"ii",array($videos_id,$value));
  290. }
  291. return true;
  292. }
  293. static function getVideoGroups($videos_id) {
  294. if(empty($videos_id)){
  295. return array();
  296. }
  297. global $global;
  298. //check if table exists if not you need to update
  299. $sql = "SELECT 1 FROM `videos_group_view` LIMIT 1";
  300. $res = sqlDAL::readSql($sql);
  301. sqlDAL::close($res);
  302. if (!$res) {
  303. if (User::isAdmin()) {
  304. $_GET['error'] = "You need to Update AVideo to version 2.3 <a href='{$global['webSiteRootURL']}update/'>Click here</a>";
  305. }
  306. return array();
  307. }
  308. $sql = "SELECT v.*, ug.*FROM videos_group_view as v "
  309. . " LEFT JOIN users_groups as ug ON users_groups_id = ug.id WHERE videos_id = ? ";
  310. $res = sqlDAL::readSql($sql,"i",array($videos_id));
  311. $fullData = sqlDAL::fetchAllAssoc($res);
  312. sqlDAL::close($res);
  313. $arr = array();
  314. if ($res!=false) {
  315. foreach ($fullData as $row) {
  316. $arr[] = $row;
  317. }
  318. } else {
  319. $arr = false;
  320. die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
  321. }
  322. return $arr;
  323. }
  324. static private function deleteGroupsFromVideo($videos_id){
  325. if (!User::canUpload()) {
  326. return false;
  327. }
  328. global $global;
  329. if (!empty($videos_id)) {
  330. $sql = "DELETE FROM videos_group_view WHERE videos_id = ?";
  331. } else {
  332. return false;
  333. }
  334. return sqlDAL::writeSql($sql,"i",array($videos_id));
  335. }
  336. }