1<?php
2require_once("classes/DBUtils.class.php");
3
4
5$course_id = (isset($_REQUEST['course_id']) && isValidID($_REQUEST['course_id'])) ?  $_REQUEST['course_id'] : 0;
6$base_id = (isset($_REQUEST['base_id']) && isValidID($_REQUEST['base_id'])) ? $_REQUEST['base_id'] : 0;
7$ethnic_id = (isset($_REQUEST['ethnic_id']) && isValidID($_REQUEST['ethnic_id'])) ?  $_REQUEST['ethnic_id'] : 0;
8$time_id = (isset($_REQUEST['time_id']) && isValidID($_REQUEST['time_id'])) ? $_REQUEST['time_id'] : 0;
9$difficult_id = (isset($_REQUEST['difficult_id']) && isValidID($_REQUEST['difficult_id'])) ? $_REQUEST['difficult_id'] : 0;
10$cost = isset($_REQUEST['cost']) && is_numeric($_REQUEST['cost']) ? $_REQUEST['cost'] : 0.00;
11$owner = isset($_REQUEST['owner']) ? $_REQUEST['owner'] : '';
12$keywords = isset($_REQUEST['keywords']) ? $_REQUEST['keywords'] : '';
13
14if (isset($_REQUEST['cost_compare'])) {
15    $tmp = $_REQUEST['cost_compare'];
16    if (!(
17        $tmp == ">" ||
18        $tmp == ">=" ||
19        $tmp == "=" ||
20        $tmp == "<" ||
21        $tmp == "<="
22        ))
23    $tmp = null;
24    $_REQUEST['cost_compare'] = $tmp;
25}
26
27?>
28<script language="JavaScript">
29<!--
30	function checkAll(val)
31	{
32		var len = document.searchForm.elements.length;
33		var j=0
34		var i=0;
35		for( i=0 ; i<len ; i++) {
36			var id = 'recipe_selected_' + j;
37			if (document.searchForm.elements[i].name == id) {
38				document.searchForm.elements[i].checked = val;
39				j++;
40			}
41		}
42	}
43
44	function confirmDelete()
45	{
46		return confirm("<?php echo $LangUI->_('Are you sure you wish to delete this recipe?');?>");
47	}
48
49	function submitForm(val)
50	{
51		if(val == "list")
52		{
53			document.searchForm.action="index.php?m=lists&a=current";
54			document.searchForm.submit();
55        }
56		else if (val == "delete")
57		{
58			if (confirmDelete())
59			{
60				document.searchForm.action="index.php?dosql=delete&m=recipes&a=search";
61				document.searchForm.submit();
62			}
63		}
64    }
65// -->
66</SCRIPT>
67</script>
68
69<table cellspacing="0" cellpadding="1" border="0" width="100%">
70<tr>
71	<td align="left" class="title"><?php echo $LangUI->_('Search Recipes');?></td>
72</tr>
73</table>
74
75<p><table cellspacing="1" cellpadding="2" border="0" class="data" width="95%">
76<form name="inputForm" action="index.php?m=recipes&a=search<?php if (isset($_REQUEST['advanced'])) echo "&advanced=yes"?>" method="post">
77<input type=hidden name="search" value="yes">
78<tr>
79	<th><?php echo $LangUI->_('Course');?></th>
80	<th><?php echo $LangUI->_('Base');?></th>
81	<th><?php echo $LangUI->_('Ethnicity');?></th>
82	<th><?php echo $LangUI->_('Preparation Time');?></th>
83<?php if (isset($_REQUEST['advanced'])) {?>
84	<th><?php echo $LangUI->_('Difficulty');?></th>
85	<th colspan="2"><?php echo $LangUI->_('Approximate Cost');?></th>
86	<th><?php echo $LangUI->_('Owner');?></th>
87<?php } ?>
88	<th><?php echo $LangUI->_('Keywords');?></th>
89</tr>
90<tr>
91	<td>
92<?php
93	// Need this later on for the user owner display
94	$db_users = DBUtils::fetchColumn( $db_table_users, 'user_name', 'user_login', 'user_name' );
95
96	// Now back to the normal display
97	$rc = DBUtils::fetchColumn( $db_table_courses, 'course_desc', 'course_id', 'course_desc' );
98	echo $rc->getMenu2('course_id', $course_id, true);
99?>
100	</td>
101	<td>
102<?php
103	$rc = DBUtils::fetchColumn( $db_table_bases, 'base_desc', 'base_id', 'base_desc' );
104	echo $rc->getMenu2('base_id', $base_id, true);
105?>
106	</td>
107	<td>
108<?php
109	$rc = DBUtils::fetchColumn( $db_table_ethnicity, 'ethnic_desc', 'ethnic_id', 'ethnic_desc' );
110	echo $rc->getMenu2('ethnic_id', $ethnic_id, true);
111?>
112	</td>
113	<td>
114<?php
115	$rc = DBUtils::fetchColumn( $db_table_prep_time, 'time_desc', 'time_id', 'time_desc' );
116	echo $rc->getMenu2('time_id', $time_id, true);
117?>
118	</td>
119<?php
120// Need to simplify the search bar, so just display this extra stuff if
121//	in advanced mode.
122if (isset($_REQUEST['advanced'])) {
123	echo "<td>\n";
124	$rc = DBUtils::fetchColumn( $db_table_difficulty, 'difficult_desc', 'difficult_id', 'difficult_desc' );
125	echo $rc->getMenu2('difficult_id', $difficult_id, true);
126	echo "</td>\n<td>\n";
127	// Workaround to put the values in the dropdown, it would be good to use adodb?
128	$arr = array(
129			">" => ">",
130			">=" => ">=",
131			"=" => "=",
132			"<" => "<",
133			"<=" => "<="
134			);
135	echo DBUtils::arraySelect( $arr, 'cost_compare', 'size=1', isset($_REQUEST["cost_compare"]) ? $_REQUEST["cost_compare"] : '');
136?>
137	</td>
138	<td>
139		<input type="text" size="5" name="cost" class="field_textbox" value="<?php echo $cost;?>">
140	</td>
141	<td>
142	<?php
143	$temp_users = $db_users;
144	echo $temp_users->getMenu2('owner', $owner, true );
145	echo "</td>";
146	// End of Advanced Section
147}
148
149// convert the users to an array
150$users = array();
151while (!$db_users->EOF) {
152	$j = $db_users->fields['user_login'];
153	$users[$j] = $db_users->fields['user_name'];
154	$db_users->MoveNext();
155}
156
157?>
158	<td><input type="text" name="keywords" class="field_textbox" value="<?php echo $keywords;?>">
159</tr>
160<tr>
161	<td align="center" colspan="9">
162		<input type="submit" value="<?php echo $LangUI->_('Search');?>" class="button" style="width:75px">&nbsp;
163		<input type="reset" value="<?php echo $LangUI->_('Clear');?>" class="button" style="width:75px">
164		<?php if (!isset($_REQUEST['advanced'])) {?>
165		&nbsp;&nbsp;&nbsp;&nbsp;<i>(<a href="./index.php?m=recipes&amp;a=search&amp;advanced=yes"><?php echo $LangUI->_('Advanced Search');?></a>)</i>
166		<?php } else {?>
167		&nbsp;&nbsp;&nbsp;&nbsp;<i>(<a href="./index.php?m=recipes&amp;a=search"><?php echo $LangUI->_('Simple Search');?></a>)</i>
168		<?php } ?>
169	</td>
170</tr>
171</tr>
172</form>
173</table>
174<script language="JavaScript">
175	document.inputForm.keywords.focus();
176</script>
177<hr size=1 noshade>
178<?php
179	// Query to get the rating of a recipe (may be disabled)
180	$rating_query = "SELECT rating_recipe, avg(rating_score) FROM $db_table_ratings GROUP BY rating_recipe";
181	// Construct the Query to search for recipes
182	$query="";
183	$query_order = " ORDER BY recipe_name";
184	$query_all="SELECT
185					recipe_id,
186					recipe_name,
187					recipe_comments,
188					recipe_private,
189					recipe_owner,
190					recipe_serving_size,
191					user_name
192				FROM $db_table_recipes
193				LEFT JOIN $db_table_users ON user_login = recipe_owner";
194	// Do not display anything if no search has been requested
195	if (!isset($_REQUEST["search"])) {
196		$query = "";
197	} else if (
198		   !$course_id && !$base_id &&
199		   !$ethnic_id && !$time_id  && !$cost &&
200		   !$owner && !$difficult_id && $keywords=="") {
201		// Nothing special specied, just spit it all out
202		$query = $query_all . $query_order;
203	} else {
204		#Construct the Query
205		$query = $query_all . " WHERE ";
206		if ($course_id) $query .= " recipe_course=" . $DB_LINK->addq($course_id, get_magic_quotes_gpc()) . " AND";
207		if ($base_id) $query .= " recipe_base=" . $DB_LINK->addq($base_id, get_magic_quotes_gpc()) . " AND";
208		if ($ethnic_id) $query .= " recipe_ethnic=" . $DB_LINK->addq($ethnic_id, get_magic_quotes_gpc()) . " AND";
209		if ($time_id) $query .= " recipe_prep_time=" . $DB_LINK->addq($time_id, get_magic_quotes_gpc()) . " AND";
210		if ($difficult_id) $query .= " recipe_difficulty=" . $DB_LINK->addq($difficult_id, get_magic_quotes_gpc())  . " AND";
211		if ($cost) $query .= " recipe_cost " . $DB_LINK->addq($_REQUEST["cost_compare"], get_magic_quotes_gpc()) . " " . $DB_LINK->addq(htmlentities($cost, ENT_QUOTES), get_magic_quotes_gpc()) . " AND";
212		if ($owner) $query .= " recipe_owner='" . $DB_LINK->addq($owner, get_magic_quotes_gpc()) . "' AND";
213		if ($keywords != "") {
214			$query .= " recipe_name LIKE '%". $DB_LINK->addq(htmlentities($keywords, ENT_QUOTES), get_magic_quotes_gpc()) . "%' OR ";
215			$query .= " recipe_directions LIKE '%".$DB_LINK->addq(htmlentities($keywords, ENT_QUOTES), get_magic_quotes_gpc()) . "%' OR ";
216			$query .= " recipe_source LIKE '%". $DB_LINK->addq(htmlentities($keywords, ENT_QUOTES), get_magic_quotes_gpc()) . "%' OR ";
217			$query .= " recipe_comments LIKE '%". $DB_LINK->addq(htmlentities($keywords, ENT_QUOTES), get_magic_quotes_gpc()) . "%'";
218		}
219		$query = preg_replace("/AND$/", "", $query);
220		$query .= $query_order;
221	}
222
223	/* ----------------------
224		The Query has been made, format and output the values returned from the database
225	----------------------*/
226	if ($query != "") {
227		$counter=0;
228		$recipes = $DB_LINK->Execute($query);
229		DBUtils::checkResult($recipes, NULL, NULL, $query);
230		$rc = $DB_LINK->Execute($rating_query);
231		DBUtils::checkResult($rc, NULL, NULL, $rating_query);
232		// The field name for an avg is different between databases, have to use numeric return position
233		$ratings = DBUtils::createList($rc, 0, 1);
234		# exit if we did not find any matches
235		if ($recipes->RecordCount() == 0)
236		{
237			echo $LangUI->_('No values returned from search') . "<br>";
238		}
239		else
240		{
241?>
242		<table cellspacing="1" cellpadding="2" border=0 width="95%" class="data">
243		<form name="searchForm" action="" method="post">
244		<input type="hidden" name="mode" value="add">
245		<tr valign="top">
246			<td colspan=6 align=left>
247				<input type="button" value="<?php echo $LangUI->_('Add to shopping list');?>" class="button" onClick='submitForm("list")'>&nbsp;
248				<?php if ($SMObj->checkAccessLevel("AUTHOR")) { ?>
249				<input type="button" value="<?php echo $LangUI->_('Delete Selected');?>" class="button" onClick='submitForm("delete")'>&nbsp;&nbsp;
250				<?php } ?>
251				<a href="javascript:checkAll(1)"><?php echo $LangUI->_('Check All');?></a> -
252				<a href="javascript:checkAll(0)"><?php echo $LangUI->_('Clear All');?></a>
253			</td>
254		</tr>
255		<tr>
256			<th width="10">+</th>
257			<th><?php echo $LangUI->_('Dish Name');?></th>
258			<?php if ($g_rb_enable_ratings) echo '<th>' . $LangUI->_('Rating') . '</th>';?>
259			<th align=center><?php echo $LangUI->_('Options');?></th>
260			<th><?php echo $LangUI->_('Comments');?></th>
261		</tr>
262<?php while (!$recipes->EOF) {
263		$recipe_id = $recipes->fields['recipe_id'];
264		/*
265			If this is a private recipe and the user does not have access to it, then skip it
266		*/
267		if (($recipes->fields['recipe_private'] == $DB_LINK->true) &&
268			(!$SMObj->getUserLoginID() ||
269			 (!$SMObj->checkAccessLevel("EDITOR") &&
270			 $SMObj->getUserLoginID() != $recipes->fields['recipe_owner'] &&
271			 !$SMObj->hasGroupsWith($recipes->fields['recipe_owner'])))) {
272				 $recipes->MoveNext();
273				 continue;
274		}
275?>
276		<tr>
277			<td width="10">
278				<input type="checkbox" name="recipe_selected_<?php echo $counter;?>" value="yes" class="field_checkbox">
279				<input type="hidden" name="recipe_id_<?php echo $counter;?>" value="<?php echo $recipe_id; ?>">
280				<input type="hidden" name="recipe_scale_<?php echo $counter;?>" value="<?php echo $recipes->fields['recipe_serving_size'];?>">
281			</td>
282			<td>
283				<a href="./index.php?m=recipes&amp;a=view&amp;recipe_id=<?php echo $recipes->fields['recipe_id'];?>">
284					<?php echo $recipes->fields['recipe_name'];?></a><br />
285					<?php
286						echo $LangUI->_('Submitted by') . ' ' .trim($recipes->fields['user_name']);
287					?>
288				</td>
289				<?php
290				if ($g_rb_enable_ratings) {
291					echo '<td align="center">';
292					// Print out the ratings Information (if it is enabled)
293					$avg = 0;
294
295					if (isset($ratings[$recipe_id]))
296					{
297						$avg = $ratings[$recipe_id] + 0; // cheap way of removing the 0's
298					}
299
300					if ($avg == 0)
301					{
302						echo $LangUI->_('Not Rated');
303					}
304					else
305					{
306						$num_stars = 0;
307						// give full stars
308						while ($avg >= 1)
309						{
310							echo '<img src="themes/' . $g_rb_theme . '/images/filled_star.gif" border="0">';
311							$avg--;
312							$num_stars++;
313						}
314
315						// award a half star of greater then .65
316						if ($avg >= 0.65)
317						{
318							echo '<img src="themes/' . $g_rb_theme . '/images/filled_star.gif" border="0">';
319							$num_stars++;
320						}
321
322						// print out the rest of them as empty stars
323						while ($num_stars < 5)
324						{
325							echo '<img src="themes/' . $g_rb_theme . '/images/empty_star.gif" border="0">';
326							$num_stars++;
327						}
328					}
329					echo '</td>';
330				}
331				?>
332			<td align="center">
333				<a href="./index.php?m=recipes&amp;a=addedit&amp;recipe_id=<?php echo $recipe_id . "\">" . $LangUI->_('Edit');?></a>
334			</td>
335			<td>
336				<?php echo $recipes->fields['recipe_comments'];?>
337			</td>
338		</tr>
339<?php
340			$recipes->MoveNext();
341			$counter++;
342		}
343?>
344		<tr>
345			<td colspan="6" align="left">
346				<input type="button" value="<?php echo $LangUI->_('Add to shopping list');?>" class="button" onClick='submitForm("list")'>&nbsp;
347				<?php if ($SMObj->checkAccessLevel("AUTHOR")) { ?>
348				<input type="button" value="<?php echo $LangUI->_('Delete Selected');?>" class="button" onClick='submitForm("delete")'>&nbsp;&nbsp;
349				<?php } ?>
350				<INPUT type="hidden" name="total_recipes" value="<?php echo $counter;?>">
351				<a href="javascript:checkAll(1)"><?php echo $LangUI->_('Check All');?></a> -
352				<a href="javascript:checkAll(0)"><?php echo $LangUI->_('Clear All');?></a>
353			</td>
354		</tr>
355		</form>
356		</table>
357<?php
358		}
359	}
360?>
361</font>
362</p>
363