<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[GlotPress] [462] trunk: Order support in GP_Thing</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>462</dd>
<dt>Author</dt> <dd>nbachiyski</dd>
<dt>Date</dt> <dd>2010-04-07 10:12:52 +0000 (Wed, 07 Apr 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Order support in GP_Thing</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkgpincludesmiscphp">trunk/gp-includes/misc.php</a></li>
<li><a href="#trunkgpincludesthingphp">trunk/gp-includes/thing.php</a></li>
<li><a href="#trunkttest_thingphp">trunk/t/test_thing.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkgpincludesmiscphp"></a>
<div class="modfile"><h4>Modified: trunk/gp-includes/misc.php (461 => 462)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/gp-includes/misc.php        2010-04-07 10:12:22 UTC (rev 461)
+++ trunk/gp-includes/misc.php        2010-04-07 10:12:52 UTC (rev 462)
</span><span class="lines">@@ -35,11 +35,15 @@
</span><span class="cx">         return isset( $array[$key] )? $array[$key] : $default;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-
</del><span class="cx"> function gp_const_get( $name, $default = '' ) {
</span><span class="cx">         return defined( $name )? constant( $name ) : $default;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+function gp_member_get( $object, $key, $default = '' ) {
+        return isset( $object->$key )? $object->$key : $default;
+}
+
+
</ins><span class="cx"> /**
</span><span class="cx"> * Makes from an array of arrays a flat array.
</span><span class="cx"> *
</span></span></pre></div>
<a id="trunkgpincludesthingphp"></a>
<div class="modfile"><h4>Modified: trunk/gp-includes/thing.php (461 => 462)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/gp-includes/thing.php        2010-04-07 10:12:22 UTC (rev 461)
+++ trunk/gp-includes/thing.php        2010-04-07 10:12:52 UTC (rev 462)
</span><span class="lines">@@ -66,8 +66,8 @@
</span><span class="cx">         /**
</span><span class="cx">          * Retrieves all rows from this table
</span><span class="cx">          */
</span><del>-        function all() {
-                return $this->many( "SELECT * FROM $this->table" );
</del><ins>+        function all( $order = null ) {
+                return $this->many( $this->select_all_from_conditions_and_order( array(), $order ) );
</ins><span class="cx">         }
</span><span class="cx">         
</span><span class="cx">         /**
</span><span class="lines">@@ -124,16 +124,16 @@
</span><span class="cx">                 return $this->map( $gpdb->get_results( $this->prepare( $args ) ) );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function find_many( $conditions ) {
-                return $this->many( $this->select_all_from_conditions( $conditions ) );
</del><ins>+        function find_many( $conditions, $order = null ) {
+                return $this->many( $this->select_all_from_conditions_and_order( $conditions, $order ) );
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        function find_one( $conditions ) {
-                return $this->one( $this->select_all_from_conditions( $conditions ) );
</del><ins>+        function find_one( $conditions, $order = null ) {
+                return $this->one( $this->select_all_from_conditions_and_order( $conditions, $order ) );
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        function find( $conditions ) {
-                return $this->find_many( $conditions );
</del><ins>+        function find( $conditions, $order = null ) {
+                return $this->find_many( $conditions, $order );
</ins><span class="cx">         }
</span><span class="cx">         
</span><span class="cx">         function query() {
</span><span class="lines">@@ -323,10 +323,25 @@
</span><span class="cx">                 return $this->apply_default_conditions( $conditions );
</span><span class="cx">         }
</span><span class="cx">         
</span><del>-        function select_all_from_conditions( $conditions ) {
-                return "SELECT * FROM $this->table WHERE " . $this->sql_from_conditions( $conditions );
</del><ins>+        function sql_from_order( $order_by, $order_how = '' ) {                
+                if ( is_array( $order_by ) ) {
+                        $order_by = implode( ' ', $order_by );
+                        $order_how = '';
+                }
+                $order_by = trim( $order_by );
+                if ( !$order_by ) return gp_member_get( $this, 'default_order' );                
+                return 'ORDER BY ' . $order_by . ( $order_how? " $order_how" : '' );
</ins><span class="cx">         }
</span><span class="cx">         
</span><ins>+        function select_all_from_conditions_and_order( $conditions, $order = null ) {
+                $query = "SELECT * FROM $this->table";
+                $conditions_sql = $this->sql_from_conditions( $conditions );
+                if ( $conditions_sql ) $query .= " WHERE $conditions_sql";
+                $order_sql = $this->sql_from_order( $order );
+                if ( $order_sql ) $query .= " $order_sql";
+                return $query;
+        }
+        
</ins><span class="cx">         function restrict_fields( $thing ) {
</span><span class="cx">                 // Don't restrict any fields by default
</span><span class="cx">         }
</span></span></pre></div>
<a id="trunkttest_thingphp"></a>
<div class="modfile"><h4>Modified: trunk/t/test_thing.php (461 => 462)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/t/test_thing.php        2010-04-07 10:12:22 UTC (rev 461)
+++ trunk/t/test_thing.php        2010-04-07 10:12:52 UTC (rev 462)
</span><span class="lines">@@ -27,4 +27,18 @@
</span><span class="cx">                 $this->assertEquals( 'a = 5', $this->t->sql_from_conditions( array('a' => 5) ) );
</span><span class="cx">                 $this->assertEquals( "(a = 5 OR a = 6) AND b = 'baba'", $this->t->sql_from_conditions( array('a' => array(5, 6), 'b' => 'baba' ) ) );
</span><span class="cx">         }
</span><ins>+        
+        function test_sql_from_order() {
+                $this->assertEquals( '', $this->t->sql_from_order( null ) );
+                $this->assertEquals( '', $this->t->sql_from_order( '' ) );
+                $this->assertEquals( '', $this->t->sql_from_order( array() ) );
+                $this->assertEquals( '', $this->t->sql_from_order( array(), 'baba' ) );
+                $this->assertEquals( 'ORDER BY x', $this->t->sql_from_order( 'x' ) );
+                $this->assertEquals( 'ORDER BY table.field', $this->t->sql_from_order( 'table.field' ) );
+                $this->assertEquals( 'ORDER BY table.field ASC', $this->t->sql_from_order( 'table.field ASC' ) );
+                $this->assertEquals( 'ORDER BY table.field', $this->t->sql_from_order( array( 'table.field' ) ) );
+                $this->assertEquals( 'ORDER BY table.field ASC', $this->t->sql_from_order( 'table.field', 'ASC' ) );
+                $this->assertEquals( 'ORDER BY table.field ASC', $this->t->sql_from_order( array( 'table.field', 'ASC' ) ) );
+                $this->assertEquals( 'ORDER BY table.field ASC', $this->t->sql_from_order( array( 'table.field', 'ASC' ), 'baba' ) );
+        }
</ins><span class="cx"> }
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>
</body>
</html>