setDBO(VoxelCareDB::getDatabase()); //global $option; $this->filter_order_Dir = $mainframe->getUserStateFromRequest( $option.$view.'.filter_order_Dir', 'filter_order_Dir', 'ASC', 'word' ); $this->filter_order = $mainframe->getUserStateFromRequest( $option.$view.'.filter_order', 'filter_order', 'name', 'cmd' ); $this->searchname = $mainframe->getUserStateFromRequest( "$option.$view.searchname", 'searchname', '', 'string' ); $this->searchname = JString::strtolower( $this->searchname ); $this->searchfranchise = $mainframe->getUserStateFromRequest( "$option.$view.searchfranchise", 'searchfranchise', '', 'string' ); if (!$this->searchfranchise) { $userId = LegacyHelper::getUserId(); $this->_db->setQuery("SELECT uf.franchise id FROM #__vxc_userfranchise uf WHERE uf.userid = ".$userId); $this->searchfranchise = $this->_db->loadResult(); } $this->searchgroup = $mainframe->getUserStateFromRequest( "$option.$view.searchgroup", 'searchgroup', '', 'string' ); if (!$this->searchgroup) { $userId =LegacyHelper::getUserId(); $this->_db->setQuery("SELECT ug.group id FROM #__vxc_usergroup ug WHERE ug.manager = ".$userId." OR ug.manager is NULL"); $this->searchgroup = $this->_db->loadResult(); } $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', 10, 'int' ); $limitstart = JRequest::getVar('limitstart',0); $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0); $this->setState('limit', $limit); $this->setState('limitstart', $limitstart); } function _buildQuery() { $where = array(); $groupby= array(); $having=array(); if ($this->searchname) $where[] = " (u.name LIKE '%".$this->searchname."%' OR u.username LIKE '%".$this->searchname."%')"; if ($this->searchfranchise) $where[] = " f.id = " . $this->searchfranchise; if ($this->_isFranchiseManager) { if ($this->searchgroup) $where[] = " ug.group = ". $this->searchgroup; } else if ($this->_isGroupManager) { if (!$this->searchgroup) { $userId =LegacyHelper::getUserId(); $where[] = " g.manager = ".$userId; } else $where[] = " ug.group = ". $this->searchgroup; } if (!$this->_isAdmin) $where[] = " u.username <> 'admin' "; $where[] = " uf.deleted IS NOT TRUE "; $where =( count($where) ) ? ' WHERE ' . implode( ' AND ', $where ) : ''; $having =( count($having) ) ? ' HAVING ' . implode( ' AND ', $having ) : ''; $groupby =( count($groupby) ) ? ' GROUP BY ' . implode( ' , ', $groupby ) : ''; $orderby = ''; $orders = array('id','name','username','credit'); if (!in_array($this->filter_order,$orders)) $this->filter_order = 'id'; if (($this->filter_order) ) { $orderby = ' order by '. $this->filter_order .' '.$this->filter_order_Dir; } $this->_query = " SELECT u.id id, u.name name, u.username username, ( u.amount_paid - u.amount_ordered ) credit FROM #__vxc_user u LEFT JOIN #__vxc_userfranchise uf ON uf.userid = u.id LEFT JOIN #__vxc_franchise f ON uf.franchise = f.id LEFT JOIN #__vxc_usergroup ug ON ug.user = u.id LEFT JOIN #__vxc_group g ON g.id = ug.group " . $where . $groupby . $having . $orderby ; return $this->_query; } function getData() { if (empty($this->_data)) { $query = $this->_buildQuery(); $this->_db->setQuery( $query,$this->getState('limitstart'), $this->getState('limit') ); $this->_data = $this->_db->loadObjectList(); } return $this->_data; } function getList() { // table ordering $lists['order_Dir'] = $this->filter_order_Dir; $lists['order'] = $this->filter_order; // search filter $lists['searchname']= $this->searchname; $lists['searchfranchise']= $this->searchfranchise; $lists['searchgroup'] = $this->searchgroup; $lists['groups'] = $this->getGroups(); return $lists; } function getTotal() { // Load the content if it doesn't already exist if (empty($this->_total)) { $query = $this->_buildQuery(); $this->_total = $this->_getListCount($query); } return $this->_total; } function getPagination() { if (empty($this->_pagination)) { jimport('joomla.html.pagination'); $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') ); } return $this->_pagination; } function getGroupManagers() { $franchiseId = $this->searchfranchise; if (!$franchiseId ) return array(); $sql = "SELECT u.id , u.name FROM #__vxc_user u LEFT JOIN #__vxc_userfranchise uf ON uf.userid= u.id WHERE uf.franchise = $franchiseId AND u.privileges LIKE '%Group manager%'"; $this->_db->setQuery($sql); return $this->_db->loadObjectList(); } function getGroups() { $franchiseId = $this->searchfranchise; if (!$franchiseId ) return array(); $userId = LegacyHelper::getUserId(); if (!$userId) return array(); $db =& $this->_db; if ($this->_isAdmin || $this->_isFranchiseManager) { $sql = "SELECT g.id id, g.name name FROM #__vxc_group g WHERE g.franchise = $franchiseId ORDER BY g.name"; } else { $sql = "SELECT g.id id, g.name name FROM #__vxc_group g WHERE g.franchise = $franchiseId AND (g.manager IS NULL OR g.manager = ".$userId.") ORDER BY g.name"; } $db->setQuery($sql); return $db->loadObjectList(); } }// class