PluginProbe
User Access Manager / 1.0
User Access Manager v1.0
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / class / UamUserGroup.class.php

UamUserGroup.class.php in User Access Manager 1.0, at class/UamUserGroup.class.php

1,458 lines 36.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UamUserGroup.class.php
4 *
5 * The UamUserGroup class file.
6 *
7 * PHP versions 5
8 *
9 * @category UserAccessManager
10 * @package UserAccessManager
11 * @author Alexander Schneider <alexanderschneider85@googlemail.com>
12 * @copyright 2008-2010 Alexander Schneider
13 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
14 * @version SVN: $Id$
15 * @link http://wordpress.org/extend/plugins/user-access-manager/
16 */
17
18 /**
19 * The user group class.
20 *
21 * @category UserAccessManager
22 * @package UserAccessManager
23 * @author Alexander Schneider <alexanderschneider85@gmail.com>
24 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
25 * @link http://wordpress.org/extend/plugins/user-access-manager/
26 */
27
28 class UamUserGroup
29 {
30 protected $accessHandler = null;
31 protected $id = null;
32 protected $groupName = null;
33 protected $groupDesc = null;
34 protected $readAccess = null;
35 protected $writeAccess = null;
36 protected $ipRange = null;
37 protected $roles = array();
38 protected $users = array(
39 'real' => -1,
40 'full' => -1
41 );
42 protected $singleUsers = array();
43 protected $categories = array(
44 'real' => -1,
45 'full' => -1
46 );
47 protected $singleCategories = array();
48 protected $posts = array(
49 'real' => -1,
50 'full' => -1
51 );
52 protected $singlePosts = array();
53 protected $pages = array(
54 'real' => -1,
55 'full' => -1
56 );
57 protected $files = array(
58 'real' => -1,
59 'full' => -1
60 );
61 private $_assignedUsers = null;
62 private $_assignedCategories = null;
63 private $_assignedPosts = null;
64
65 /**
66 * Consturtor
67 *
68 * @param object &$uamAccessHandler The access handler object.
69 * @param integer $id The id of the user group.
70 *
71 * @return null
72 */
73 function __construct(&$uamAccessHandler, $id = null)
74 {
75 $this->accessHandler = $uamAccessHandler;
76
77 if ($id !== null) {
78 global $wpdb;
79
80 $this->id = $id;
81
82 $dbUsergroup = $wpdb->get_row(
83 "SELECT *
84 FROM " . DB_ACCESSGROUP . "
85 WHERE ID = " . $this->id . "
86 LIMIT 1",
87 ARRAY_A
88 );
89
90 $this->groupName = $dbUsergroup['groupname'];
91 $this->groupDesc = $dbUsergroup['groupdesc'];
92 $this->readAccess = $dbUsergroup['read_access'];
93 $this->writeAccess = $dbUsergroup['write_access'];
94 $this->ipRange = $dbUsergroup['ip_range'];
95 }
96 }
97
98 /**
99 * Returns the user access handler object.
100 *
101 * @return object
102 */
103 function &getAccessHandler()
104 {
105 return $this->accessHandler;
106 }
107
108 /**
109 * Deletes the user group.
110 *
111 * @return null
112 */
113 function delete()
114 {
115 if ($this->id == null) {
116 return false;
117 }
118
119 global $wpdb;
120
121 $wpdb->query(
122 "DELETE FROM " . DB_ACCESSGROUP . "
123 WHERE ID = $this->id LIMIT 1"
124 );
125
126 $this->_deleteRolesFromDb();
127 $this->_deletePostByTypeFromDb('all');
128 $this->_deleteCategoriesFromDb();
129 $this->_deleteUsersFromDb();
130 }
131
132 /**
133 * Saves the user group.
134 *
135 * @return null;
136 */
137 function save()
138 {
139 global $wpdb;
140
141 if ($this->id == null) {
142 $wpdb->query(
143 "INSERT INTO " . DB_ACCESSGROUP . " (
144 ID,
145 groupname,
146 groupdesc,
147 read_access,
148 write_access,
149 ip_range
150 )
151 VALUES (
152 NULL,
153 '" . $this->groupName . "',
154 '" . $this->groupDesc . "',
155 '" . $this->readAccess . "',
156 '" . $this->writeAccess . "',
157 '" . $this->ipRange . "'
158 )"
159 );
160
161 $this->id = $wpdb->insert_id;
162 } else {
163 $wpdb->query(
164 "UPDATE " . DB_ACCESSGROUP . "
165 SET groupname = '" . $this->groupName . "',
166 groupdesc = '" . $this->groupDesc . "',
167 read_access = '" . $this->readAccess . "',
168 write_access = '" . $this->writeAccess . "',
169 ip_range = '" . $this->ipRange . "'
170 WHERE ID = " . $this->id
171 );
172
173 $this->getPosts();
174 $this->getPages();
175 $this->getFiles();
176 $this->getRoles();
177 $this->getCategories();
178 $this->getUsers();
179
180 $this->_deletePostByTypeFromDb('all');
181 $this->_deleteRolesFromDb();
182 $this->_deleteCategoriesFromDb();
183 $this->_deleteUsersFromDb();
184 }
185
186 foreach ($this->getUsers() as $userKey => $user) {
187 $wpdb->query(
188 "INSERT INTO " . DB_ACCESSGROUP_TO_USER . " (
189 group_id,
190 user_id
191 )
192 VALUES(
193 '" . $this->id . "',
194 '" . $userKey . "'
195 )"
196 );
197 }
198
199 foreach ($this->getCategories() as $categoryKey => $category) {
200 $wpdb->query(
201 "INSERT INTO " . DB_ACCESSGROUP_TO_CATEGORY . " (
202 group_id,
203 category_id
204 )
205 VALUES(
206 '" . $this->id . "',
207 '" . $categoryKey . "'
208 )"
209 );
210 }
211
212 foreach ($this->getRoles() as $roleKey => $role) {
213 $wpdb->query(
214 "INSERT INTO " . DB_ACCESSGROUP_TO_ROLE . " (
215 group_id,
216 role_name
217 )
218 VALUES(
219 '" . $this->id . "',
220 '" . $roleKey . "'
221 )"
222 );
223 }
224
225 $allPosts
226 = array_merge($this->getPosts(), $this->getPages(), $this->getFiles());
227
228 foreach ($allPosts as $post) {
229 $wpdb->query(
230 "INSERT INTO " . DB_ACCESSGROUP_TO_POST . " (
231 group_id,
232 post_id
233 )
234 VALUES(
235 '" . $this->id . "',
236 '" . $post->ID . "'
237 )"
238 );
239 }
240 }
241
242
243 /*
244 * Primary values.
245 */
246
247 /**
248 * Returns the group id.
249 *
250 * @return integer
251 */
252 function getId()
253 {
254 return $this->id;
255 }
256
257 /**
258 * Returns the group name.
259 *
260 * @return string
261 */
262 function getGroupName()
263 {
264 return $this->groupName;
265 }
266
267 /**
268 * Sets the group name.
269 *
270 * @param string $groupName The new group name.
271 *
272 * @return null
273 */
274 function setGroupName($groupName)
275 {
276 $this->groupName = $groupName;
277 }
278
279 /**
280 * Returns the group description.
281 *
282 * @return string
283 */
284 function getGroupDesc()
285 {
286 return $this->groupDesc;
287 }
288
289 /**
290 * Sets the group description.
291 *
292 * @param string $groupDesc The new group description.
293 *
294 * @return null
295 */
296 function setGroupDesc($groupDesc)
297 {
298 $this->groupDesc = $groupDesc;
299 }
300
301 /**
302 * Returns the read access.
303 *
304 * @return string
305 */
306 function getReadAccess()
307 {
308 return $this->readAccess;
309 }
310
311 /**
312 * Sets the read access.
313 *
314 * @param string $readAccess The read access.
315 *
316 * @return null
317 */
318 function setReadAccess($readAccess)
319 {
320 $this->readAccess = $readAccess;
321 }
322
323 /**
324 * Returns the write access.
325 *
326 * @return string
327 */
328 function getWriteAccess()
329 {
330 return $this->writeAccess;
331 }
332
333 /**
334 * Sets the write access.
335 *
336 * @param string $writeAccess The wirte access.
337 *
338 * @return null
339 */
340 function setWriteAccess($writeAccess)
341 {
342 $this->writeAccess = $writeAccess;
343 }
344
345 /**
346 * Returns the ip range.
347 *
348 * @param string $type The return type.
349 *
350 * @return array
351 */
352 function getIpRange($type = null)
353 {
354 if ($type == 'string') {
355 return $this->ipRange;
356 }
357
358 $ipRange = explode(';', $this->ipRange);
359
360 if ($ipRange[0] == null) {
361 return null;
362 }
363
364 return $ipRange;
365 }
366
367 /**
368 * Sets the ip range.
369 *
370 * @param string $ipRange The new ip range.
371 *
372 * @return null
373 */
374 function setIpRange($ipRange)
375 {
376 if (is_array($ipRange)) {
377 $ipRange = implode(';', $ipRange);
378 }
379
380 $this->ipRange = $ipRange;
381 }
382
383
384 /*
385 * Group roles functions.
386 */
387
388 /**
389 * Returns the roles in the group
390 *
391 * @return array
392 */
393 function getRoles()
394 {
395 if ($this->id == null) {
396 return array();
397 }
398
399 if ($this->roles != array()) {
400 return $this->roles;
401 }
402
403 global $wpdb;
404
405 $dbRoles = $wpdb->get_results(
406 "SELECT *
407 FROM " . DB_ACCESSGROUP_TO_ROLE . "
408 WHERE group_id = " . $this->id,
409 ARRAY_A
410 );
411
412 if (isset($dbRoles)) {
413 foreach ($dbRoles as $dbRole) {
414 $this->roles[trim($dbRole['role_name'])] = $dbRole;
415 }
416 }
417
418 return $this->roles;
419 }
420
421 /**
422 * Adds a role to the role group.
423 *
424 * @param integer $roleName The role name which should be added.
425 *
426 * @return null
427 */
428 function addRole($roleName)
429 {
430 $this->getRoles();
431
432 /**
433 * $this->roles[$roleName] = &get_role($roleName);
434 * Makes trouble, but why?
435 * Error: "Notice: Only variable references should be returned by reference"
436 */
437
438 $this->roles[$roleName] = array('role_name' => $roleName);
439 }
440
441 /**
442 * Removes a role from the role group.
443 *
444 * @param integer $roleName The role name which should be removed.
445 *
446 * @return null
447 */
448 function removeRole($roleName)
449 {
450 $this->getRoles();
451 unset($this->roles[$roleName]);
452 }
453
454 /**
455 * Unsets the roles.
456 *
457 * @param boolean $plusRemove If true also database entrys will remove.
458 *
459 * @return null;
460 */
461 function unsetRoles($plusRemove = false)
462 {
463 if ($plusRemove) {
464 $this->_deleteRolesFromDb();
465 }
466
467 $this->roles = array();
468 }
469
470 /**
471 * Removes all roles from the user group.
472 *
473 * @return null
474 */
475 private function _deleteRolesFromDb()
476 {
477 if ($this->id == null) {
478 return false;
479 }
480
481 global $wpdb;
482
483 $wpdb->query(
484 "DELETE FROM " . DB_ACCESSGROUP_TO_ROLE . "
485 WHERE group_id = ".$this->id
486 );
487 }
488
489
490 /*
491 * Group users functions.
492 */
493
494 /**
495 * Returns the assigned users.
496 *
497 * @return array
498 */
499 private function _getAssignedUsers()
500 {
501 if ($this->_assignedUsers !== null) {
502 return $this->_assignedUsers;
503 }
504
505 global $wpdb;
506
507 $dbUsers = $wpdb->get_results(
508 "SELECT user_id
509 FROM " . DB_ACCESSGROUP_TO_USER . "
510 WHERE group_id = " . $this->id . "
511 ORDER BY user_id"
512 );
513
514 $this->_assignedUsers = array();
515
516 foreach ($dbUsers as $dbUser) {
517 $this->_assignedUsers[$dbUser->user_id] = $dbUser->user_id;
518 }
519
520 return $this->_assignedUsers;
521 }
522
523 /**
524 * Checks it the user is assigned to the group.
525 *
526 * @param integer $userId The user id.
527 *
528 * @return boolean
529 */
530 private function _isUserAssignedToGroup($userId)
531 {
532 if (array_key_exists($userId, $this->_getAssignedUsers())) {
533 return true;
534 } else {
535 return false;
536 }
537 }
538
539 /**
540 * Returns a single user.
541 *
542 * @param object $user The user.
543 * @param string $type The return type. Can be real or full.
544 *
545 * @return object
546 */
547 private function _getSingleUser($user, $type)
548 {
549 if (!isset($user->ID)) {
550 return null;
551 }
552
553 if (isset($this->singleUsers[$user->ID])) {
554 return $this->singleUsers[$user->ID];
555 }
556
557 $isRecursiveMember = array();
558
559 if ($type == 'full') {
560 global $wpdb;
561
562 $curUserdata = get_userdata($user->ID);
563
564 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
565 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
566 } else {
567 $capabilities = null;
568 }
569
570 $role = is_array($capabilities) ?
571 array_keys($capabilities) : 'norole';
572
573 if (array_key_exists($role[0], $this->getRoles())
574 ) {
575 $isRecursiveMember
576 = array('byRole' => array());
577 $isRecursiveMember['byRole'][]
578 = $role[0];
579 }
580 }
581
582 if ($this->_isUserAssignedToGroup($user->ID)
583 || $isRecursiveMember != array()
584 ) {
585 if ($isRecursiveMember != array()) {
586 $user->recursiveMember = $isRecursiveMember;
587 }
588
589 $this->singleUsers[$user->ID] = $user;
590 } else {
591 $this->singleUsers[$user->ID] = null;
592 }
593
594 return $this->singleUsers[$user->ID];
595 }
596
597 /**
598 * Returns the users in the group
599 *
600 * @param string $type The return type. Can be real or full.
601 *
602 * @return array
603 */
604 function getUsers($type = 'real')
605 {
606 if ($this->id == null) {
607 return array();
608 }
609
610 if ($type != 'real'
611 && $type != 'full'
612 ) {
613 return array();
614 }
615
616 if ($this->users[$type] != -1) {
617 return $this->users[$type];
618 } else {
619 $this->users[$type] = array();
620 }
621
622 global $wpdb;
623
624 if ($type == 'real') {
625 $dbUsers = $wpdb->get_results(
626 "SELECT user_id as ID
627 FROM " . DB_ACCESSGROUP_TO_USER . "
628 WHERE group_id = " . $this->id . "
629 ORDER BY user_id",
630 ARRAY_A
631 );
632 } elseif ($type == 'full') {
633 $dbUsers = $wpdb->get_results(
634 "SELECT ID, user_nicename
635 FROM $wpdb->users
636 ORDER BY user_nicename"
637 );
638 }
639
640 if (isset($dbUsers)) {
641 foreach ($dbUsers as $dbUser) {
642 $user = $this->_getSingleUser($dbUser, $type);
643
644 if ($user !== null) {
645 $this->users[$type][$user->ID] = $user;
646 }
647 }
648 }
649
650 return $this->users[$type];
651 }
652
653 /**
654 * Adds a user to the user group.
655 *
656 * @param integer $userID The user id which should be added.
657 *
658 * @return null
659 */
660 function addUser($userID)
661 {
662 $this->getUsers();
663 $this->users['real'][$userID] = get_userdata($userID);
664 $this->users['full'] = -1;
665 }
666
667 /**
668 * Removes a user from the user group.
669 *
670 * @param integer $userID The user id which should be removed.
671 *
672 * @return null
673 */
674 function removeUser($userID)
675 {
676 $this->getUsers();
677 unset($this->users['real'][$userID]);
678 $this->users['full'] = -1;
679 }
680
681 /**
682 * Unsets the users.
683 *
684 * @param boolean $plusRemove If true also database entrys will remove.
685 *
686 * @return null;
687 */
688 function unsetUsers($plusRemove = false)
689 {
690 if ($plusRemove) {
691 $this->_deleteUsersFromDb();
692 }
693
694 $this->users = array(
695 'real' => array(),
696 'full' => array(),
697 );
698 }
699
700 /**
701 * Removes all users from the user group.
702 *
703 * @return null
704 */
705 private function _deleteUsersFromDb()
706 {
707 global $wpdb;
708
709 $wpdb->query(
710 "DELETE FROM " . DB_ACCESSGROUP_TO_USER . "
711 WHERE group_id = ".$this->id
712 );
713 }
714
715 /**
716 * Checks if the given user is a member of the group.
717 *
718 * @param interger $userId The id of the user which should be checked.
719 * @param boolean $withInfo If true then we return additional infos.
720 *
721 * @return boolean
722 */
723 function userIsMember($userId, $withInfo = false)
724 {
725 $user->ID = $userId;
726 $user = $this->_getSingleUser($user, 'full');
727
728 if ($user !== null) {
729 if (isset($user->recursiveMember)
730 && $withInfo
731 ) {
732 return $user->recursiveMember;
733 }
734
735 return true;
736 }
737
738 return false;
739 }
740
741
742 /*
743 * Group categories functions.
744 */
745
746 /**
747 * Returns the assigned categories.
748 *
749 * @return array
750 */
751 private function _getAssignedCategories()
752 {
753 if ($this->_assignedCategories !== null) {
754 return $this->_assignedCategories;
755 }
756
757 global $wpdb;
758
759 $dbCategories = $wpdb->get_results(
760 "SELECT *
761 FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
762 WHERE group_id = " . $this->id . "
763 ORDER BY category_id"
764 );
765
766 $this->_assignedCategories = array();
767
768 foreach ($dbCategories as $dbCategory) {
769 $this->_assignedCategories[$dbCategory->category_id]
770 = $dbCategory->category_id;
771 }
772
773 return $this->_assignedCategories;
774 }
775
776 /**
777 * Checks it the category is assigned to the group.
778 *
779 * @param integer $categoryId The category id.
780 *
781 * @return boolean
782 */
783 private function _isCategoryAssignedToGroup($categoryId)
784 {
785 if (array_key_exists($categoryId, $this->_getAssignedCategories())) {
786 return true;
787 } else {
788 return false;
789 }
790 }
791
792 /**
793 * Returns a single category.
794 *
795 * @param object $category The category.
796 * @param string $type The return type. Can be real or full.
797 *
798 * @return object
799 */
800 private function _getSingleCategory($category, $type)
801 {
802 if (!isset($category->term_id)) {
803 return null;
804 }
805
806 if (isset($this->singleCategories[$category->term_id])) {
807 return $this->singleCategories[$category->term_id];
808 }
809
810 $isRecursiveMember = array();
811
812 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
813 $uamOptions = $userAccessManager->getAdminOptions();
814
815 if ($uamOptions['lock_recursive'] == 'true'
816 && $type == 'full'
817 ) {
818 if ($category->parent != 0) {
819 $parentCategory = get_post($category->parent);
820
821 $parentCategory = $this->_getSingleCategory(
822 $parentCategory,
823 $type
824 );
825
826 if ($parentCategory !== null) {
827 if (isset($parentCategory->recursiveMember)) {
828 $isRecursiveMember['byCategory'][]
829 = $parentCategory;
830 } else {
831 $isRecursiveMember['byCategory'][]
832 = $parentCategory->term_id;
833 }
834 }
835 }
836 }
837
838 if ($this->_isCategoryAssignedToGroup($category->term_id)
839 || $isRecursiveMember != array()
840 ) {
841 if ($isRecursiveMember != array()) {
842 $category->recursiveMember = $isRecursiveMember;
843 }
844
845 $this->singleCategories[$category->term_id] = $category;
846 } else {
847 $this->singleCategories[$category->term_id] = null;
848 }
849
850 return $this->singleCategories[$category->term_id];
851 }
852
853 /**
854 * Returns the categories in the group
855 *
856 * @param string $type The return type. Can be real or full.
857 *
858 * @return array
859 */
860 function getCategories($type = 'real')
861 {
862 if ($this->id == null) {
863 return array();
864 }
865
866 if ($type != 'real'
867 && $type != 'full'
868 ) {
869 return array();
870 }
871
872 if ($this->categories[$type] != -1) {
873 return $this->categories[$type];
874 } else {
875 $this->categories[$type] = array();
876 }
877
878 global $wpdb;
879 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
880 $uamOptions = $userAccessManager->getAdminOptions();
881
882 $dbCategories = $wpdb->get_results(
883 "SELECT *
884 FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
885 WHERE group_id = " . $this->id . "
886 ORDER BY category_id",
887 ARRAY_A
888 );
889
890 if (isset($dbCategories)) {
891 foreach ($dbCategories as $dbCategorie) {
892 $category = get_category($dbCategorie['category_id']);
893
894 if ($category != null) {
895 if ($uamOptions['lock_recursive'] == 'true'
896 && $type == 'full'
897 ) {
898 //We have to remove the filter to get all categories
899 $removeSucc = remove_filter(
900 'get_terms',
901 array(
902 $this->getAccessHandler()->getUserAccessManager(),
903 'showCategory'
904 )
905 );
906
907 if ($removeSucc) {
908 $args = array(
909 'child_of' => $category->term_id,
910 'hide_empty' => false
911 );
912
913 $categoryChilds = get_categories($args);
914
915 add_filter(
916 'get_terms',
917 array(&$userAccessManager, 'showCategory')
918 );
919
920 foreach ($categoryChilds as $categoryChild) {
921 $categoryChild->recursiveMember
922 = array('byCategory' => array());
923 $categoryChild->recursiveMember['byCategory'][]
924 = $category->term_id;
925 $this->categories[$type][$categoryChild->term_id]
926 = $categoryChild;
927 }
928 }
929 }
930
931 $this->categories[$type][$category->term_id] = $category;
932 }
933 }
934 }
935
936 return $this->categories[$type];
937 }
938
939 /**
940 * Adds a category to the category group.
941 *
942 * @param integer $categoryID The category id which should be added.
943 *
944 * @return null
945 */
946 function addCategory($categoryID)
947 {
948 $this->getCategories();
949 $this->categories['real'][$categoryID] = get_category($categoryID);
950 $this->categories['full'] = -1;
951 }
952
953 /**
954 * Removes a category from the category group.
955 *
956 * @param integer $categoryID The category id which should be removed.
957 *
958 * @return null
959 */
960 function removeCategory($categoryID)
961 {
962 $this->getCategories();
963 unset($this->categories['real'][$categoryID]);
964 $this->categories['full'] = -1;
965 }
966
967 /**
968 * Unsets the categories.
969 *
970 * @param boolean $plusRemove If true also database entrys will remove.
971 *
972 * @return null;
973 */
974 function unsetCategories($plusRemove = false)
975 {
976 if ($plusRemove) {
977 $this->_deleteCategoriesFromDb();
978 }
979
980 $this->categories = array(
981 'real' => array(),
982 'full' => array(),
983 );
984 }
985
986 /**
987 * Removes all categories from the user group.
988 *
989 * @return null
990 */
991 private function _deleteCategoriesFromDb()
992 {
993 global $wpdb;
994
995 $wpdb->query(
996 "DELETE FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
997 WHERE group_id = ".$this->id
998 );
999 }
1000
1001 /**
1002 * Checks if the given post is a member of the group.
1003 *
1004 * @param interger $categoryId The id of the post which should be checked.
1005 * @param boolean $withInfo If true then we return additional infos.
1006 *
1007 * @return boolean
1008 */
1009 function categoryIsMember($categoryId, $withInfo = false)
1010 {
1011 $category = get_category($categoryId);
1012 $category = $this->_getSingleCategory($category, 'full');
1013
1014 if ($category !== null) {
1015 if (isset($category->recursiveMember)
1016 && $withInfo
1017 ) {
1018 return $category->recursiveMember;
1019 }
1020
1021 return true;
1022 }
1023
1024 /*$categories = $this->getCategories('full');
1025
1026 if (array_key_exists($categoryId, $categories)) {
1027 if (isset($categories[$categoryId]->recursiveMember)
1028 && $withInfo
1029 ) {
1030 return $categories[$categoryId]->recursiveMember;
1031 }
1032
1033 return true;
1034 }*/
1035
1036 return false;
1037 }
1038
1039
1040 /*
1041 * Group posts functions.
1042 */
1043
1044 /**
1045 * Returns the assigned posts
1046 *
1047 * @return array
1048 */
1049 private function _getAssignedPosts()
1050 {
1051 if ($this->_assignedPosts !== null) {
1052 return $this->_assignedPosts;
1053 }
1054
1055 global $wpdb;
1056
1057 $dbPosts = $wpdb->get_results(
1058 "SELECT post_id
1059 FROM " . DB_ACCESSGROUP_TO_POST . "
1060 WHERE group_id = " . $this->id
1061 );
1062
1063 $this->_assignedPosts = array();
1064
1065 foreach ($dbPosts as $dbPost) {
1066 $this->_assignedPosts[$dbPost->post_id] = $dbPost->post_id;
1067 }
1068
1069 return $this->_assignedPosts;
1070 }
1071
1072 /**
1073 * Checks it the post is assigned to the group.
1074 *
1075 * @param integer $postId The post id.
1076 *
1077 * @return boolean
1078 */
1079 private function _isPostAssignedToGroup($postId)
1080 {
1081 if (array_key_exists($postId, $this->_getAssignedPosts())) {
1082 return true;
1083 } else {
1084 return false;
1085 }
1086 }
1087
1088 /**
1089 * Returns the membership of a single post.
1090 *
1091 * @param object $post The post object.
1092 * @param string $type The return type.
1093 * @param string $postType The post type needed for the intern representation.
1094 *
1095 * @return object
1096 */
1097 function _getSinglePost($post, $type, $postType)
1098 {
1099 if (!isset($post->ID)) {
1100 return null;
1101 }
1102
1103 if (isset($this->singlePosts[$post->ID])) {
1104 return $this->singlePosts[$post->ID];
1105 }
1106
1107 $isRecursiveMember = array();
1108
1109 $userAccessManager = &$this->getAccessHandler()->getUserAccessManager();
1110 $uamOptions = $userAccessManager->getAdminOptions();
1111
1112 if ($type == 'full') {
1113 foreach ($this->getCategories('full') as $category) {
1114 if (in_category($category->cat_ID, $post->ID)) {
1115 $isRecursiveMember['byCategory'][] = $category->cat_ID;
1116 //break;
1117 }
1118 }
1119
1120 if (($postType == 'page'
1121 || $postType == 'file')
1122 && $uamOptions['lock_recursive'] == 'true'
1123 ) {
1124 if ($post->post_parent != 0) {
1125 $parentPost = get_post($post->post_parent);
1126
1127 $parentPost = $this->_getSinglePost(
1128 $parentPost,
1129 $type,
1130 $parentPost->post_type
1131 );
1132
1133 if ($parentPost !== null) {
1134 if (isset($parentPost->recursiveMember)) {
1135 $isRecursiveMember['byPost'][]
1136 = $parentPost;
1137 } else {
1138 $isRecursiveMember['byPost'][]
1139 = $parentPost->ID;
1140 }
1141 }
1142 }
1143 }
1144 }
1145
1146 if ($this->_isPostAssignedToGroup($post->ID)
1147 || $isRecursiveMember != array()
1148 ) {
1149 if ($isRecursiveMember != array()) {
1150 $post->recursiveMember = $isRecursiveMember;
1151 }
1152 $this->singlePosts[$post->ID] = $post;
1153 } else {
1154 $this->singlePosts[$post->ID] = null;
1155 }
1156
1157 return $this->singlePosts[$post->ID];
1158 }
1159
1160 /**
1161 * Returns the posts by the given type in the group
1162 *
1163 * @param string $postType The type of the post.
1164 * @param string $type The return type. Can be real or full.
1165 *
1166 * @return array
1167 */
1168 private function _getPostByType($postType, $type)
1169 {
1170 if ($type != 'real'
1171 && $type != 'full'
1172 ) {
1173 return array();
1174 }
1175
1176 if ($postType == 'file') {
1177 $wpType = 'attachment';
1178 } else {
1179 $wpType = $postType;
1180 }
1181
1182 if ($this->{$postType.'s'}[$type] != -1) {
1183 return $this->{$postType.'s'}[$type];
1184 } else {
1185 $this->{$postType.'s'}[$type] = array();
1186 }
1187
1188 global $wpdb;
1189
1190 $posts = $wpdb->get_results(
1191 "SELECT ID, post_parent
1192 FROM $wpdb->posts
1193 WHERE post_type = '".$wpType."'"
1194 );
1195
1196 if (isset($posts)) {
1197 foreach ($posts as $post) {
1198 $post = $this->_getSinglePost($post, $type, $postType);
1199
1200 if ($post !== null) {
1201 $this->{$postType.'s'}[$type][$post->ID] = $post;
1202 }
1203 }
1204 }
1205
1206 return $this->{$postType.'s'}[$type];
1207 }
1208
1209 /**
1210 * Removes all post of all types from the user group.
1211 *
1212 * @param string $postType The type which should be deleted.
1213 *
1214 * @return null;
1215 */
1216 private function _deletePostByTypeFromDb($postType)
1217 {
1218 if ($this->id == null) {
1219 return false;
1220 }
1221
1222 global $wpdb;
1223
1224 if ($postType == 'all') {
1225 $wpdb->query(
1226 "DELETE FROM " . DB_ACCESSGROUP_TO_POST . "
1227 WHERE group_id = ".$this->id
1228 );
1229 } else {
1230 if ($type == 'post') {
1231 $curPostTypes = $this->getPosts();
1232 } elseif ($type == 'page') {
1233 $curPostTypes = $this->getPages();
1234 } elseif ($type == 'file') {
1235 $curPostTypes = $this->getFiles();
1236 }
1237
1238 foreach ($curPostTypes as $id => $post) {
1239 $wpdb->query(
1240 "DELETE FROM " . DB_ACCESSGROUP_TO_POST . "
1241 WHERE group_id = ".$this->id."
1242 AND post_id = ".$id."
1243 LIMIT 1"
1244 );
1245 }
1246 }
1247 }
1248
1249 /**
1250 * Returns the pages in the group
1251 *
1252 * @param string $type The return type. Can be real or full.
1253 *
1254 * @return array
1255 */
1256 function getPosts($type = 'real')
1257 {
1258 return $this->_getPostByType('post', $type);
1259 }
1260
1261 /**
1262 * Adds a post to the post group.
1263 *
1264 * @param integer $postID The post id which should be added.
1265 *
1266 * @return null
1267 */
1268 function addPost($postID)
1269 {
1270 $this->getPosts();
1271 $this->posts['real'][$postID] = get_post($postID);
1272 $this->posts['full'] = -1;
1273 }
1274
1275 /**
1276 * Removes a post from the post group.
1277 *
1278 * @param integer $postID The post id which should be removed.
1279 *
1280 * @return null
1281 */
1282 function removePost($postID)
1283 {
1284 $this->getPosts();
1285 unset($this->posts['real'][$postID]);
1286 $this->posts['full'] = -1;
1287 }
1288
1289 /**
1290 * Unsets the posts.
1291 *
1292 * @param boolean $plusRemove If true also database entrys will remove.
1293 *
1294 * @return null;
1295 */
1296 function unsetPosts($plusRemove = false)
1297 {
1298 if ($plusRemove) {
1299 $this->_deletePostByTypeFromDb('post');
1300 }
1301
1302 $this->posts = array(
1303 'real' => array(),
1304 'full' => array(),
1305 );;
1306 }
1307
1308 /**
1309 * Returns the pages in the group
1310 *
1311 * @param string $type The return type. Can be real or full.
1312 *
1313 * @return array
1314 */
1315 function getPages($type = 'real')
1316 {
1317 return $this->_getPostByType('page', $type);
1318 }
1319
1320 /**
1321 * Adds a page to the page group.
1322 *
1323 * @param integer $pageID The page id which should be added.
1324 *
1325 * @return null
1326 */
1327 function addPage($pageID)
1328 {
1329 $this->getPages();
1330 $this->pages['real'][$pageID] = get_post($pageID);
1331 $this->pages['full'] = -1;
1332 }
1333
1334 /**
1335 * Removes a page from the page group.
1336 *
1337 * @param integer $pageID The page id which should be removed.
1338 *
1339 * @return null
1340 */
1341 function removePage($pageID)
1342 {
1343 $this->getPages();
1344 unset($this->pages['real'][$pageID]);
1345 $this->pages['full'] = -1;
1346 }
1347
1348 /**
1349 * Unsets the pages.
1350 *
1351 * @param boolean $plusRemove If true also database entrys will remove.
1352 *
1353 * @return null;
1354 */
1355 function unsetPages($plusRemove = false)
1356 {
1357 if ($plusRemove) {
1358 $this->_deletePostByTypeFromDb('page');
1359 }
1360
1361 $this->pages = array(
1362 'real' => array(),
1363 'full' => array(),
1364 );;
1365 }
1366
1367 /**
1368 * Returns the files in the group
1369 *
1370 * @param string $type The return type. Can be real or full.
1371 *
1372 * @return array
1373 */
1374 function getFiles($type = 'real')
1375 {
1376 return $this->_getPostByType('file', $type);
1377 }
1378
1379 /**
1380 * Adds a file to the file group.
1381 *
1382 * @param integer $fileID The file id which should be added.
1383 *
1384 * @return null
1385 */
1386 function addFile($fileID)
1387 {
1388 $this->getFiles();
1389 $this->files['real'][$fileID] = get_post($fileID);
1390 $this->files['full'] = -1;
1391 }
1392
1393 /**
1394 * Removes a file from the file group.
1395 *
1396 * @param integer $fileID The file id which should be removed.
1397 *
1398 * @return null
1399 */
1400 function removeFile($fileID)
1401 {
1402 $this->getFiles();
1403 unset($this->files['real'][$fileID]);
1404 $this->files['full'] = -1;
1405 }
1406
1407 /**
1408 * Unsets the files.
1409 *
1410 * @param boolean $plusRemove If true also database entrys will remove.
1411 *
1412 * @return null;
1413 */
1414 function unsetFiles($plusRemove = false)
1415 {
1416 if ($plusRemove) {
1417 $this->_deletePostByTypeFromDb('file');
1418 }
1419
1420 $this->files = array(
1421 'real' => array(),
1422 'full' => array(),
1423 );
1424 }
1425
1426 /**
1427 * Checks if the given post is a member of the group.
1428 *
1429 * @param interger $postId The id of the post which should be checked.
1430 * @param boolean $withInfo If true then we return additional infos.
1431 *
1432 * @return boolean
1433 */
1434 function postIsMember($postId, $withInfo = false)
1435 {
1436 $post = get_post($postId);
1437
1438 if ($post->post_type == 'attachment') {
1439 $postType = 'file';
1440 } else {
1441 $postType = $post->post_type;
1442 }
1443
1444 $post = $this->_getSinglePost($post, 'full', $postType);
1445
1446 if ($post !== null) {
1447 if (isset($post->recursiveMember)
1448 && $withInfo
1449 ) {
1450 return $post->recursiveMember;
1451 }
1452
1453 return true;
1454 }
1455
1456 return false;
1457 }
1458 }