/** * Returns the DB key associated with the sort type * * @param string $sorttype the sort type * @return string */ function getSortKey($sorttype = null) { if (is_null($sorttype)) { $sorttype = $this->getSortType(); } return albumSortKey($sorttype); }
/** * Searches the table for tags * Returns an array of database records. * * @param string $searchstring * @param string $tbl set to 'albums' or 'images' * @param array $idlist list of ids seeding the search from the other fields * @return array */ function searchTags($searchstring, $tbl, $idlist) { $allIDs = null; $exact = getOption('exact_tag_match'); $sql = 'SELECT t.`name`, o.`objectid` FROM ' . prefix('tags') . ' AS t, ' . prefix('obj_to_tag') . ' AS o WHERE t.`id`=o.`tagid` AND o.`type`="' . $tbl . '" AND ('; foreach ($searchstring as $singlesearchstring) { switch ($singlesearchstring) { case '&': case '!': case '|': case '(': case ')': break; default: $targetfound = true; if ($exact) { $sql .= '`name` = "' . mysql_real_escape_string($singlesearchstring) . '" OR '; } else { $sql .= '`name` LIKE "%' . mysql_real_escape_string($singlesearchstring) . '%" OR '; } } } $sql = substr($sql, 0, strlen($sql) - 4) . ') ORDER BY t.`id`'; $objects = query_full_array($sql, true); if (is_array($objects)) { $tagid = ''; $taglist = array(); foreach ($objects as $object) { $tagid = strtolower($object['name']); if (!isset($taglist[$tagid]) || !is_array($taglist[$tagid])) { $taglist[$tagid] = array(); } $taglist[$tagid][] = $object['objectid']; } $op = ''; $idstack = array(); $opstack = array(); while (count($searchstring) > 0) { $singlesearchstring = array_shift($searchstring); switch ($singlesearchstring) { case '&': case '!': case '|': $op = $op . $singlesearchstring; break; case '(': array_push($idstack, $idlist); array_push($opstack, $op); $idlist = array(); $op = ''; break; case ')': $objectid = $idlist; $idlist = array_pop($idstack); $op = array_pop($opstack); switch ($op) { case '&': if (is_array($objectid)) { $idlist = array_intersect($idlist, $objectid); } else { $idlist = array(); } break; case '!': break; // what to do with initial NOT? // what to do with initial NOT? case '&!': if (is_array($objectid)) { $idlist = array_diff($idlist, $objectid); } break; case '': case '|': if (is_array($objectid)) { $idlist = array_merge($idlist, $objectid); } break; } $op = ''; break; default: $lookfor = strtolower($singlesearchstring); $objectid = NULL; foreach ($taglist as $key => $objlist) { if ($exact && $lookfor == $key || !$exact && preg_match('%' . $lookfor . '%', $key)) { if (is_array($objectid)) { $objectid = array_merge($objectid, $objlist); } else { $objectid = $objlist; } } } switch ($op) { case '&': if (is_array($objectid)) { $idlist = array_intersect($idlist, $objectid); } else { $idlist = array(); } break; case '!': if (is_null($allIDs)) { $allIDs = array(); $result = query_full_array("SELECT `id` FROM " . prefix($tbl)); if (is_array($result)) { foreach ($result as $row) { $allIDs[] = $row['id']; } } } $idlist = array_merge($idlist, array_diff($allIDs, $objectid)); break; case '&!': if (is_array($objectid)) { $idlist = array_diff($idlist, $objectid); } break; case '': case '|': if (is_array($objectid)) { $idlist = array_merge($idlist, $objectid); } break; } $idlist = array_unique($idlist); $op = ''; break; } $idlist = array_unique($idlist); } } if (count($idlist) == 0) { return NULL; } $sql = 'SELECT DISTINCT `id`,`show`,`title`,`desc`,'; if ($tbl == 'albums') { $sql .= "`folder` "; } else { $sql .= "`albumid`,`filename`,`location`,`city`,`state`,`country` "; } $sql .= "FROM " . prefix($tbl) . " WHERE "; if (!zp_loggedin()) { $sql .= "`show` = 1 AND "; } $sql .= "("; foreach ($idlist as $object) { $sql .= '(`id`=' . $object . ') OR '; } $sql = substr($sql, 0, strlen($sql) - 4) . ')'; if ($tbl == 'albums') { if (empty($this->dynalbumname)) { $key = subalbumSortKey(getOption('gallery_sorttype')); if (getOption('gallery_sortdirection')) { $key .= " DESC"; } } else { $gallery = new Gallery(); $album = new Album($gallery, $this->dynalbumname); $key = $album->getSubalbumSortKey(); if ($key != '`sort_order`') { if ($album->getSortDirection('album')) { $key .= " DESC"; } } } } else { if (empty($this->dynalbumname)) { $key = albumSortKey(getOption('image_sorttype')); if (getOption('image_sortdirection')) { $key .= " DESC"; } } else { $gallery = new Gallery(); $album = new Album($gallery, $this->dynalbumname); $key = $album->getSortKey(); if ($key != '`sort_order`') { if ($album->getSortDirection('image')) { $key .= " DESC"; } } } } $sql .= " ORDER BY " . $key . ""; $result = query_full_array($sql); return $result; }