1: <?php
2: /**
3: * Extended User Profile
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package profile
15: * @since 2.3.0
16: * @author Jan Pedersen
17: * @author Taiwen Jiang <phppp@users.sourceforge.net>
18: */
19:
20: // defined('XOOPS_ROOT_PATH') || exit("XOOPS root path not defined");
21:
22: /**
23: * Class ProfileVisibility
24: */
25: class ProfileVisibility extends XoopsObject
26: {
27: public $field_id;
28: public $user_group;
29: public $profile_group;
30:
31: /**
32: *
33: */
34: public function __construct()
35: {
36: $this->initVar('field_id', XOBJ_DTYPE_INT);
37: $this->initVar('user_group', XOBJ_DTYPE_INT);
38: $this->initVar('profile_group', XOBJ_DTYPE_INT);
39: }
40: }
41:
42: /**
43: * Class ProfileVisibilityHandler
44: */
45: class ProfileVisibilityHandler extends XoopsPersistableObjectHandler
46: {
47: /**
48: * @param null|XoopsDatabase $db
49: */
50: public function __construct(XoopsDatabase $db)
51: {
52: parent::__construct($db, 'profile_visibility', 'profilevisibility', 'field_id');
53: }
54:
55: /**
56: * Get fields visible to the $user_groups on a $profile_groups profile
57: *
58: * @param array $profile_groups groups of the user to be accessed
59: * @param array $user_groups groups of the visitor, default as $GLOBALS['xoopsUser']
60: *
61: * @return array
62: */
63: public function getVisibleFields($profile_groups, $user_groups = null)
64: {
65: $profile_groups[] = $user_groups[] = 0;
66: $sql = "SELECT field_id FROM {$this->table} WHERE profile_group IN (" . implode(',', $profile_groups) . ')';
67: $sql .= ' AND user_group IN (' . implode(',', $user_groups) . ')';
68: $field_ids = array();
69: $result = $this->db->query($sql);
70: if ($this->db->isResultSet($result)) {
71: while (false !== (list($field_id) = $this->db->fetchRow($result))) {
72: $field_ids[] = $field_id;
73: }
74: }
75:
76: return $field_ids;
77: }
78:
79: /**
80: * get all rows matching a condition
81: *
82: * @param CriteriaElement $criteria {@link CriteriaElement} to match
83: *
84: * @return array of row arrays, indexed by field_id
85: */
86: public function getAllByFieldId(CriteriaElement $criteria = null)
87: {
88: $rawRows = parent::getAll($criteria, null, false, false);
89:
90: usort($rawRows, array($this, 'visibilitySort'));
91:
92: $rows = array();
93: foreach ($rawRows as $rawRow) {
94: $rows[$rawRow['field_id']][] = $rawRow;
95: }
96:
97: return $rows;
98: }
99:
100: /**
101: * compare two arrays, each a row from profile_visibility
102: * The comparison is on three columns, 'field_id', 'user_group', 'profile_group' considered in that
103: * order for comparison
104: *
105: * @param array $a associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
106: * @param array $b associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
107: *
108: * @return int integer less that zero if $a is less than $b
109: * integer zero if $a and $b are equal
110: * integer greater than zero if $a is greater than $b
111: */
112: protected function visibilitySort($a, $b)
113: {
114: $fieldDiff = $a['field_id'] - $b['field_id'];
115: $userDiff = $a['user_group'] - $b['user_group'];
116: $profDiff = $a['profile_group'] - $b['profile_group'];
117: if (0 != $fieldDiff) {
118: return $fieldDiff;
119: } elseif (0 !== $userDiff) {
120: return $userDiff;
121: } else {
122: return $profDiff;
123: }
124: }
125: }
126: