<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  
  // load viewer library
  require_once('../admin/lib/viewer_functions.php');


  // global items
  list($global_itemsRecords, $global_itemsMetaData) = getRecords(array(
    'tableName'   => 'global_items',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $global_itemsRecord = @$global_itemsRecords[0]; // get first record
  if (!$global_itemsRecord) { dieWith404("Record not found!"); } // show error message if no record found
  
	// search
	$searchOptions = array();
	$searchOptions['keywords'] = @$FORM['q'];
	$searchOptions['perPage'] = "25";
	$searchOptions['debugSql'] = "0";
	
	$searchTables = array();
	
	
	$searchTables['who_we_are_pages'] = array(
		'viewerUrl' => '/who-we-are/',
		'titleField' => 'title',
		'summaryField' => 'primary_content',
		'searchFields' => array('title','primary_content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['what_we_do_pages'] = array(
		'viewerUrl' => '/what-we-do/',
		'titleField' => 'title',
		'summaryField' => 'primary_content',
		'searchFields' => array('title','primary_content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['projects_programs_pages'] = array(
		'viewerUrl' => '/what-we-do/projects-programs/',
		'titleField' => 'title',
		'summaryField' => 'primary_content',
		'searchFields' => array('title','primary_content','search_keywords'),
		'field1' =>  'title',
		);
	
		$searchTables['grants'] = array(
		'viewerUrl' => '/what-we-do/grants/',
		'titleField' => 'title',
		'summaryField' => 'summary',
		'searchFields' => array('title','grantee','summary','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['previous_initiatives'] = array(
		'viewerUrl' => '/what-we-do/?id=8',
		'titleField' => 'title',
		'summaryField' => 'description',
		'searchFields' => array('title','description','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['resource_pages'] = array(
		'viewerUrl' => '/resources/',
		'titleField' => 'title',
		'summaryField' => 'primary_content',
		'searchFields' => array('title','primary_content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['teachable_moments'] = array(
		'viewerUrl' => '/resources/?id=2',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['branches_list'] = array(
		'viewerUrl' => '/resources/?id=3',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['papers_list'] = array(
		'viewerUrl' => '/resources/?id=4',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['roots_list'] = array(
		'viewerUrl' => '/resources/?id=4',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['health_scans_list'] = array(
		'viewerUrl' => '/resources/?id=5',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','search_keywords'),
		'field1' =>  'title',
		);
		
	$searchTables['news_items'] = array(
		'viewerUrl' => '/news/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','source'),
		'field1' =>  'title',
		);
		
	$searchTables['press_release_items'] = array(
		'viewerUrl' => '/news/press-releases/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content','source'),
		'field1' =>  'title',
		);
	
	$searchTables['contct_pages'] = array(
		'viewerUrl' => '/contact-us/',
		'titleField' => 'title',
		'summaryField' => 'primary_content',
		'searchFields' => array('title','primary_content','search_keywords'),
		'field1' =>  'title',
		);
	
	$searchTables['latest_developments'] = array(
		'viewerUrl' => '/latest-developments/',
		'titleField' => 'title',
		'summaryField' => 'content',
		'searchFields' => array('title','content'),
		'field1' =>  'title',
		);
		
	list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
				
  function maxWords($textOrHtml, $maxWords) {
    $text = strip_tags($textOrHtml);
    $words = preg_split("/\s+/", $text, $maxWords+1);
    if (count($words) > $maxWords) { unset($words[$maxWords]); }
    $output = join(' ', $words);
    return $output;
    }

?>
	
	<h2>Search <?php if(@$FORM['q']) echo " Results" ?></h2>
	
	<div class="searchAgain">
			
		<p>
			<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
	
				<input type="text" name="q" value="<?php echo htmlspecialchars(@$FORM['q']); ?>" size="35"> <input type="submit" value="Search<?php if(@$FORM['q']) echo " Again" ?>">
	
			</form>
		</p>
	
		<?php if(@$FORM['q']) { ?>
	
			<p>
				Your search for <strong>"<?php echo htmlspecialchars(@$FORM['q']); ?>"</strong> returned <?php echo $searchDetails['totalRecords']; ?> results:
			</p>
	
		<?php } ?>
		
	</div><!--/searchAgain-->
	
	<?php if(@$FORM['q']!=""): ?>

		<!-- show errors -->
		<?php if ($searchDetails['invalidPageNum']): ?>
			Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
		<?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
			No records matched search query!<br/><br/>
		<?php elseif ($searchOptions['keywords'] == ""): ?>
			Enter a keyword to search.<br/><br/>
		<?php endif ?>

		<?php
			if($searchDetails['noRecordsFound']) {
				}
			else {
				function subval_sort($a,$subkey, $direction = "ASC") {
					foreach($a as $k=>$v) {
					$b[$k] = strtolower($v[$subkey]);
					}
					if ($direction == "DESC") {
					  arsort($b);
					}
					else {
					  asort($b);
					}
				foreach($b as $key=>$val) {
					$c[] = $a[$key];
					}
				return $c;
				}
	
				$searchRows = subval_sort($searchRows,'field1','ASC');
				}
		?>
		
		<ol class="searchResults">
				
			<?php foreach ($searchRows as $record): ?>
			
				<?php
					if($record['tablename']=="who_we_are_pages") $title_prefix = "Who We Are";
					elseif($record['tablename']=="what_we_do_pages") $title_prefix = "What We Do";
					elseif($record['tablename']=="projects_programs_pages") $title_prefix = "Projects &amp; Programs";
					elseif($record['tablename']=="grants") $title_prefix = "Grants";
					elseif($record['tablename']=="previous_initiatives") $title_prefix = "Previous Initiatives";
					elseif($record['tablename']=="resource_pages") $title_prefix = "Resource Pages";
					elseif($record['tablename']=="teachable_moments") $title_prefix = "Teachable Moments";
					elseif($record['tablename']=="branches_list") $title_prefix = "Branches";
					elseif($record['tablename']=="papers_list") $title_prefix = "Papers";
					elseif($record['tablename']=="roots_list") $title_prefix = "ROOTS";
					elseif($record['tablename']=="health_scans_list") $title_prefix = "Health Scans";
					elseif($record['tablename']=="news_items") $title_prefix = "News";
					elseif($record['tablename']=="press_release_items") $title_prefix = "Press Releases";
					elseif($record['tablename']=="contct_pages") $title_prefix = "Contact Us";
					elseif($record['tablename']=="latest_developments") $title_prefix = "Latest Developments";
					else $title_prefix = "";
				?>
				
				<li class="clickbox">
				
					<h4>
						<a href="<?php echo $record['_link'] ?>">
							<?php if($title_prefix) echo $title_prefix." &raquo; " ?><?php echo $record['_title'] ?>
						</a>
					</h4>
					<p>
						<?php if ($record['_summary']): ?>
							<?php  echo maxWords($record['_summary'], 30); ?>...
						<?php else: ?>
							No description available for page.
						<?php endif ?>
					</p>
					
				</li>
				
			<?php endforeach ?>
			
		</ol>
		
	<?php if ($searchDetails['prevPage']): ?>
		<a href="<?php echo $searchDetails['prevPageLink'] ?>">&lt;&lt; prev</a>
	<?php else: ?>
		&lt;&lt; prev
	<?php endif ?>
		- page <?php echo $searchDetails['page'] ?> of <?php echo $searchDetails['totalPages'] ?> -
	<?php if ($searchDetails['nextPage']): ?>
		<a href="<?php echo $searchDetails['nextPageLink'] ?>">next &gt;&gt;</a>
	<?php else: ?>
		next &gt;&gt;
	<?php endif ?>

	<?php endif ?>

	<?php
		if(@$_POST["q"]) {
			// write date/search term to search-log.txt
			$myFile = "search-log.txt";
			$fh = fopen($myFile, 'a');
			$stringData = date("m/d/y h:i:s a")." | Query: ".@$_POST["q"]." (".$searchDetails['totalRecords']." results returned)\n";
			fwrite($fh, $stringData);
			fclose($fh);
			}
	?>
				
