Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
menu_get_assets.inc
1 <?php
27 function menu_get_assets($id_name, $majorids, $ignore_permissions = FALSE)
28 {
29 
30  $db = MatrixDAL::getDb();
31 
32  if (!isset($GLOBALS['MENU_DATA'][$id_name])) {
33  $GLOBALS['MENU_DATA'][$id_name] = Array();
34  }
35  $storage =& $GLOBALS['MENU_DATA'][$id_name]; // need ref
36  $assetids = Array();
37  $majorids_str = '';
38  foreach ($majorids as $id) {
39  if (empty($storage[$id])) {
40  $majorids_str .= '\''.(string)$id.'\',';
41  } else {
42  foreach ($storage[$id] as $row) {
43  $assetids[] = (int) $row['assetid'];
44  }
45  }
46  }
47 
48  if (empty($majorids_str)) return $assetids;
49 
50  // Watch out for this static var, once it has been set, it won't go into the if is_null. However the bind vars will need to be there because this static var contain the query that uses bind vars.
51  static $USERIDS_COND = NULL;
52  static $USERIDS_BIND_VARS = NULL;
53  $bind_vars = Array();
54 
55  $public_userid = $GLOBALS['SQ_SYSTEM']->am->getSystemAssetid('public_user');
56  if (is_null($USERIDS_COND)) {
57  // if they are logged in add their parents to the list
58  if ($GLOBALS['SQ_SYSTEM']->userRoot() || $GLOBALS['SQ_SYSTEM']->userSystemAdmin() || $ignore_permissions) {
59  $USERIDS_COND = '';
60  } else {
61  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($GLOBALS['SQ_SYSTEM']->currentUserId());
62  $userids = $user->getUserGroups();
63  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($user);
64  $userids[] = $public_userid;
65  $userids[] = $GLOBALS['SQ_SYSTEM']->user->id;
66  $userids_bind_vars = Array();
67  $userids_vars_p = Array();
68  $userids_vars_r = Array();
69  for (reset($userids); NULL !== ($i = key($userids)); next($userids)) {
70  $userids_vars_p[] = ':mga_userids_p_'.$i;
71  $userids_bind_vars['mga_userids_p_'.$i] = (string) $userids[$i];
72  $userids_vars_r[] = ':mga_userids_r_'.$i;
73  $userids_bind_vars['mga_userids_r_'.$i] = (string) $userids[$i];
74  }//end for
75  $USERIDS_BIND_VARS = serialize($userids_bind_vars);
76  $userids_p_str = implode(', ', $userids_vars_p);
77  $userids_r_str = implode(', ', $userids_vars_r);
78  $USERIDS_COND = 'AND (p.userid IN ('.$userids_p_str.') ';
79  $USERIDS_COND .= 'OR r.userid IN ('.$userids_r_str.')) ';
80  $USERIDS_COND .= ' AND (
81  (p.permission = :p_permission AND (
82  p.userid <> :p_userid
83  OR r.userid <> :r_userid
84  OR (p.userid = :p_userid_1 AND p.granted = \'1\')
85  OR (r.userid = :r_userid_1 AND p.granted = \'1\')
86  )
87  )
88  OR (
89  p.permission > :p_permission_1 AND p.granted = \'1\'
90  )
91  )';
92  $USERIDS_COND .= '
93  GROUP BY a.assetid, l.majorid, a.type_code, a.status, a.name, a.short_name, pt.path, l.sort_order, p.assetid
94  HAVING MIN(p.granted) <> \'0\'';
95 
96  }//end else
97 
98  }//end if
99 
100  // This check to avoid the case when the viewing entity is root user or sys admins, trying to bind vars to an empty query will result in Fatal error from Oracle. Need to be explicitly not empty string.
101  if ($USERIDS_COND !== '') {
102  $bind_vars['p_permission'] = SQ_PERMISSION_READ;
103  $bind_vars['p_permission_1'] = SQ_PERMISSION_READ;
104  $bind_vars['p_userid'] = $public_userid;
105  $bind_vars['p_userid_1'] = $public_userid;
106  $bind_vars['r_userid'] = $public_userid;
107  $bind_vars['r_userid_1'] = $public_userid;
108  }//end if
109 
110  // Add the User IDs bind vars
111  if (!is_null($USERIDS_BIND_VARS)) {
112  $userids_bind_vars_values = @unserialize($USERIDS_BIND_VARS);
113  foreach ($userids_bind_vars_values as $bind_id => $bind_value) {
114  $bind_vars[$bind_id] = $bind_value;
115  }
116  }
117 
118  $sql = 'SELECT DISTINCT a.assetid, l.majorid, a.type_code, a.status, a.name, a.short_name, pt.path, l.sort_order
119  FROM '.SQ_TABLE_RUNNING_PREFIX.'ast a
120  INNER JOIN '.SQ_TABLE_RUNNING_PREFIX.'ast_lnk l ON a.assetid = l.minorid
121  INNER JOIN '.SQ_TABLE_RUNNING_PREFIX.'ast_path pt ON a.assetid = pt.assetid ';
122  if (!empty($USERIDS_COND)) {
123  $sql .= '
124  INNER JOIN '.SQ_TABLE_RUNNING_PREFIX.'ast_perm p ON a.assetid = p.assetid ';
125  $sql .= '
126  LEFT JOIN '.SQ_TABLE_RUNNING_PREFIX.'vw_ast_role r ON p.userid = r.roleid AND p.assetid = r.assetid ';
127  }
128  $where = 'l.majorid IN ('.substr($majorids_str, 0, -1).')
129  AND '.db_extras_bitand(MatrixDAL::getDbType(), 'l.link_type', SQ_SC_LINK_FRONTEND_NAV).' > 0 ';
130  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'a');
131  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'l');
132  if (!empty($USERIDS_COND)) {
133  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'p');
134  }
135  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'pt');
136  $where .= ' '.$USERIDS_COND.'
137  ORDER BY l.majorid, l.sort_order';
138 
139  try {
140  $query = MatrixDAL::preparePdoQuery($sql.$where);
141  foreach ($bind_vars as $bind_var => $bind_value) {
142  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
143  }
144  $result = MatrixDAL::executePdoAssoc($query);
145  } catch (Exception $e) {
146  throw new Exception('Unable to get valid menu entries due to database error: '.$e->getMessage());
147  }
148 
149  $assetids = Array();
150 
151  foreach ($result as $row) {
152  if (!($row['status'] & (SQ_STATUS_LIVE | SQ_STATUS_LIVE_APPROVAL))) {
153  $menu_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($row['assetid']);
154  $read_access = $menu_asset->readAccess();
155 
156  $row['name'] = $menu_asset->name;
157  $row['short_name'] = $menu_asset->short_name;
158 
159  if ($row['status'] & SQ_SC_STATUS_NOT_LIVE) {
160  // somewhere between under construction and live so we show this by altering the name
161  $row['name'] = '(( '.$row['name'].' ))';
162  $row['short_name'] = '(( '.$row['short_name'].' ))';
163  }
164 
165  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($menu_asset);
166  unset($menu_asset);
167  if (!$read_access) continue;
168  }
169  $assetids[] = $row['assetid'];
170  if (!isset($storage[$row['majorid']])) {
171  $storage[$row['majorid']] = Array();
172  }
173  $storage[$row['majorid']][$row['assetid']] = $row;
174 
175  }
176 
177  return $assetids;
178 
179 }//end menu_get_assets()
180 
181 
182 ?>