=> 'add_date_gteq', 'date_lteq' => 'add_date_lteq', 'groupid' => 'add_groupid', 'members_gteq' => 'add_members_gteq', 'members_lteq' => 'add_members_lteq', 'discussion_gteq' => 'add_discussion_gteq', 'discussion_lteq' => 'add_discussion_lteq', 'message_gteq' => 'add_messages_gteq', 'message_lteq' => 'add_messages_lteq', 'picture_gteq' => 'add_pictures_gteq', 'picture_lteq' => 'add_pictures_lteq', 'member' => 'add_member', 'membertype' => 'add_membertype', 'creator' => 'add_creator', 'pending' => 'add_pending', 'type' => 'add_type', 'moderatedgms' => 'add_moderatedgms', 'subscribed' => 'add_subscribed' ); /** * List of valid sorting fields. * Key is the unique ID (from the form), value is the column name * * @var array */ var $valid_sort = array( 'members' => 'socialgroup.members', 'created' => 'socialgroup.dateline', 'name' => 'socialgroup.name', 'category' => 'sgc.title', 'pictures' => 'socialgroup.picturecount', // see constructor: this is modified 'messages' => 'socialgroup.visible', 'lastpost' => 'socialgroup.lastpost', 'discussions' => 'socialgroup.discussions' ); /** * @var vB_Registry */ var $registry = null; /** * List of errors (DM style) * * @var array */ var $errors = array(); /** * Where clause pieces. Will be ANDed together * * @var array */ var $where = array(); /** * List of joins necessary * * @var array */ var $joins = array(); /** * The Query Limit offset * * @var integer */ var $limitoffset = 0; /** * The Query Limit * * @var integer */ var $limit = 0; /** * Constructor. * * @param vB_Registry */ function vB_SGSearchGenerator(&$registry) { $this->registry =& $registry; // need to update the picture count if (isset($this->valid_sort['pictures'])) { $this->valid_sort['pictures'] = 'IF(socialgroup.options & ' . $registry->bf_misc_socialgroupoptions['enable_group_albums'] . ', socialgroup.picturecount, 0)'; } } /** * Determines whether the current search has errors * * @return boolean */ function has_errors() { return !empty($this->errors); } /** * Verifies the sorting field and grabs the necessary data * * @param string (In/Out) The specified sort field, translated to the column name * @param string (In/Out) The specified sort order, translated to the appropriate safe value * @param string (Output) Raw sort field passed in * @param string (Output) Raw sort order passed in * * @return bool Returns true unless something fails. (Always returns true right now) */ function verify_sort(&$sort, &$sortorder, &$sort_raw, &$sortorder_raw, &$sort_criteria) { $sort_raw = $sort; $sortorder_raw = $sortorder; if (!isset($this->valid_sort["$sort"])) { $sort = 'socialgroup.dateline'; $sort_raw = 'dateline'; } else { $sort = $this->valid_sort["$sort"]; } switch (strtolower($sortorder)) { case 'asc': case 'desc': break; default: $sortorder = 'DESC'; $sortorder_raw = 'DESC'; } $sort_criteria = $sort . ' ' . $sortorder; if ($sort_raw != 'created') { $newsort = 'created'; $newsort_order = $sortorder; $this->verify_sort($newsort, $newsort_order, $newsort_raw, $newsort_order_raw, $newsort_criteria); $sort_criteria .= ', ' . $newsort_criteria; } return true; } /** * Adds a search criteria * * @param string Name of criteria * @param mixed How to restrict the criteria * * @return boolean True on success */ function add($name, $value) { if (!isset($this->valid_fields["$name"])) { $this->error('sg_search_field_x_unknown', htmlspecialchars_uni($name)); return SG_SEARCHGEN_CRITERIA_FAILED; } $raw = $value; $add_method = $this->valid_fields["$name"]; return $this->$add_method($name, $value); } /** * Adds an error to the list, phrased for the current user. 1 or more arguments * * @param string Error phrase name */ function error($errorphrase) { $args = func_get_args(); if (is_array($errorphrase)) { $error = fetch_error($errorphrase); } else { $error = call_user_func_array('fetch_error', $args); } $this->errors[] = $error; } /** * Generates the search query bits * * @return array|false False if error, array consisting of joins and where clause otherwise */ function generate() { if (!$this->has_errors()) { if (!empty($this->limit)) { $limit = "LIMIT " . (!empty($this->limitoffset) ? $this->limitoffset . ',' : '') . $this->limit; } else { $limit = ''; } return array( 'columns' => '', 'joins' => implode("\n", $this->joins), 'where' => implode("\nAND ", $this->where), 'limit' => $limit ); } else { return false; } } /** * Prepares a criteria that may either be a scalar or an array * * @param mixed Value to process * @param callback Callback function to call on each value * @param string Text to implode the array with * * @return mixed Returns true if the array is empty, otherwise the processed values */ function prepare_scalar_array($value, $callback = '', $array_splitter = ',') { if (is_array($value)) { if ($callback) { $value = array_map($callback, $value); } $value = array_values($value); if (count($value) == 0 OR (count($value) == 1 AND empty($value[0]))) { return call_user_func($callback, ''); } else { return implode($array_splitter, $value); } } else if ($callback) { return call_user_func($callback, $value); } else { return $value; } } /** * Adds group ID criteria * * @param string * @param integer|array * * @return bool