------------------------------------------------------------
revno: 1140
committer: Roger Martin <rg1...@gmail.com>
branch nick: aikiframework
timestamp: Tue 2012-03-13 23:46:15 +0100
message:
  implemented blueprint Using in-widget (sql()sql), need a way to define 'no 
results returned' 
modified:
  libs/SqlMarkup.php


--
lp:aikiframework
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk

Your team Aiki Framework Developers is subscribed to branch lp:aikiframework.
To unsubscribe from this branch go to 
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk/+edit-subscription
=== modified file 'libs/SqlMarkup.php'
--- libs/SqlMarkup.php	2012-03-02 21:15:25 +0000
+++ libs/SqlMarkup.php	2012-03-13 22:46:15 +0000
@@ -41,9 +41,14 @@
 	 *
 	 * @todo need a serious security review
 	 */
-	public function sql($text) {	       
-		return preg_replace_callback(   // i love this function :-o
-			'/\(sql\((.*)\)sql\)/Us', array($this,"sql_query"), $text); 
+	public function sql($text) {	
+		$pattern = '/\(sql\((.*)\)sql\)/s';
+		
+		// in each step replace the outer (sql(..)sql)
+		while ( preg_match( $pattern, $text) ){
+			$text= preg_replace_callback($pattern, array($this,"sql_query"), $text);
+	    }
+	    return $text;		 
 	}
 
 	/**
@@ -55,69 +60,62 @@
 	public function sql_query($matchRaw) {
 		global $aiki, $db;
 
-		$match = $aiki->url->apply_url_on_query($matchRaw[1]);
-
-		$html_output = '';
-		$count_sql = preg_match_all('/\((.*)| (?R)\)/s', $match, $matches);
-
-		$match = '';
-
-		foreach ( $matches[1] as $sql ) {
-
-			$sql_html = explode("||", trim($sql));
-
-			$sql_html[1] = str_replace($sql_html[0]."||", '', $sql);
-
-			$sql_query = $sql_html[0];
-
+		
+		if ( is_array($matchRaw) ){
+			// called by preg_replace_callback
+			$check = $aiki->url->apply_url_on_query($matchRaw[1]);			
+		} else {
+			$check= $matchRaw;
+		}
+						
+		if ( !preg_match_all('/\((((?>[^()]+)|(?R))*)\)/', $check, $matches) ){
+			return $check;		
+		}
+		
+		
+		$output="";		
+		$if_no_results=false;
+		foreach ( $matches[1] as $match ){			
+		    if ( substr_count($match,"||")==0 ){
+				continue;
+			} else {				
+				list($sql_query,$sql_html) = explode("||", $match, 2);
+			}
+			
+			if ( strtoupper(trim($sql_query))=="IF_NO_RESULTS"){
+				if ( $if_no_results ){
+					$output .= $sql_html;
+				}				
+				continue;
+			}
+				
 			if ($sql_query) {
 				$sql_query = str_replace("\'", "'", $sql_query);
 				$sql_query = str_replace('\"', '"', $sql_query);
 
 				$db->last_error= false;
-				$results = $db->get_results($sql_query);
+				
+				$results = $db->get_results($sql_query, ARRAY_A);
 				if ( $db->last_error ){
 					return t("Error in SQL").
 						(is_debug_on() ? " ". $db->last_error : "" );
 				}
-				
+					
 				if ($results) {
+					$if_no_results=false;
 					foreach ( $results as $result ) {
-						$html = trim($sql_html[1]);
-						if (!preg_match('/\(select(.*)/', $html)) {
-							$html = substr($html,0,-1);
-						}
-						//ez_SQL return array or Object not assoc
-						$result = get_object_vars($result);
+						$html = trim($sql_html);
 						foreach ($result as $k => $v) {
 							$html = str_replace("[-[".$k."]-]", $v,  $html);
 						}
-						/* just in case
-						$result = $aiki->AikiArray->object2array($result);
-						$result_key = @array_flip($result);
-
-						foreach ( $result as $field ) {
-							if (isset($result_key[$field])) {
-								$html = str_replace("[-[".$result_key[$field].
-											"]-]", $field,  $html);
-							}
-						}
-						*/
-						$match .= $html;
-						
-						/**
-						 * @todo investigate why these are hidden, kill or keep
-						 */
-						//$match = str_replace("\r", ' ', $match);
-						//$match = str_replace("\n", ' ', $match);
-						$match = preg_replace("/\)\s\s+\(/", '(', $match);
-
-						$match .= $this->sql_query($html);
+						$output .= $html;																		
 					} // loop over results
-				} // if results
-			} // if query
-		} // loop over matches
-		return $match;
+				} else {
+					$if_no_results=true;
+				}// if results
+			} // if query		
+		} // loop
+		return $output;
 	} // end of function
 } // end of class
 

_______________________________________________
Mailing list: https://launchpad.net/~aikiframework-devel
Post to     : aikiframework-devel@lists.launchpad.net
Unsubscribe : https://launchpad.net/~aikiframework-devel
More help   : https://help.launchpad.net/ListHelp

Reply via email to