Created
January 20, 2009 18:14
-
-
Save delonnewman/49582 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pdsearch_menu() { | |
$items['profile_search'] = array( | |
'title' => 'Profile Search', | |
'page callback' => 'profile_search', | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK | |
); | |
return $items; | |
} | |
function profile_search() { | |
return drupal_get_form('profile_search_form'); | |
} | |
global $RESULTS; | |
$RESULTS = get_results(); // HACK: This is horrible, but it looks like it may work | |
function profile_search_form() { | |
global $RESULTS; | |
$form['search'] = array( | |
'#type' => 'fieldset', | |
'#collapsible' => TRUE, | |
'#attributes' => array( | |
'style' => 'border: none; padding: 0', | |
), | |
); | |
$form['search']['profile_active'] = array( | |
'#type' => 'hidden', | |
'#default_value' => 1, | |
); | |
$form['search']['position-field'] = array( | |
'#type' => 'fieldset', | |
'#collapsible' => TRUE, | |
'#attributes' => array( | |
'style' => 'width: 140px; float:left; border: none; padding: 0 5 0 0', | |
), | |
); | |
$form['search']['position-field']['position'] = array( | |
'#type' => 'select', | |
'#title' => t('Select Position'), | |
//'#default_value' => $_POST['position'], | |
'#options' => array( | |
'' => '---- Any ----', | |
'babysitter' => 'Babysitter', | |
'housekeeper' => 'Housekeeper', | |
'helper' => "Mother's Helper", | |
'assistant' => 'Personal Assistant', | |
'nanny' => 'Nanny', | |
), | |
); | |
$form['search']['zip-field'] = array( | |
'#type' => 'fieldset', | |
'#collapsible' => TRUE, | |
'#attributes' => array( | |
'style' => 'width: 100px; border: none; padding: 0', | |
), | |
); | |
$form['search']['zip-field']['zip'] = array( | |
'#type' => 'textfield', | |
'#title' => 'Enter Zip Code', | |
//'#default_value' => $_POST['zip'], | |
'#size' => 10 | |
); | |
$form['search']['search-field'] = array( | |
'#type' => 'fieldset', | |
'#collapsible' => TRUE, | |
'#attributes' => array( | |
'style' => 'width: 100px; border: none; padding: 0', | |
), | |
); | |
$form['search']['search-field']['search_button'] = array( | |
'#type' => 'submit', | |
'#value' => t('Search') | |
); | |
$form['results'] = array( | |
'#value' => $RESULTS | |
); | |
return $form; | |
} | |
function profile_search_form_submit($form_id, $form_values) { | |
foreach (array_keys($form_values) as $param) { | |
switch ($param) { | |
case 'profile_active': | |
$conditions[_get_field_id($param)] = "= %d"; | |
$values[_get_field_id($param)] = 1; | |
break; | |
case 'position': | |
if ($form_values['position'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "LIKE '%%%s%%'"; | |
$values[_get_field_id($param)] = $form_values['position']; | |
break; | |
case 'zip': | |
if ($form_values['zip'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "= '%s'"; | |
$values[_get_field_id($param)] = $form_values['zip']; | |
break; | |
case 'children': | |
if ($form_values['children'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "LIKE '%%%s%%'"; | |
$values[_get_field_id($param)] = $form_values['children']; | |
break; | |
case 'schedule': | |
if ($form_values['schedule'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "LIKE '%%%s%%'"; | |
$values[_get_field_id($param)] = $form_values['schedule']; | |
break; | |
case 'living': | |
if ($form_values['living'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "LIKE '%%%s%%'"; | |
$values[_get_field_id($param)] = $form_values['living']; | |
break; | |
case 'vehicle': | |
if ($form_values['vehicle'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "= %d"; | |
$values[_get_field_id($param)] = $form_values['vehicle']; | |
break; | |
case 'legal': | |
if ($form_values['legal'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "= %d"; | |
$values[_get_field_id($param)] = $form_values['legal']; | |
break; | |
case 'education': | |
if ($form_values['education'] == "") { continue; } | |
$conditions[_get_field_id($param)] = "LIKE '%%%s%%'"; | |
$values[_get_field_id($param)] = $form_values['education']; | |
break; | |
} | |
} | |
global $RESULTS; | |
$RESULTS = get_results($conditions, $values); | |
} | |
function get_results($conditions=NULL, $values=NULL) { | |
$conditions[_get_field_id('profile_active')] = "= %d"; | |
$values[_get_field_id('profile_active')] = 1; | |
return _show_profile_list_items(_get_providers($conditions, $values)); | |
} | |
/** | |
* Data access functions | |
*/ | |
function _get_providers($conditions=array(), $values=array()) { | |
$sql = generate_sql($conditions); | |
return _get_array_from_result(db_query($sql, $values), 'uid'); | |
} | |
function _get_field($field_id) { | |
return db_result(db_query('SELECT name FROM profile_fields WHERE fid %d', $field_id)); | |
} | |
function _get_field_id($field) { | |
return db_result(db_query("SELECT fid FROM profile_fields WHERE name = '%s'", $field)); | |
} | |
/** | |
* Utility functions | |
*/ | |
// TODO: There are a few functions like these that are general enough that | |
// they should really be put in a utility library | |
function _calculate_age($birth_date) { | |
$date_now = getdate(); | |
$age = $date_now['year'] - $birth_date['year']; | |
if ($date_now['month'] < $birth_date['month']) { | |
$age -= 1; | |
} elseif ($date_now['month'] == $birth_date['month']) { | |
if ($date_now['day'] < $birth_date['day']) { | |
$age -= 1; | |
} | |
} | |
return $age; | |
} | |
function generate_sql($conditions, $frags = array( | |
'table_listing' => array(), | |
'from_clause' => array(), | |
'where_clause' => array(), | |
'uid_resolution' => array())) { | |
if (empty($conditions)) { | |
$table_listing = join(', ', $frags['table_listing']); | |
$from_clause = join(', ', $frags['from_clause']); | |
$where_clause = join(' AND ', $frags['where_clause']); | |
$uid_resolution = ' AND ' . join(' AND ', $frags['uid_resolution']); | |
return "SELECT $table_listing FROM $from_clause WHERE $where_clause$uid_resolution"; | |
} elseif (!empty($conditions) && sizeof($conditions) == 1) { | |
$fids = array_keys($conditions); | |
$where_clause = "fid = " . $fids[0] . ' AND value ' . $conditions[$fids[0]]; | |
return "SELECT uid FROM profile_values WHERE $where_clause"; | |
} | |
$n = 0; | |
$alpha = range('a', 'z'); | |
foreach (array_keys($conditions) as $fid) { | |
$table_name = $alpha[$n]; | |
array_push($frags['table_listing'], $table_name . '.uid'); | |
array_push($frags['from_clause'], 'profile_values ' . $table_name); | |
array_push($frags['where_clause'], "($table_name.fid = $fid AND $table_name.value " . $conditions[$fid] . ')'); | |
if ($n < sizeof($conditions)) { | |
array_push($frags['uid_resolution'], $table_name . '.uid = ' . $alpha[$n+1] . '.uid'); | |
} | |
unset($conditions[$fid]); | |
$n++; | |
} | |
return generate_sql($conditions, $frags); | |
} | |
/** | |
* UI Elements | |
*/ | |
// TODO: This could use a clean up. | |
function _show_profile_list_items(&$providers) { | |
global $user; | |
drupal_add_js('sites/all/modules/custom/pdsearch/jquery.truncate-2.3-pack.js', 'module'); | |
$html .= '<table id="profile_search"><tr><thead>'; | |
if (($user->uid != NULL) && !in_array('provider', array_values($user->roles))) { | |
$html .= '<th> </th>'; | |
} | |
$html .= '<th>Photo</th>'; | |
$html .= '<th>First Name</th><th>Age</th><th>Position</th><th>City/State</th><th>Zip Code</th></tr></thead>'; | |
$html .= '<tbody>'; | |
$javascript = "$(function() {\n"; | |
$num_per_page = 5; | |
$total_items = sizeof($providers); | |
$num_pages = $total_items / $num_per_page; | |
if($num_pages > (int)$num_pages) { | |
$num_pages = (int)$num_pages + 1; | |
} | |
$page_num = (int)arg(1) - 1; | |
$paged_providers = array_slice($providers, $page_num * $num_per_page, $num_per_page); | |
foreach ($paged_providers as $provider_id) { | |
$provider = user_load(array('uid' => $provider_id)); | |
$javascript .= "\t$('.position_$provider_id').truncate(60);\n"; | |
$javascript .= "\t$('.location_$provider_id').truncate(20);\n"; | |
profile_load_profile($provider); | |
// TODO: Make this an AJAX call | |
$html .= '<tr>'; | |
if (($user->uid != NULL) && !in_array('provider', array_values($user->roles))) { | |
$html .= '<td>' . _get_favorite_icon($user->uid, $provider) . '</td>'; | |
} | |
if ($provider->picture) { | |
$html .= '<td><img width="32" height="32" src="' . $provider->picture . '" alt="' . $provider->first_name . ' Photo" /></td>'; | |
} else { | |
$html .= '<td><img src="sites/all/themes/pd/images/icons/default-user-32x32.png" alt="' . $provider->first_name . ' Photo" /></td>'; | |
} | |
$html .= '<td>' . l($provider->first_name, 'user/' . $provider->uid) . '</td>'; | |
$html .= '<td>' . _calculate_age($provider->birth_date) . '</td>'; | |
if (is_array($provider->position)) { | |
$html .= '<td class="position_' . $provider->uid . '">' . join(', ', $provider->position) . '</td>'; | |
} else { | |
$html .= '<td>' . $provider->position . '</td>'; | |
} | |
$html .= "<td class=\"location_$provider_id\">" . $provider->city . ', ' . $provider->state . '</td>'; | |
$html .= '<td>' . $provider->zip . '</td>'; | |
} | |
$html .= '</tbody></table>'; | |
$javascript .= "});"; | |
drupal_add_js($javascript, 'inline'); | |
if ($total_items > $num_per_page) { | |
$html .= '<div id="pager"><ul class="links">'; | |
if ($page_num > 1) { | |
$html .= '<li>' . l('<<Previous', 'profile_search/' . $page_num) . '</li>'; | |
} | |
foreach (range(1, $num_pages) as $num) { | |
$html .= '<li>' . l($num, 'profile_search/' . $num) . '</li>'; | |
} | |
if ($page_num < $num_pages - 1) { | |
$html .= '<li>' . l('Next>>', 'profile_search/' . ($page_num + 2)) . '</li>'; | |
} | |
$html .= '</ul></div>'; | |
} | |
return $html; | |
} | |
// TODO: This could use a clean up. | |
function _show_profile_list_item($provider_id) { | |
global $user; | |
$provider = user_load(array('uid' => $provider_id)); | |
$javascript .= "\t$('.position_$provider_id').truncate(60);\n"; | |
$javascript .= "\t$('.location_$provider_id').truncate(20);\n"; | |
profile_load_profile($provider); | |
$html .= '<tr>'; | |
if (($user->uid != NULL) && !in_array('provider', array_values($user->roles))) { | |
$html .= '<td>' . _get_favorite_icon($user->uid, $provider) . '</td>'; | |
} | |
if ($provider->picture) { | |
$html .= '<td><img width="32" height="32" src="' . $provider->picture . '" alt="' . $provider->first_name . ' Photo" /></td>'; | |
} else { | |
$html .= '<td><img src="sites/all/themes/pd/images/icons/default-user-32x32.png" alt="' . $provider->first_name . ' Photo" /></td>'; | |
} | |
$html .= '<td>' . l($provider->first_name, 'user/' . $provider->uid) . '</td>'; | |
$html .= '<td>' . _calculate_age($provider->birth_date) . '</td>'; | |
if (is_array($provider->position)) { | |
$html .= '<td class="position_' . $provider->uid . '">' . join(', ', $provider->position) . '</td>'; | |
} else { | |
$html .= '<td>' . $provider->position . '</td>'; | |
} | |
$html .= "<td class=\"location_$provider_id\">" . $provider->city . ', ' . $provider->state . '</td>'; | |
$html .= '<td>' . $provider->zip . '</td>'; | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment