<!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-&gt;$key )? $object-&gt;$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-&gt;many( &quot;SELECT * FROM $this-&gt;table&quot; );
</del><ins>+        function all( $order = null ) {
+                return $this-&gt;many( $this-&gt;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-&gt;map( $gpdb-&gt;get_results( $this-&gt;prepare( $args ) ) );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function find_many( $conditions ) {
-                return $this-&gt;many( $this-&gt;select_all_from_conditions( $conditions ) );
</del><ins>+        function find_many( $conditions, $order = null ) {
+                return $this-&gt;many( $this-&gt;select_all_from_conditions_and_order( $conditions, $order ) );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function find_one( $conditions ) {
-                return $this-&gt;one( $this-&gt;select_all_from_conditions( $conditions ) );
</del><ins>+        function find_one( $conditions, $order = null ) {
+                return $this-&gt;one( $this-&gt;select_all_from_conditions_and_order( $conditions, $order ) );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function find( $conditions ) {
-                return $this-&gt;find_many( $conditions );
</del><ins>+        function find( $conditions, $order = null ) {
+                return $this-&gt;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-&gt;apply_default_conditions( $conditions );
</span><span class="cx">         }
</span><span class="cx">         
</span><del>-        function select_all_from_conditions( $conditions ) {
-                return &quot;SELECT * FROM $this-&gt;table WHERE &quot; . $this-&gt;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? &quot; $order_how&quot; : '' );
</ins><span class="cx">         }
</span><span class="cx">         
</span><ins>+        function select_all_from_conditions_and_order( $conditions, $order = null ) {
+                $query = &quot;SELECT * FROM $this-&gt;table&quot;;
+                $conditions_sql = $this-&gt;sql_from_conditions( $conditions );
+                if ( $conditions_sql ) $query .= &quot; WHERE $conditions_sql&quot;;
+                $order_sql = $this-&gt;sql_from_order( $order );
+                if ( $order_sql ) $query .= &quot; $order_sql&quot;;
+                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-&gt;assertEquals( 'a = 5', $this-&gt;t-&gt;sql_from_conditions( array('a' =&gt; 5) ) );
</span><span class="cx">                 $this-&gt;assertEquals( &quot;(a = 5 OR a = 6) AND b = 'baba'&quot;, $this-&gt;t-&gt;sql_from_conditions( array('a' =&gt; array(5, 6), 'b' =&gt; 'baba' ) ) );
</span><span class="cx">         }
</span><ins>+        
+        function test_sql_from_order() {
+                $this-&gt;assertEquals( '', $this-&gt;t-&gt;sql_from_order( null ) );
+                $this-&gt;assertEquals( '', $this-&gt;t-&gt;sql_from_order( '' ) );
+                $this-&gt;assertEquals( '', $this-&gt;t-&gt;sql_from_order( array() ) );
+                $this-&gt;assertEquals( '', $this-&gt;t-&gt;sql_from_order( array(), 'baba' ) );
+                $this-&gt;assertEquals( 'ORDER BY x', $this-&gt;t-&gt;sql_from_order( 'x' ) );
+                $this-&gt;assertEquals( 'ORDER BY table.field', $this-&gt;t-&gt;sql_from_order( 'table.field' ) );
+                $this-&gt;assertEquals( 'ORDER BY table.field ASC', $this-&gt;t-&gt;sql_from_order( 'table.field ASC' ) );
+                $this-&gt;assertEquals( 'ORDER BY table.field', $this-&gt;t-&gt;sql_from_order( array( 'table.field' ) ) );
+                $this-&gt;assertEquals( 'ORDER BY table.field ASC', $this-&gt;t-&gt;sql_from_order( 'table.field', 'ASC' ) );
+                $this-&gt;assertEquals( 'ORDER BY table.field ASC', $this-&gt;t-&gt;sql_from_order( array( 'table.field', 'ASC' ) ) );
+                $this-&gt;assertEquals( 'ORDER BY table.field ASC', $this-&gt;t-&gt;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>