function getAPISearchByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL) { if ($company_id == '') { return FALSE; } if (!is_array($order)) { //Use Filter Data ordering if its set. if (isset($filter_data['sort_column']) and $filter_data['sort_order']) { $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']); } } $additional_order_fields = array('status_id', 'last_login_date', 'total_active_days', 'last_login_days', 'this_month_max_active_users', 'this_month_avg_active_users', 'this_month_min_active_users', 'last_month_max_active_users', 'last_month_avg_active_users', 'last_month_min_active_users'); $sort_column_aliases = array('status' => 'status_id', 'product_edition' => 'product_edition_id'); $order = $this->getColumnsFromAliases($order, $sort_column_aliases); if ($order == NULL) { $order = array('status_id' => 'asc', 'name' => 'asc'); $strict = FALSE; } else { //Always try to order by status first so INACTIVE employees go to the bottom. if (!isset($order['status_id'])) { $order = Misc::prependArray(array('status_id' => 'asc'), $order); } //Always sort by last name,first name after other columns if (!isset($order['name'])) { $order['name'] = 'asc'; } $strict = TRUE; } //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10); //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10); $uf = new UserFactory(); $cuff = new CompanyUserCountFactory(); $ph = array(); $query = ' select a.*, _ADODB_COUNT (select max(last_login_date) from ' . $uf->getTable() . ' as uf where uf.company_id = a.id ) as last_login_date, ((select max(last_login_date) from ' . $uf->getTable() . ' as uf where uf.company_id = a.id )-a.created_date) as total_active_days, (' . time() . '-(select max(last_login_date) from ' . $uf->getTable() . ' as uf where uf.company_id = a.id )) as last_login_days, (select min(active_users) as min_active_users from ' . $cuff->getTable() . ' as cuff where cuff.company_id = a.id AND date_stamp >= ' . $this->db->qstr($this->db->BindDate(TTDate::getBeginMonthEpoch())) . ' AND date_stamp <= ' . $this->db->qstr($this->db->BindDate(time())) . ' ) as this_month_min_active_users, (select avg(active_users) as avg_active_users from ' . $cuff->getTable() . ' as cuff where cuff.company_id = a.id AND date_stamp >= ' . $this->db->qstr($this->db->BindDate(TTDate::getBeginMonthEpoch())) . ' AND date_stamp <= ' . $this->db->qstr($this->db->BindDate(time())) . ' ) as this_month_avg_active_users, (select max(active_users) as max_active_users from ' . $cuff->getTable() . ' as cuff where cuff.company_id = a.id AND date_stamp >= ' . $this->db->qstr($this->db->BindDate(TTDate::getBeginMonthEpoch())) . ' AND date_stamp <= ' . $this->db->qstr($this->db->BindDate(time())) . ' ) as this_month_max_active_users, (select min(active_users) as min_active_users from ' . $cuff->getTable() . ' as cuff where cuff.company_id = a.id AND date_stamp >= ' . $this->db->qstr($this->db->BindDate(TTDate::getBeginMonthEpoch(TTDate::getBeginMonthEpoch() - 86400))) . ' AND date_stamp <= ' . $this->db->qstr($this->db->BindDate(TTDate::getEndMonthEpoch(TTDate::getBeginMonthEpoch() - 86400))) . ' ) as last_month_min_active_users, (select avg(active_users) as avg_active_users from ' . $cuff->getTable() . ' as cuff where cuff.company_id = a.id AND date_stamp >= ' . $this->db->qstr($this->db->BindDate(TTDate::getBeginMonthEpoch(TTDate::getBeginMonthEpoch() - 86400))) . ' AND date_stamp <= ' . $this->db->qstr($this->db->BindDate(TTDate::getEndMonthEpoch(TTDate::getBeginMonthEpoch() - 86400))) . ' ) as last_month_avg_active_users, (select max(active_users) as max_active_users from ' . $cuff->getTable() . ' as cuff where cuff.company_id = a.id AND date_stamp >= ' . $this->db->qstr($this->db->BindDate(TTDate::getBeginMonthEpoch(TTDate::getBeginMonthEpoch() - 86400))) . ' AND date_stamp <= ' . $this->db->qstr($this->db->BindDate(TTDate::getEndMonthEpoch(TTDate::getBeginMonthEpoch() - 86400))) . ' ) as last_month_max_active_users, y.first_name as created_by_first_name, y.middle_name as created_by_middle_name, y.last_name as created_by_last_name, z.first_name as updated_by_first_name, z.middle_name as updated_by_middle_name, z.last_name as updated_by_last_name _ADODB_COUNT from ' . $this->getTable() . ' as a LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 ) LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 ) where 1=1 '; $query .= isset($filter_data['permission_children_ids']) ? $this->getWhereClauseSQL('a.created_by', $filter_data['permission_children_ids'], 'numeric_list', $ph) : NULL; $query .= isset($filter_data['id']) ? $this->getWhereClauseSQL('a.id', $filter_data['id'], 'numeric_list', $ph) : NULL; $query .= isset($filter_data['exclude_id']) ? $this->getWhereClauseSQL('a.id', $filter_data['exclude_id'], 'not_numeric_list', $ph) : NULL; if (isset($filter_data['status']) and trim($filter_data['status']) != '' and !isset($filter_data['status_id'])) { $filter_data['status_id'] = Option::getByFuzzyValue($filter_data['status'], $this->getOptions('status')); } $query .= isset($filter_data['status_id']) ? $this->getWhereClauseSQL('a.status_id', $filter_data['status_id'], 'numeric_list', $ph) : NULL; $query .= isset($filter_data['name']) ? $this->getWhereClauseSQL('a.name', $filter_data['name'], 'text_metaphone', $ph) : NULL; $query .= isset($filter_data['short_name']) ? $this->getWhereClauseSQL('a.short_name', $filter_data['short_name'], 'text', $ph) : NULL; $query .= isset($filter_data['product_edition_id']) ? $this->getWhereClauseSQL('a.product_edition_id', $filter_data['product_edition_id'], 'numeric_list', $ph) : NULL; $query .= isset($filter_data['country']) ? $this->getWhereClauseSQL('a.country', $filter_data['country'], 'upper_text_list', $ph) : NULL; $query .= isset($filter_data['province']) ? $this->getWhereClauseSQL('a.province', $filter_data['province'], 'upper_text_list', $ph) : NULL; $query .= isset($filter_data['city']) ? $this->getWhereClauseSQL('a.city', $filter_data['city'], 'text', $ph) : NULL; $query .= isset($filter_data['address1']) ? $this->getWhereClauseSQL('a.address1', $filter_data['address1'], 'text', $ph) : NULL; $query .= isset($filter_data['address2']) ? $this->getWhereClauseSQL('a.address2', $filter_data['address2'], 'text', $ph) : NULL; $query .= isset($filter_data['postal_code']) ? $this->getWhereClauseSQL('a.postal_code', $filter_data['postal_code'], 'text', $ph) : NULL; $query .= isset($filter_data['work_phone']) ? $this->getWhereClauseSQL('a.work_phone', $filter_data['work_phone'], 'phone', $ph) : NULL; $query .= isset($filter_data['fax_phone']) ? $this->getWhereClauseSQL('a.fax_phone', $filter_data['fax_phone'], 'phone', $ph) : NULL; $query .= isset($filter_data['business_number']) ? $this->getWhereClauseSQL('a.business_number', $filter_data['businessnumber'], 'text', $ph) : NULL; $query .= isset($filter_data['created_by']) ? $this->getWhereClauseSQL(array('a.created_by', 'y.first_name', 'y.last_name'), $filter_data['created_by'], 'user_id_or_name', $ph) : NULL; $query .= isset($filter_data['updated_by']) ? $this->getWhereClauseSQL(array('a.updated_by', 'z.first_name', 'z.last_name'), $filter_data['updated_by'], 'user_id_or_name', $ph) : NULL; $query .= ' AND a.deleted = 0 '; $query .= $this->getWhereSQL($where); $query .= $this->getSortSQL($order, $strict, $additional_order_fields); $this->ExecuteSQL($query, $ph, $limit, $page); return $this; }