commit 078fb00efb76cda6a90a79357bad4d27d2f01cfb
Author: Botea Razvan <utherr.ghujax@gmail.com>
Date:   Sat Mar 19 12:25:28 2011 +0200

    Implemented Random Word; added support for multi-result, multi-column result queries.

diff --git a/phplib/db.php b/phplib/db.php
index f46b160..4cd2a42 100644
--- a/phplib/db.php
+++ b/phplib/db.php
@@ -8,7 +8,7 @@ function db_init() {
   ADOdb_Active_Record::SetDatabaseAdapter($db);
   $db->Execute('set names utf8');
   $GLOBALS['db'] = $db;
-  // $db->debug = true; //just for debug
+  //$db->debug = true; //just for debug
 }
 
 function db_execute($query) {
@@ -67,6 +67,23 @@ function db_getArray($recordSet) {
   return $result;
 }
 
+/**
+ * Executes a query and returns a multidimensional array, much like mysql_fetch_assoc, because AdoDB has a horrible method to return an assoc array.
+ * @uses Never use this function with a query that returns only ONE column. Use db_fetchSingleRow instead.
+ * @param string $query The query to be executed.
+ * @return returns a multidimensional array of the form $result[index]['collumn'] or false if there's no result.
+ */
+
+function db_getArrayAssoc($query) {
+  
+  $query_result = $GLOBALS['db']->GetAssoc($query);
+  $result = array();
+  foreach ($query_result as $array_index ) 
+    $result[] = $array_index;
+  
+  return count($result) ? $result : FALSE;
+}
+
 function db_getObjects($obj, $dbResult) {
   $class = get_class($obj);
   $result = array();
diff --git a/templates/zepu/index.ihtml b/templates/zepu/index.ihtml
index c1281eb..3855615 100644
--- a/templates/zepu/index.ihtml
+++ b/templates/zepu/index.ihtml
@@ -7,6 +7,8 @@
   {include file="zepu/search.ihtml" advancedSearch=1}
 </div>
 
+<div class="random_word"> </div>
+
 <div class="clearer"></div>
 
 <div class="statusReport">
diff --git a/templates/zepu/pageLayout.ihtml b/templates/zepu/pageLayout.ihtml
index 87b6f75..f5c713c 100644
--- a/templates/zepu/pageLayout.ihtml
+++ b/templates/zepu/pageLayout.ihtml
@@ -3,7 +3,7 @@
   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     {include file="common/bits/css.ihtml" zepu=1}
-    {include file="common/bits/js.ihtml" dex=1}
+    {include file="common/bits/js.ihtml" dex=1 jquery=1}
     <title>{$page_title|escape} | DEX online</title>
     {if $page_description}
       <meta name="description" content="{$page_description}"/>
@@ -11,7 +11,7 @@
     <link rel="search" type="application/opensearchdescription+xml" href="http://dexonline.ro/download/dex.xml" title="Căutare dexonline.ro"/>
   </head>
 
-  <body>
+  <body onload="$('.random_word').hide()">
     <div id="header">
       <div id="headerContainer">
         {include file="common/bits/bannerH.ihtml"}
diff --git a/templates/zepu/search.ihtml b/templates/zepu/search.ihtml
index 60c4579..bc04652 100644
--- a/templates/zepu/search.ihtml
+++ b/templates/zepu/search.ihtml
@@ -1,6 +1,7 @@
 <form action="{$wwwRoot}search.php" name="frm" onsubmit="return friendlyRedirect()" id="searchForm">
   <input type="text" name="cuv" class="search_field" value="{$cuv|escape}"  maxlength="50" title="Caută"/>
   <input type="image" value="Search" class="search_button" src="{$imgRoot}/zepu/cauta.png" alt="Caută"/>
+  <input type="button" value="I'm feeling lucky" style="position:relative; left:180px" onclick="loadRandomWord()" />
   {if !$advancedSearch}
     <a href="#" onclick="return toggleDivVisibility('advSearch')">Căutare avansată</a>
   {/if}
diff --git a/wwwbase/ajax/random_word.php b/wwwbase/ajax/random_word.php
new file mode 100644
index 0000000..d320fe9
--- /dev/null
+++ b/wwwbase/ajax/random_word.php
@@ -0,0 +1,29 @@
+<?php
+require_once("../../phplib/util.php");
+require_once("../../phplib/db.php");
+
+$IsEmpty=true;
+$responseText = "<span class=\"def\" onclick=\"return searchClickedWord(event);\" title=\"Clic pentru a naviga la acest cuvânt\">";
+
+while($IsEmpty) {
+  // Because there are "gaps" in the IDs field I cannot know for sure if the
+  // random number ID exist and must repeat untill I find it
+  $max_words = db_getSingleValue("SELECT COUNT(*) as words FROM `Lexem`");
+  
+  $random_w = rand(0,$max_words);
+  $Lexem = db_getArrayAssoc("SELECT * FROM `LexemDefinitionMap` WHERE `lexemId`='$random_w'");
+  
+  if($Lexem) {
+    $IsEmpty = false;
+    for($i=0; $i < count($Lexem); $i++) {
+      $LexemDef = db_getArrayAssoc("SELECT * FROM `Definition` WHERE `id` = '".$Lexem[$i]['definitionId']."'");
+      if(!$LexemDef)  //Unfortunately there are "dead" definitions in LexemDefinitionMap
+        continue;
+      $responseText .= "<br />".$LexemDef[0]['htmlRep']."<br />";
+    }
+  }
+}
+$responseText .= "</span>";
+echo $responseText;
+
+?>
\ No newline at end of file
diff --git a/wwwbase/js/dex.js b/wwwbase/js/dex.js
index c530b0d..fc25e24 100644
--- a/wwwbase/js/dex.js
+++ b/wwwbase/js/dex.js
@@ -348,6 +348,14 @@ function isValidWord(word) {
 	return false;
 }
 
+function loadRandomWord() {
+	$('.random_word').load(getWwwRoot()+"ajax/random_word.php","",
+			function() {
+				$('.random_word').slideDown(500);
+			}
+		);
+}
+
 function searchClickedWord(evt) {
   var st = getSelectedText();
   if (st && st != '') {
diff --git a/wwwbase/styles/zepu.css b/wwwbase/styles/zepu.css
index 94a413a..5b37718 100644
--- a/wwwbase/styles/zepu.css
+++ b/wwwbase/styles/zepu.css
@@ -324,6 +324,13 @@ div.noParadigm {
   white-space: nowrap;
 }
 
+div.random_word {
+  margin: 0 auto;
+  text-align: justify;
+  position: relative;
+  width: 60%;
+}
+
 span.isLoc {
   color: green;
   cursor: help;
@@ -542,7 +549,7 @@ span.spaced {
 }
 
 table.static {
-  margin-left: 20px;
+  margin: 0 auto;
 }
 
 /* jquery tablesorter stuff */
