/** * Search paginated * * @param string $search_for Search string * @param Project $project Search in this project * @param boolean $include_private * @param integer $items_per_page * @param integer $current_page * @return array */ static function searchPaginated($search_for, Project $project, $include_private = false, $items_per_page = 10, $current_page = 1) { $conditions = SearchableObjects::getSearchConditions($search_for, $project, $include_private); $pagination = new DataPagination(SearchableObjects::countUniqueObjects($conditions), $items_per_page, $current_page); $items = SearchableObjects::doSearch($conditions, $pagination->getItemsPerPage(), $pagination->getLimitStart()); return array($items, $pagination); }
/** * Render simple pagination (links to pages spearated with commas). Example: * * simple_pages($pager, 'http://www.google.com?page=#PAGE#', '#PAGE#'); * * @access public * @param DataPagination $pagination Pagination object * @param string $url_base Base URL where $page_placeholder will be replaced with * current page number * @param string $page_placeholder Short string inside of $url_base witch will be * replaced with current page number * @param string $separator String that will separate page URLs * @return string */ function simple_pagination(DataPagination $pagination, $url_base, $page_placeholder = '#PAGE#', $separator = ', ') { $page_urls = array(); for ($i = 1; $i <= $pagination->getTotalPages(); $i++) { if ($i == $pagination->getCurrentPage()) { $page_urls[] = "({$i})"; } else { $page_urls[] = '<a href="' . str_replace($page_placeholder, $i, $url_base) . '">' . $i . '</a>'; } // if } // for return count($page_urls) ? implode($separator, $page_urls) : ''; }
static function searchByType($search_for, $project_csvs, $object_type = '', $include_private = false, $items_per_page = 10, $current_page = 1, $columns_csv = null, $user_id = 0) { $remaining = 0; $safe_search_for = str_replace("'", '"', $search_for); $conditions = SearchableObjects::getSearchConditions($safe_search_for, $project_csvs, true, $object_type, $columns_csv, $user_id); $count = SearchableObjects::countUniqueObjects($conditions); $pagination = new DataPagination($count, $items_per_page, $current_page); if ($count > 0) { $items = SearchableObjects::doSearch($conditions, $pagination->getItemsPerPage(), $pagination->getLimitStart(), $search_for); } else { $items = array(); } return array($items, $pagination); }
/** * Returns a list of emails according to the requested parameters * * @param string $tag * @param array $attributes * @param Project $project * @return array */ function getEmails($tag = null, $account_id = null, $state = null, $read_filter = "", $classif_filter = "", $project = null, $start = null, $limit = null, $order_by = 'received_date', $dir = 'ASC', $archived = false, $count = false) { // Check for accounts $accountConditions = ""; if (isset($account_id) && $account_id > 0) { //Single account $accountConditions = " AND `account_id` = " . DB::escape($account_id); } // Check for unclassified emails if ($classif_filter != '' && $classif_filter != 'all') { if ($classif_filter == 'unclassified') { $classified = "AND NOT "; } else { $classified = "AND "; } $classified .= "`id` IN (SELECT `object_id` FROM `" . TABLE_PREFIX . "workspace_objects` WHERE `object_manager` = 'MailContents')"; } else { $classified = ""; } // Check for drafts emails if ($state == "draft") { $stateConditions = " `state` = '2'"; } else { if ($state == "sent") { $stateConditions = " (`state` = '1' OR `state` = '3' OR `state` = '5')"; } else { if ($state == "received") { $stateConditions = " (`state` = '0' OR `state` = '5')"; } else { if ($state == "junk") { $stateConditions = " `state` = '4'"; } else { if ($state == "outbox") { $stateConditions = " `state` >= 200"; } else { $stateConditions = ""; } } } } } // Check read emails if ($read_filter != "" && $read_filter != "all") { if ($read_filter == "unread") { $read = "AND NOT "; $subread = "AND NOT `mc`."; } else { $read = "AND "; $subread = "AND `mc`."; } $read2 = "`id` IN (SELECT `rel_object_id` FROM `" . TABLE_PREFIX . "read_objects` `t` WHERE `user_id` = " . logged_user()->getId() . " AND `t`.`rel_object_manager` = 'MailContents' AND `t`.`is_read` = '1')"; $read .= $read2; $subread .= $read2; } else { $read = ""; $subread = ""; } //Check for tags if (!isset($tag) || $tag == '' || $tag == null) { $tagstr = ""; // dummy condition $subtagstr = ""; } else { $tagstr = "AND (SELECT count(*) FROM `" . TABLE_PREFIX . "tags` WHERE `" . TABLE_PREFIX . "mail_contents`.`id` = `" . TABLE_PREFIX . "tags`.`rel_object_id` AND `" . TABLE_PREFIX . "tags`.`tag` = " . DB::escape($tag) . " AND `" . TABLE_PREFIX . "tags`.`rel_object_manager` ='MailContents' ) > 0 "; $subtagstr = "AND (SELECT count(*) FROM `" . TABLE_PREFIX . "tags` WHERE " . "`mc`.`id` = `" . TABLE_PREFIX . "tags`.`rel_object_id` AND `" . TABLE_PREFIX . "tags`.`tag` = " . DB::escape($tag) . " AND `" . TABLE_PREFIX . "tags`.`rel_object_manager` ='MailContents' ) > 0 "; } $permissions = ' AND ( ' . permissions_sql_for_listings(MailContents::instance(), ACCESS_LEVEL_READ, logged_user(), $project instanceof Project ? $project->getId() : 0) . ')'; //Check for projects (uses accountConditions if ($project instanceof Project) { $pids = $project->getAllSubWorkspacesQuery(!$archived); $projectConditions = " AND " . self::getWorkspaceString($pids); } else { $projectConditions = ""; } if ($archived) { $archived_cond = "AND `archived_by_id` <> 0"; } else { $archived_cond = "AND `archived_by_id` = 0"; } $state_conv_cond_1 = $state != 'received' ? " {$stateConditions} AND " : " `state` <> '2' AND "; $state_conv_cond_2 = $state != 'received' ? " AND (`mc`.`state` = '1' OR `mc`.`state` = '3' OR `mc`.`state` = '5') " : " AND `mc`.`state` <> '2' "; if (user_config_option('show_emails_as_conversations')) { $archived_by_id = $archived ? "AND `mc`.`archived_by_id` != 0" : "AND `mc`.`archived_by_id` = 0"; $trashed_by_id = "AND `mc`.`trashed_on` = " . DB::escape(EMPTY_DATETIME); $conversation_cond = "AND IF(`conversation_id` = 0, {$stateConditions}, {$state_conv_cond_1} NOT EXISTS (SELECT * FROM `" . TABLE_PREFIX . "mail_contents` `mc` WHERE `" . TABLE_PREFIX . "mail_contents`.`conversation_id` = `mc`.`conversation_id` AND `" . TABLE_PREFIX . "mail_contents`.`account_id` = `mc`.`account_id` AND `" . TABLE_PREFIX . "mail_contents`.`received_date` < `mc`.`received_date` {$archived_by_id} AND `mc`.`is_deleted` = 0 {$trashed_by_id} {$subtagstr} {$subread} {$state_conv_cond_2}))"; $box_cond = "AND IF(EXISTS(SELECT * FROM `" . TABLE_PREFIX . "mail_contents` `mc` WHERE `" . TABLE_PREFIX . "mail_contents`.`conversation_id` = `mc`.`conversation_id` AND `" . TABLE_PREFIX . "mail_contents`.`id` <> `mc`.`id` AND `" . TABLE_PREFIX . "mail_contents`.`account_id` = `mc`.`account_id` {$archived_by_id} AND `mc`.`is_deleted` = 0 {$trashed_by_id} AND {$stateConditions}), TRUE, {$stateConditions})"; } else { $conversation_cond = ""; $box_cond = "AND {$stateConditions}"; } $conditions = "`is_deleted` = 0 {$archived_cond} {$projectConditions} {$accountConditions} {$tagstr} {$classified} {$read} {$permissions} {$conversation_cond} {$box_cond}"; if ($count) { return self::count($conditions); } else { if (!defined('EMAIL_PERFORMANCE_WORKAROUND') || !EMAIL_PERFORMANCE_WORKAROUND) { return self::paginate(array('conditions' => $conditions, 'order' => "{$order_by} {$dir}"), config_option('files_per_page'), $start / $limit + 1); } else { /* COMPLEX WORKAROUND FOR PERFORMANCE */ $conditions = "`is_deleted` = 0 {$archived_cond} {$projectConditions} {$tagstr} {$classified} {$read} {$accountConditions}"; $page = (int) ($start / $limit) + 1; $order = "{$order_by} {$dir}"; $count = null; if (defined('INFINITE_PAGING') && INFINITE_PAGING) { $count = 10000000; } $pagination = new DataPagination($count ? $count : self::count($conditions), $limit, $page); $ids = self::findAll(array('conditions' => $conditions, 'id' => true)); $ids = array_reverse($ids); $ret_ids = array(); $offset = 0; $block_len = $pagination->getItemsPerPage(); while (count($ret_ids) < $pagination->getLimitStart() + $pagination->getItemsPerPage() && $offset < count($ids)) { $tmp_ids = array(); for ($i = $offset; $i < count($ids) && $i < $offset + $block_len; $i++) { $tmp_ids[] = $ids[$i]; } $tmp_ids = self::findAll(array('conditions' => "`id` IN (" . implode(",", $tmp_ids) . ") {$permissions} {$conversation_cond} {$box_cond}", 'order' => $order, 'id' => true)); $ret_ids = array_merge($ret_ids, $tmp_ids); $offset += $block_len; } $ids = array(); for ($i = $pagination->getLimitStart(); $i < count($ret_ids); $i++) { $ids[] = $ret_ids[$i]; } $objects = array(); foreach ($ids as $id) { $objects[] = self::findById($id); } return array($objects, $pagination); } } }
/** * This function will return paginated result. Result is array where first element is * array of returned object and second populated pagination object that can be used for * obtaining and rendering pagination data using various helpers. * * Items and pagination array vars are indexed with 0 for items and 1 for pagination * because you can't use associative indexing with list() construct * * @access public * @param array $arguments Query argumens (@see find()) Limit and offset are ignored! * @param integer $items_per_page Number of items per page * @param integer $current_page Current page number * @return array */ function paginate($arguments = null, $items_per_page = 10, $current_page = 1, $count = null) { if(!is_array($arguments)) $arguments = array(); $conditions = array_var($arguments, 'conditions'); if (defined('INFINITE_PAGING') && INFINITE_PAGING) $count = 10000000; $pagination = new DataPagination($count ? $count : $this->count($conditions), $items_per_page, $current_page); $arguments['offset'] = $pagination->getLimitStart(); $arguments['limit'] = $pagination->getItemsPerPage(); $items = $this->findAll($arguments); return array($items, $pagination); } // paginate
/** * This function will return paginated result. Result is array where first element is * array of returned object and second populated pagination object that can be used for * obtaining and rendering pagination data using various helpers. * * Items and pagination array vars are indexed with 0 for items and 1 for pagination * because you can't use associative indexing with list() construct * * @access public * @param array $arguments Query argumens (@see find()) Limit and offset are ignored! * @param integer $items_per_page Number of items per page * @param integer $current_page Current page number * @return array */ function paginate($arguments = null, $items_per_page = 10, $current_page = 1) { if (!is_array($arguments)) { $arguments = array(); } // if $conditions = array_var($arguments, 'conditions'); $pagination = new DataPagination($this->count($conditions), $items_per_page, $current_page); $arguments['offset'] = $pagination->getLimitStart(); $arguments['limit'] = $pagination->getItemsPerPage(); $items = $this->findAll($arguments); return array($items, $pagination); }
function paginateOrderByName($arguments = null, $items_per_page = 10, $current_page = 1) { if (!is_array($arguments)) { $arguments = array(); } $conditions = array_var($arguments, 'conditions'); $contactsTableName = Contacts::instance()->getTableName(true); $pagination = new DataPagination($this->count($conditions), $items_per_page, $current_page); if (strlen($conditions) > 0) { $conditions .= " AND " . $this->getTableName(true) . ".contact_id = {$contactsTableName}.id"; } else { $conditions = $this->getTableName(true) . ".contact_id = {$contactsTableName}.id"; } $offset = $pagination->getLimitStart(); $limit = $pagination->getItemsPerPage(); $sql = "SELECT " . $this->getTableName(true) . ".* FROM " . $this->getTableName(true) . ", {$contactsTableName}" . " WHERE {$conditions} ORDER BY UPPER(lastname) ASC, UPPER(firstname) ASC LIMIT {$offset}, {$limit}"; // Run! $rows = DB::executeAll($sql); if (!is_array($rows) || count($rows) < 1) { $items = null; } $objects = array(); foreach ($rows as $row) { $object = $this->loadFromRow($row); if (instance_of($object, $this->getItemClass())) { $objects[] = $object; } } // foreach $items = count($objects) ? $objects : null; return array($items, $pagination); }