add order metatag
not too happy with how this works...but it does work
This commit is contained in:
		
							parent
							
								
									9cae856df7
								
							
						
					
					
						commit
						2c2f27ca64
					
				@ -26,6 +26,7 @@
 | 
				
			|||||||
$tag_n = 0; // temp hack
 | 
					$tag_n = 0; // temp hack
 | 
				
			||||||
$_flexihash = null;
 | 
					$_flexihash = null;
 | 
				
			||||||
$_fh_last_opts = null;
 | 
					$_fh_last_opts = null;
 | 
				
			||||||
 | 
					$order_sql = null; // this feels ugly
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require_once "lib/flexihash.php";
 | 
					require_once "lib/flexihash.php";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -114,7 +115,7 @@ class Image {
 | 
				
			|||||||
		assert(is_numeric($start));
 | 
							assert(is_numeric($start));
 | 
				
			||||||
		assert(is_numeric($limit));
 | 
							assert(is_numeric($limit));
 | 
				
			||||||
		assert(is_array($tags));
 | 
							assert(is_array($tags));
 | 
				
			||||||
		global $database, $user;
 | 
							global $database, $user, $order_sql;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$images = array();
 | 
							$images = array();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -128,13 +129,15 @@ class Image {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$querylet = Image::build_search_querylet($tags);
 | 
							$querylet = Image::build_search_querylet($tags);
 | 
				
			||||||
		$querylet->append(new Querylet("ORDER BY images.id DESC LIMIT :limit OFFSET :offset", array("limit"=>$limit, "offset"=>$start)));
 | 
							$querylet->append(new Querylet($order_sql ?: " ORDER BY images.id DESC"));
 | 
				
			||||||
 | 
							$querylet->append(new Querylet(" LIMIT :limit OFFSET :offset", array("limit"=>$limit, "offset"=>$start)));
 | 
				
			||||||
		#var_dump($querylet->sql); var_dump($querylet->variables);
 | 
							#var_dump($querylet->sql); var_dump($querylet->variables);
 | 
				
			||||||
		$result = $database->execute($querylet->sql, $querylet->variables);
 | 
							$result = $database->execute($querylet->sql, $querylet->variables);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		while($row = $result->fetch()) {
 | 
							while($row = $result->fetch()) {
 | 
				
			||||||
			$images[] = new Image($row);
 | 
								$images[] = new Image($row);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							$order_sql = null;
 | 
				
			||||||
		return $images;
 | 
							return $images;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -663,8 +666,6 @@ class Image {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$terms = Tag::resolve_aliases($terms);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// parse the words that are searched for into
 | 
							// parse the words that are searched for into
 | 
				
			||||||
		// various types of querylet
 | 
							// various types of querylet
 | 
				
			||||||
		foreach($terms as $term) {
 | 
							foreach($terms as $term) {
 | 
				
			||||||
@ -677,6 +678,15 @@ class Image {
 | 
				
			|||||||
				continue;
 | 
									continue;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								$aliases = explode(" ", Tag::resolve_alias($term));
 | 
				
			||||||
 | 
								$found = array_search($term, $aliases);
 | 
				
			||||||
 | 
								if($found !== false){
 | 
				
			||||||
 | 
									unset($aliases[$found]);
 | 
				
			||||||
 | 
								}else{
 | 
				
			||||||
 | 
									$term = array_shift($aliases);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								foreach($aliases as $alias)	array_push($terms, $alias);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			$stpe = new SearchTermParseEvent($term, $terms);
 | 
								$stpe = new SearchTermParseEvent($term, $terms);
 | 
				
			||||||
			send_event($stpe);
 | 
								send_event($stpe);
 | 
				
			||||||
			if($stpe->is_querylet_set()) {
 | 
								if($stpe->is_querylet_set()) {
 | 
				
			||||||
@ -824,8 +834,6 @@ class Image {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$terms = Tag::resolve_aliases($terms);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		reset($terms); // rewind to first element in array.
 | 
							reset($terms); // rewind to first element in array.
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		// turn each term into a specific type of querylet
 | 
							// turn each term into a specific type of querylet
 | 
				
			||||||
@ -836,6 +844,15 @@ class Image {
 | 
				
			|||||||
				$term = substr($term, 1);
 | 
									$term = substr($term, 1);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
 | 
								$aliases = explode(" ", Tag::resolve_alias($term));
 | 
				
			||||||
 | 
								$found = array_search($term, $aliases);
 | 
				
			||||||
 | 
								if($found !== false){
 | 
				
			||||||
 | 
									unset($aliases[$found]);
 | 
				
			||||||
 | 
								}else{
 | 
				
			||||||
 | 
									$term = array_shift($aliases);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								foreach($aliases as $alias)	array_push($terms, $alias);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			$stpe = new SearchTermParseEvent($term, $terms);
 | 
								$stpe = new SearchTermParseEvent($term, $terms);
 | 
				
			||||||
			send_event($stpe);
 | 
								send_event($stpe);
 | 
				
			||||||
			if($stpe->is_querylet_set()) {
 | 
								if($stpe->is_querylet_set()) {
 | 
				
			||||||
@ -1082,23 +1099,12 @@ class Tag {
 | 
				
			|||||||
		assert(is_array($tags));
 | 
							assert(is_array($tags));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$new = array();
 | 
							$new = array();
 | 
				
			||||||
 | 
							foreach($tags as $tag) {
 | 
				
			||||||
		$i = 0;
 | 
								$new_set = explode(' ', Tag::resolve_alias($tag));
 | 
				
			||||||
		$tag_count = count($tags);
 | 
								foreach($new_set as $new_one) {
 | 
				
			||||||
		while($i<$tag_count) {
 | 
									$new[] = $new_one;
 | 
				
			||||||
			$aliases = explode(' ', Tag::resolve_alias($tags[$i]));
 | 
					 | 
				
			||||||
			foreach($aliases as $alias){
 | 
					 | 
				
			||||||
				if(!in_array($alias, $new)){
 | 
					 | 
				
			||||||
					if($tags[$i] == $alias){
 | 
					 | 
				
			||||||
						$new[] = $alias;
 | 
					 | 
				
			||||||
					}elseif(!in_array($alias, $tags)){
 | 
					 | 
				
			||||||
						$tags[] = $alias;
 | 
					 | 
				
			||||||
						$tag_count++;
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			$i++;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$new = array_iunique($new); // remove any duplicate tags
 | 
							$new = array_iunique($new); // remove any duplicate tags
 | 
				
			||||||
		return $new;
 | 
							return $new;
 | 
				
			||||||
 | 
				
			|||||||
@ -87,6 +87,11 @@
 | 
				
			|||||||
 *      <ul>
 | 
					 *      <ul>
 | 
				
			||||||
 *        <li>source=http://example.com -- find all images with "http://example.com" in the source
 | 
					 *        <li>source=http://example.com -- find all images with "http://example.com" in the source
 | 
				
			||||||
 *      </ul>
 | 
					 *      </ul>
 | 
				
			||||||
 | 
					 *    <li>order (id, width, height, filesize, filename)_(ASC, DESC), eg
 | 
				
			||||||
 | 
					 *      <ul>
 | 
				
			||||||
 | 
					 *        <li>order=width -- find all images sorted from highest > lowest width
 | 
				
			||||||
 | 
					 *        <li>order=filesize_asc -- find all images sorted from lowest > highest filesize
 | 
				
			||||||
 | 
					 *      </ul>
 | 
				
			||||||
 *  </ul>
 | 
					 *  </ul>
 | 
				
			||||||
 *  <p>Search items can be combined to search for images which match both,
 | 
					 *  <p>Search items can be combined to search for images which match both,
 | 
				
			||||||
 *  or you can stick "-" in front of an item to search for things that don't
 | 
					 *  or you can stick "-" in front of an item to search for things that don't
 | 
				
			||||||
@ -320,6 +325,14 @@ class Index extends Extension {
 | 
				
			|||||||
			$cmp = ltrim($matches[1], ":") ?: "=";
 | 
								$cmp = ltrim($matches[1], ":") ?: "=";
 | 
				
			||||||
			$event->add_querylet(new Querylet("height $cmp :height{$this->stpen}",array("height{$this->stpen}"=>int_escape($matches[2]))));
 | 
								$event->add_querylet(new Querylet("height $cmp :height{$this->stpen}",array("height{$this->stpen}"=>int_escape($matches[2]))));
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							else if(preg_match("/^order[=|:](id|width|height|filesize|filename)[_]?(desc|asc)?$/i", $event->term, $matches)){
 | 
				
			||||||
 | 
								global $order_sql;
 | 
				
			||||||
 | 
								$order = strtolower($matches[1]);
 | 
				
			||||||
 | 
								$sort = isset($matches[2]) ? strtoupper($matches[2]) : (preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC");
 | 
				
			||||||
 | 
								// $event->add_querylet(new Querylet("ORDER BY images.:order :sort", array("order" => $order, "sort" => $sort)));
 | 
				
			||||||
 | 
								$event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag
 | 
				
			||||||
 | 
								$order_sql = " ORDER BY images.$order $sort";
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$this->stpen++;
 | 
							$this->stpen++;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user