Ejemplo n.º 1
0
 /**
  * It will perfome a search on the ads table
  *
  * $res = Model_Ad::search(array(
  *		'search_string' => "php, london",
  *		'telecommute' => 1,
  *		'jobtype_id' => 3,
  *		'category_id' => 2,
  *		'limit' => 50,
  *		'offset' => 0,
  *		'fields' => array('id', 'title'),
  * ));
  *
  * @param Array $args
  * @return Array
  */
 public static function search($args)
 {
     // Merge default args
     $args = array_merge(array('search_string' => '', 'telecommute' => 0, 'jobtype_id' => 0, 'jobboard_id' => 0, 'category_id' => 0, 'limit' => 50, 'offset' => 0, 'created_after' => 0, 'random_order' => false, 'fields' => array()), $args);
     // Loads application config file
     $config = Kohana::$config->load('application');
     // Create the client, tell it where the server
     // is and how long to wait for a response.
     $sphinx = new Helper_Sphinx();
     $sphinx->SetServer('localhost', 9312);
     $sphinx->SetConnectTimeout(1);
     // Use the exteneded v2 match type
     $sphinx->SetMatchMode(SPH_MATCH_BOOLEAN);
     // Set order & limit
     if ($args['search_string'] != "") {
         $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'created_at DESC, highlight DESC');
     } else {
         $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'created_at DESC');
     }
     $sphinx->SetLimits($args['offset'], $args['limit']);
     // Give me back the results as an array
     $sphinx->SetArrayResult(true);
     // Select only with telecommute
     if ($args['telecommute'] > 0) {
         $sphinx->SetFilter("telecommute", array(1));
     }
     // Select only with jobtype_id
     if ($args['jobtype_id'] > 0) {
         $sphinx->SetFilter("jobtype_id", array($args['jobtype_id']));
     }
     // Select only with jobboard_id
     if ($args['jobboard_id'] > 0) {
         $sphinx->SetFilter("jobboard_id", array($args['jobboard_id']));
     }
     // Select only with category_id
     if ($args['category_id'] > 0) {
         $sphinx->SetFilter("category_id", array($args['category_id']));
     }
     // Select only ads more recentent than created_after
     if ($args['created_after'] > 0) {
         $sphinx->SetFilterRange('created_at', $args['created_after'], strtotime('now'));
     }
     // Set random order
     if ($args['random_order']) {
         $sphinx->setSortMode(SPH_SORT_EXTENDED, '@random');
     }
     // Perfom the query against sphinx server
     $sphinx_results = $sphinx->Query($args['search_string'], 'ads');
     $rows = array();
     $words = array();
     // check if there's any results
     if ($sphinx_results['total'] > 0) {
         $ids = '';
         // prepare the rows ID's to be used against mysql query
         for ($i = 0; $i < count($sphinx_results['matches']); $i++) {
             $ids .= $sphinx_results['matches'][$i]['id'];
             if ($i < count($sphinx_results['matches']) - 1) {
                 $ids .= ',';
             }
         }
         // Merge the array so it can be included in the SQL query
         $fields = implode(',', $args['fields']);
         // Get the ID rows from MySQL
         $rows = DB::query(Database::SELECT, "SELECT {$fields} FROM\n\t\t\t\tads WHERE id IN({$ids}) AND active = 1 ORDER BY FIELD(id, {$ids}) ")->execute()->as_array();
         // Get all the keywords
         if (isset($sphinx_results['words'])) {
             foreach ($sphinx_results['words'] as $key => $value) {
                 $words[] = $key;
             }
         }
         // Cleans and formats data
         foreach ($rows as $key => $ad) {
             $rows[$key]['created_at'] = Helper_Datetime::format($ad['created_at'], $config['ad']['date_format']);
             $rows[$key]['is_new'] = Helper_Datetime::is_new($ad['created_at'], $config['ad']['is_new_days']);
             $rows[$key]['jobtype'] = ORM::factory('jobtype', $ad['jobtype_id'])->name;
             $rows[$key]['jobboard'] = ORM::factory('jobboard', $ad['jobboard_id'])->name;
             $rows[$key]['url'] = Helper_Utils::get_ad_url($ad['title'], $ad['id']);
             $rows[$key]['title'] = Text::limit_chars($ad['title'], 55, '...');
             if (in_array('company_logo', $args['fields'])) {
                 if (!$ad['company_logo']) {
                     $rows[$key]['company_logo'] = 'default';
                 }
                 $rows[$key]['company_logo'] = Helper_Utils::static_path('media/uploads/' . $rows[$key]['company_logo'] . '_thumb.png');
             }
             $title[] = $rows[$key]['title'];
             unset($rows[$key]['jobtype_id']);
         }
     }
     return array('rows' => $rows, 'total' => $sphinx_results['total'], 'words' => $words);
 }
Ejemplo n.º 2
0
    echo $ad->company_address;
    ?>
				</span>
		<?php 
}
?>
		<!-- ./Company Address -->

	</div>
	<!-- ./ad-header-left -->

	<!-- Ad-description -->
	<div id="ad-description">
		<hr>
		<small class="muted">Created at <?php 
echo Helper_Datetime::format($ad->created_at, $config['ad']['date_format']);
?>
</small>
		<p>
			<?php 
echo $ad->description;
?>
		</p>

		<br/>
		<?php 
if ($ad->budget) {
    ?>
			<span class="budget">Budget: <strong><?php 
    echo $ad->budget;
    ?>