1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15: 16: 17: 18: 19:
20: class MenusDefaultDecorator extends MenusDecoratorAbstract implements MenusDecoratorInterface
21: {
22: protected $user;
23:
24: protected $owner;
25:
26: protected $user_groups;
27:
28: protected $user_uid;
29:
30: protected $get_uid;
31:
32: function start()
33: {
34: $xoops = Xoops::getInstance();
35: $member_handler = $xoops->getHandlerMember();
36:
37: if (!$xoops->isUser()) {
38: $user = $member_handler->createUser();
39: $user->setVar('uid', 0);
40: $user->setVar('uname', $GLOBALS['xoopsConfig']['anonymous']);
41: } else {
42: $user = $xoops->user;
43: }
44:
45: $ownerid = isset($_GET['uid']) ? (int)($_GET['uid']) : null;
46: $owner = $member_handler->getUser($ownerid);
47:
48: if (!is_object($owner)) {
49:
50: $owner = $member_handler->createUser();
51: $owner->setVar('uid', 0);
52: $owner->setVar('uname', $xoops->getConfig('anonymous'));
53: }
54: $this->user = $user->getValues();
55: $this->owner = $owner->getValues();
56: $this->user_groups = $xoops->getUserGroups();
57: $this->user_uid = $xoops->isUser() ? $xoops->user->getVar('uid') : 0;
58: $this->get_uid = isset($_GET['uid']) ? (int)($_GET['uid']) : 0;
59: }
60:
61: function accessFilter(&$access_filter)
62: {
63: $access_filter['is_owner']['name'] = _PL_MENUS_MENUS_ISOWNER;
64: $access_filter['is_owner']['method'] = 'isOwner';
65: $access_filter['is_not_owner']['name'] = _PL_MENUS_MENUS_ISNOTOWNER;
66: $access_filter['is_not_owner']['method'] = 'isNotOwner';
67: }
68:
69: function decorateMenu(&$menu)
70: {
71: $decorations = array('link', 'title', 'alt_title');
72: foreach ($decorations as $decoration) {
73: if ($decoration === 'alt_title' && empty($menu['alt_title'])) {
74: $menu['alt_title'] = $menu['title'];
75: }
76: $menu[$decoration] = self::_doDecoration($menu[$decoration]);
77: if ($decoration === 'link') {
78: if (!preg_match('/mailto:/i', $menu['link']) && !preg_match('#://#i', $menu['link'])) {
79: $menu['link'] = \XoopsBaseConfig::get('url') . '/' . $menu['link'];
80: }
81: }
82: }
83: }
84:
85: function end(&$menus)
86: {
87:
88: }
89:
90: function hasAccess($menu, &$hasAccess)
91: {
92: $groups = $this->user_groups;
93: if ($menu['visible'] == 0 || !array_intersect($menu['groups'], $groups)) {
94: $hasAccess = false;
95: return;
96: }
97:
98: $hooks = array_intersect($menu['hooks'], get_class_methods(__CLASS__));
99:
100: foreach ($hooks as $method) {
101: if (!self::$method()) {
102: $hasAccess = false;
103: return;
104: }
105: }
106: }
107:
108: function _doDecoration($string)
109: {
110: if (!preg_match('/{(.*\|.*)}/i', $string, $reg)) {
111: return $string;
112: }
113:
114: $expression = $reg[0];
115: list($validator, $value) = array_map('strtolower', explode('|', $reg[1]));
116:
117:
118: if ($value === 'pass') {
119: return $string;
120: }
121:
122: if ($validator === 'user') {
123: $value = isset($this->user[$value]) ? $this->user[$value] : self::getExtraValue('user', $value);
124: $string = str_replace($expression, $value, $string);
125: }
126:
127: if ($validator === 'uri') {
128: $value = isset($_GET[$value]) ? $_GET[$value] : 0;
129: $string = str_replace($expression, $value, $string);
130: }
131:
132: if ($validator === 'owner') {
133: $value = isset($this->owner[$value]) ? $this->owner[$value] : self::getExtraValue('owner', $value);
134: $string = str_replace($expression, $value, $string);
135: }
136:
137: return $string;
138: }
139:
140: function isOwner()
141: {
142: return ($this->user_uid != 0 && ($this->user_uid == $this->get_uid)) ? true : false;
143: }
144:
145: function isNotOwner()
146: {
147: return !self::isOwner();
148: }
149:
150: function getExtraValue($type = 'user', $value)
151: {
152: $xoops = Xoops::getInstance();
153: $ret = 0;
154: $values = array('pm_new', 'pm_readed', 'pm_total');
155: if (!in_array($value, $values)) {
156: return $ret;
157: }
158:
159: $entry = $this->$type;
160: if (empty($entry)) {
161: return $ret;
162: }
163:
164: $pm_handler = $xoops->getHandlerPrivateMessage();
165:
166: $criteria = new CriteriaCompo();
167: if ($value === 'pm_new') {
168: $criteria->add(new Criteria('read_msg', 0));
169: $criteria->add(new Criteria('to_userid', $entry['uid']));
170: }
171:
172: if ($value === 'pm_readed') {
173: $criteria->add(new Criteria('read_msg', 1));
174: $criteria->add(new Criteria('to_userid', $entry['uid']));
175: }
176:
177: if ($value === 'pm_total') {
178: $criteria->add(new Criteria('to_userid', $entry['uid']));
179: }
180:
181: $entry[$value] = $pm_handler->getCount($criteria);
182:
183: $this->$type = $entry;
184: unset($criteria);
185:
186: return $entry[$value];
187: }
188: }
189: