| 1 |
<?php
|
| 2 |
// $Id: coach.module 4.2.05
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Fearless Living Coach Module
|
| 6 |
*
|
| 7 |
*/
|
| 8 |
|
| 9 |
/** To Do:
|
| 10 |
_ Let Admin (with 'manage coaches' access) create coach entry for a user.
|
| 11 |
maybe they just explicitly enter the UID for the coach...
|
| 12 |
_ Get variables out of db_query... use db_query properly.
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_help().
|
| 16 |
*
|
| 17 |
* Throughout Drupal, hook_help() is used to display help text at the top of
|
| 18 |
* pages. Some other parts of Drupal pages get explanatory text from these hooks
|
| 19 |
* as well. We use it here to provide a description of the module on the
|
| 20 |
* module administration page.
|
| 21 |
*/
|
| 22 |
function coach_help($section) {
|
| 23 |
global $user;
|
| 24 |
$top = "<div class='coach-help'><strong>HELP:</strong><br />
|
| 25 |
This page is where you edit the coach information that is displayed to site visitors. Some things to note:<br />
|
| 26 |
1) You should enter <strong>your first and last name for the title</strong> field below.<br />
|
| 27 |
2) If you'd like to <strong>add a picture</strong> to appear with your coach listing, upload the picture into your <a href='".url('user/'.$user->uid.'/edit')."'>user profile</a>. </div>";
|
| 28 |
switch ($section) {
|
| 29 |
case 'node/add/coach':
|
| 30 |
return $top;
|
| 31 |
case (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit'):
|
| 32 |
$node = node_load(array('nid' => arg(1)));
|
| 33 |
if ($node->type == 'coach') {
|
| 34 |
return $top;
|
| 35 |
}
|
| 36 |
case 'admin/modules#description':
|
| 37 |
// This description is shown in the listing at admin/modules.
|
| 38 |
return t('Coach information management.');
|
| 39 |
case 'node/add#coach':
|
| 40 |
// This description shows up when users click "create content."
|
| 41 |
return t('Edit coach information.');
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Implementation of hook_node_name().
|
| 47 |
*
|
| 48 |
* This is a required node hook. Since our module only defines one node
|
| 49 |
* type, we won't implement hook_node_types(), and our hook_node_name()
|
| 50 |
* implementation simply returns the translated name of the node type.
|
| 51 |
*/
|
| 52 |
function coach_node_name($node) {
|
| 53 |
return t('coach listing');
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of hook_access().
|
| 58 |
*
|
| 59 |
* Node modules may implement node_access() to determine the operations
|
| 60 |
* users may perform on nodes. This example uses a very common access pattern.
|
| 61 |
*/
|
| 62 |
function coach_access($op, $node) {
|
| 63 |
global $user;
|
| 64 |
//print_r($node);
|
| 65 |
if ($op == 'create') {
|
| 66 |
// Only users with permission to do so may create this node type.
|
| 67 |
if (user_access('manage coaches') || (user_access('edit coach info') && ! coach_get_entry($user->uid))) {
|
| 68 |
// has coach permission and hasn't created one yet
|
| 69 |
return TRUE;
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
// Users who create a node may edit or delete it later, assuming they have the
|
| 74 |
// necessary permissions.
|
| 75 |
if ($op == 'update' || $op == 'delete') {
|
| 76 |
if (user_access('manage coaches') || (user_access('edit coach info') && ($user->uid == $node->uid))) {
|
| 77 |
return TRUE;
|
| 78 |
}
|
| 79 |
}
|
| 80 |
if ($op == 'view') {
|
| 81 |
return TRUE;
|
| 82 |
}
|
| 83 |
}
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Implementation of hook_perm().
|
| 87 |
*
|
| 88 |
* Since we are limiting the ability to create new nodes to certain users,
|
| 89 |
* we need to define what those permissions are here. We also define a permission
|
| 90 |
* to allow users to edit the nodes they created.
|
| 91 |
*/
|
| 92 |
function coach_perm() {
|
| 93 |
return array('edit coach info', 'edit coach personal page', 'manage coaches');
|
| 94 |
}
|
| 95 |
|
| 96 |
|
| 97 |
function coach_settings() {
|
| 98 |
$form = form_textfield(t('Coaches Per Page'), 'coach_limit', variable_get('coach_limit', 15), 3, 3, 'The number of coaches that appear on a page.');
|
| 99 |
$form .= form_textfield(t('Words Per Coach'), 'coach_list_trim', variable_get('coach_list_trim', 20), 3, 3, 'The number of words shown in coach\'s description when shown in a list.');
|
| 100 |
return $form;
|
| 101 |
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Implementation of hook_link().
|
| 106 |
*
|
| 107 |
* This is implemented so that an edit link is displayed for users who have
|
| 108 |
* the rights to edit a node.
|
| 109 |
*/
|
| 110 |
|
| 111 |
// why can't we use an 'edit' tab?
|
| 112 |
function coach_link($type, $node = 0, $main) {
|
| 113 |
$links = array();
|
| 114 |
if ($type == 'node' && $node->type == 'coach') {
|
| 115 |
// Don't display a redundant edit link if they are node administrators.
|
| 116 |
if (coach_access('update', $node) && !user_access('manage coaches')) {
|
| 117 |
$links[] = l(t('edit this info'), "node/$node->nid/edit");
|
| 118 |
}
|
| 119 |
}
|
| 120 |
return $links;
|
| 121 |
}
|
| 122 |
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Implementation of hook_menu().
|
| 126 |
*
|
| 127 |
* In order for users to be able to add nodes of their own, we need to
|
| 128 |
* give them a link to the node composition form here.
|
| 129 |
*/
|
| 130 |
|
| 131 |
function coach_menu($may_cache) {
|
| 132 |
$items = array();
|
| 133 |
global $user;
|
| 134 |
$nid = coach_get_entry($user->uid);
|
| 135 |
if ($may_cache) {
|
| 136 |
// create a coach entry
|
| 137 |
$items[] = array('path' => 'node/add/coach', 'title' => t('coach listing'), 'access' => (user_access('edit coach info') && !$nid));
|
| 138 |
}
|
| 139 |
return $items;
|
| 140 |
}
|
| 141 |
|
| 142 |
/**
|
| 143 |
* Implementation of hook_form().
|
| 144 |
*
|
| 145 |
* Now it's time to describe the form for collecting the information
|
| 146 |
* specific to this node type. This hook requires us to return some HTML
|
| 147 |
* that will be later placed inside the form.
|
| 148 |
*/
|
| 149 |
function coach_form(&$node, &$error) {
|
| 150 |
$output = '';
|
| 151 |
global $user;
|
| 152 |
profile_load_profile($user);
|
| 153 |
|
| 154 |
// In order to be able to attach taxonomy terms to this node, we need
|
| 155 |
// to display the appropriate form elements.
|
| 156 |
if (function_exists('taxonomy_node_form')) {
|
| 157 |
$output .= implode('', taxonomy_node_form('coach', $node));
|
| 158 |
}
|
| 159 |
// Now we define the form elements specific to our node type.
|
| 160 |
$output .= form_textfield(t('First Name'), 'firstname', $node->firstname ? $node->firstname : $user->profile_firstname , 60, '', $error['firstname']);
|
| 161 |
$output .= form_textfield(t('Last Name'), 'lastname', $node->lastname ? $node->lastname : $user->profile_lastname, 60, '', $error['lastname']);
|
| 162 |
$output .= form_textfield(t('Additional Certifications'), 'cert', $node->cert, 60, '', 'Your CFLC level will be added automatically. If you have more letters that go after your name, put \'em here.');
|
| 163 |
// TODO: check access and show or hide uid
|
| 164 |
$output .= form_hidden('cuid', $GLOBALS['user']->uid);
|
| 165 |
if (!$node->email) {$node->email = $GLOBALS['user']->mail;}
|
| 166 |
$output .= form_select(t('Gender'), 'sex', $node->sex ? $node->sex : $user->profile_sex, array("female" => "female","male" => "male"), '');
|
| 167 |
$output .= form_textfield(t('Email Address'), 'email', $node->email, 60, '', $error['email']);
|
| 168 |
$output .= form_textfield(t('Web Site'), 'website', $node->website ? $node->website : $user->profile_website, 60, '', 'Optional: Address of your website (<b>excluding</b> <i>http://</i>). Example: <i>www.fearlessliving.org</i>');
|
| 169 |
$output .= form_textfield(t('Main Phone Number'), 'phone', $node->phone, 60, '');
|
| 170 |
$output .= form_textfield(t('Alternative Phone'), 'altphone', $node->altphone, 60, '');
|
| 171 |
$output .= form_textfield(t('Fax Number'), 'fax', $node->fax, 60, '');
|
| 172 |
$output .= form_textarea(t('Directory Bio'), 'teaser', $node->teaser, 60, 10, 'This short bio is what appears in the coach directory and at the top of your coach page. There is a limit of 250 characters (approximately 40 words), so get the essentials in there and provide a reason for people to click on your coach page.');
|
| 173 |
if (user_access('edit coach personal page')) {
|
| 174 |
$output .= form_textarea(t('Coach Page Content'), 'body', $node->body, 60, 20, 'Use this area to publish expanded information about yourself and your CFLC services. You may choose to include an expanded bio, timely announcements, personal web site links, photos, rate information, or other relevant Fearless information. For detailed instructions on using the tools on this site to publish your information, download this PDF.');
|
| 175 |
}
|
| 176 |
$output .= filter_form('format', $node->format);
|
| 177 |
$param = array();
|
| 178 |
|
| 179 |
return $output;
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
| 183 |
* Implementation of hook_validate().
|
| 184 |
*
|
| 185 |
*/
|
| 186 |
function coach_validate(&$node) {
|
| 187 |
if (!$_POST['edit']) {
|
| 188 |
if (!$node->title) {
|
| 189 |
$user = user_load(array('uid'=>$node->uid));
|
| 190 |
$node->title = $user->profile_firstname.' '.$user->profile_lastname;
|
| 191 |
}
|
| 192 |
}
|
| 193 |
if (strlen($node->teaser) > 250) {
|
| 194 |
form_set_error('teaser', 'Your Directory Bio exceeds the 250 character limit. Please shorten and resubmit.');
|
| 195 |
}
|
| 196 |
if (!user_access('edit coach personal page')) {
|
| 197 |
$node->body = '';
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
/**
|
| 202 |
* Implementation of hook_insert().
|
| 203 |
*
|
| 204 |
* As a new node is being inserted into the database, we need to do our own
|
| 205 |
* database inserts.
|
| 206 |
*/
|
| 207 |
function coach_insert($node) {
|
| 208 |
db_query("INSERT INTO {coach} (nid, cuid, firstname, lastname, cert, email, website, phone, altphone, fax, sex) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $node->nid, $node->cuid, $node->firstname, $node->lastname, $node->cert, $node->email, $node->website, $node->phone, $node->altphone, $node->fax, $node->sex);
|
| 209 |
}
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Implementation of hook_update().
|
| 213 |
*
|
| 214 |
* As an existing node is being updated in the database, we need to do our own
|
| 215 |
* database updates.
|
| 216 |
*/
|
| 217 |
function coach_update($node) {
|
| 218 |
db_query("UPDATE {coach} SET firstname = '%s', lastname = '%s', cert = '%s', email = '%s', website = '%s', phone = '%s', altphone = '%s', fax = '%s', sex = '%s' WHERE nid = %d", $node->firstname, $node->lastname, $node->cert, $node->email, $node->website, $node->phone, $node->altphone, $node->fax, $node->sex, $node->nid);
|
| 219 |
}
|
| 220 |
|
| 221 |
/**
|
| 222 |
* Implementation of hook_delete().
|
| 223 |
*
|
| 224 |
* When a node is deleted, we need to clean up related tables.
|
| 225 |
*/
|
| 226 |
function coach_delete($node) {
|
| 227 |
db_query('DELETE FROM {coach} WHERE nid = %d', $node->nid);
|
| 228 |
}
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Implementation of hook_load().
|
| 232 |
*
|
| 233 |
* Now that we've defined how to manage the node data in the database, we
|
| 234 |
* need to tell Drupal how to get the node back out. This hook is called
|
| 235 |
* every time a node is loaded, and allows us to do some loading of our own.
|
| 236 |
*/
|
| 237 |
function coach_load($node) {
|
| 238 |
$additions = db_fetch_object(db_query('SELECT c.cuid, c.firstname, c.lastname, c.cert, c.email, c.website, c.phone, c.altphone, c.fax, c.sex, u.picture FROM {coach} c INNER JOIN {users} u ON u.uid = c.cuid WHERE nid = %d', $node->nid));
|
| 239 |
$additions->cert = coach_cert_role($additions->cuid) . ($additions->cert ? (', '.$additions->cert) : (''));
|
| 240 |
return $additions;
|
| 241 |
}
|
| 242 |
|
| 243 |
/**
|
| 244 |
* Implementation of hook_view().
|
| 245 |
*
|
| 246 |
* This is a typical implementation that simply runs the node text through
|
| 247 |
* the output filters.
|
| 248 |
*/
|
| 249 |
function coach_view(&$node, $teaser = FALSE, $page = FALSE) {
|
| 250 |
$node = node_prepare($node, $teaser);
|
| 251 |
//$output = theme('coach_out', $node, $content);
|
| 252 |
$node->teaser = theme('coach_teaser', $node, $node->teaser, $page);
|
| 253 |
$node->body = $node->teaser . theme('coach_body', $node, $node->body);
|
| 254 |
}
|
| 255 |
|
| 256 |
function coach_load_random() {
|
| 257 |
$node = db_fetch_object(db_query('SELECT c.cuid, c.firstname, c.lastname, c.cert, c.email, c.website, c.phone, c.altphone, c.fax, c.sex, n.body, n.teaser, n.title, n.uid, n.nid, u.picture FROM {coach} c INNER JOIN {node} n ON c.nid = n.nid INNER JOIN {users} u ON n.uid = u.uid ORDER BY rand()'));
|
| 258 |
return $node;
|
| 259 |
}
|
| 260 |
|
| 261 |
function theme_coach_teaser($node, $content, $page) {
|
| 262 |
$output = '<div class="coach_teaser">';
|
| 263 |
if ($node->picture) {
|
| 264 |
$output .= theme('image', $node->picture, 'alt', 'title', array('style'=>"float:left; margin-right: 5px; margin-bottom: 5px"));
|
| 265 |
}
|
| 266 |
if ($node->cert) {
|
| 267 |
$list[] = $node->cert;
|
| 268 |
}
|
| 269 |
$list['loc'] = $node->location['city'] ? $node->location['city'] : '';
|
| 270 |
$list['loc'] .= ($node->location['city'] && $node->location['province']) ? ', ' : '';
|
| 271 |
$list['loc'] .= ($node->location['province']) ? strtoupper($node->location['province']) : '';
|
| 272 |
if ($node->phone) {
|
| 273 |
$list[] = 'ph: '.$node->phone;
|
| 274 |
}
|
| 275 |
if ($node->website) {
|
| 276 |
$list[] = '<a href = "'.(strstr($node->website, 'http') ? ($node->website) : ('http://'.$node->website)).'">'.$node->website.'</a>';
|
| 277 |
}
|
| 278 |
if ($node->email) {
|
| 279 |
$list[] = '<a href = "mailto:'.$node->email.'">'.$node->email.'</a>';
|
| 280 |
}
|
| 281 |
$output .= '<div class="coach_credits">';
|
| 282 |
$output .= implode(' | ', $list);
|
| 283 |
$output .= '</div>';
|
| 284 |
$output .= '<div class="coach_teaser">'.$content.'</div>';
|
| 285 |
if (!$page) {
|
| 286 |
$output .= ' '.l(t('Read my coach page...'), 'node/'.$node->nid);
|
| 287 |
}
|
| 288 |
if ($node->distance) {
|
| 289 |
$output .= '<br />distance: <strong>'.$node->distance.' '.$node->distance_unit.'</strong> from '.$node->from_zip;
|
| 290 |
}
|
| 291 |
$output .= '<br clear="all" /></div>';
|
| 292 |
return $output;
|
| 293 |
}
|
| 294 |
|
| 295 |
function theme_coach_body($node, $content) {
|
| 296 |
if (strlen(trim($content)) > 6) {
|
| 297 |
$output = '<div class="coach_body">';
|
| 298 |
$output .= $content;
|
| 299 |
$output .= '<br clear="all" /></div>';
|
| 300 |
return $output;
|
| 301 |
}
|
| 302 |
}
|
| 303 |
|
| 304 |
|
| 305 |
function coach_random_coach() {
|
| 306 |
$node = coach_load_random();
|
| 307 |
if ($node->picture) {
|
| 308 |
$output = '<div class="coach-rand-pic">'.theme('image', $node->picture, $node->title).'</div>';
|
| 309 |
}
|
| 310 |
$output .= '<div class="coach-rand-name">'.$node->title.'</div>';
|
| 311 |
$output .= '<div class="coach-rand-body">'.coach_word_trim($node->teaser, 25).' '.l('more...', 'node/'.$node->nid).'</div>';
|
| 312 |
return $output;
|
| 313 |
}
|
| 314 |
|
| 315 |
function mt_random_coach() {
|
| 316 |
return coach_random_coach();
|
| 317 |
}
|
| 318 |
|
| 319 |
// not used... see coach_word_trim()
|
| 320 |
function coach_word_truncate($text, $count) {
|
| 321 |
if (!count(explode(" ", $text)) > $count) {
|
| 322 |
return $text;
|
| 323 |
}
|
| 324 |
else {
|
| 325 |
return trim(implode(" ", array_slice(explode(" ", $text), 0, $count)));
|
| 326 |
}
|
| 327 |
}
|
| 328 |
|
| 329 |
|
| 330 |
/**
|
| 331 |
* Return a rendered list of coaches
|
| 332 |
* $type can be
|
| 333 |
* 'alpha' - alphabetical list
|
| 334 |
* 'geo' - list by distance from $zip in $country
|
| 335 |
*
|
| 336 |
*/
|
| 337 |
|
| 338 |
function coach_list_by($type, $zip = NULL, $country = NULL) {
|
| 339 |
global $user;
|
| 340 |
$countries = _location_get_iso3166_list();
|
| 341 |
switch ($type) {
|
| 342 |
case 'alpha':
|
| 343 |
$nids = coach_by_alpha_better($user->location['country']);
|
| 344 |
while ($row = db_fetch_object($nids)) {
|
| 345 |
$node = node_load(array('nid'=>$row->nid));
|
| 346 |
if ($node->nid) {
|
| 347 |
if ($node->location['country'] != $lastcountry) {
|
| 348 |
$output .= theme('coach_location_head', $countries[$node->location['country']]);
|
| 349 |
}
|
| 350 |
$lastcountry = $node->location['country'];
|
| 351 |
$output .= node_view($node, TRUE);
|
| 352 |
}
|
| 353 |
}
|
| 354 |
$output .= '<br /><br />'.theme('pager', array(), 20);
|
| 355 |
break;
|
| 356 |
case 'geo':
|
| 357 |
if ($_REQUEST['country']) {
|
| 358 |
$zip = $_REQUEST['zip'];
|
| 359 |
$country = $_REQUEST['country'];
|
| 360 |
}
|
| 361 |
elseif ($_REQUEST['zip']) {
|
| 362 |
form_set_error('country', t('You need to select a country as well as a zip code.'));
|
| 363 |
}
|
| 364 |
elseif ($user->uid > 0) {
|
| 365 |
$zip = $user->location['postal_code'];
|
| 366 |
$country = $user->location['country'];
|
| 367 |
}
|
| 368 |
if (!$zip && !$country) {
|
| 369 |
$zip = '80302';
|
| 370 |
$country = 'us';
|
| 371 |
}
|
| 372 |
$output = theme('coach_location_form', $country, $zip);
|
| 373 |
$nids = coach_by_distance_better($zip, $country);
|
| 374 |
while ($row = db_fetch_object($nids)) {
|
| 375 |
$node = node_load(array('nid'=>$row->nid));
|
| 376 |
if ($node->nid) {
|
| 377 |
if ($node->location['country'] != $lastcountry) {
|
| 378 |
$output .= theme('coach_location_head', $countries[$node->location['country']]);
|
| 379 |
}
|
| 380 |
$lastcountry = $node->location['country'];
|
| 381 |
$node->distance_unit = 'mi';
|
| 382 |
//convert meters to miles/km
|
| 383 |
$node->distance = round($row->distance/(($row->distance_unit == 'km') ? 1000.0 : 1609.347), 1);
|
| 384 |
$node->from_zip = $zip;
|
| 385 |
$output .= node_view($node, TRUE);
|
| 386 |
}
|
| 387 |
}
|
| 388 |
$output .= '<br /><br />'.theme('pager', array(), 20, 0, array('zip'=>$zip, 'country'=>$country));
|
| 389 |
break;
|
| 390 |
case '':
|
| 391 |
|
| 392 |
}
|
| 393 |
return $output;
|
| 394 |
}
|
| 395 |
|
| 396 |
function theme_coach_location_form($country, $zip) {
|
| 397 |
$row[0][] = form_select('Country', 'country', $country, coach_get_countries(), NULL, 0, FALSE, TRUE);
|
| 398 |
$row[0][] = form_textfield('Postal Code', 'zip', $zip, 16, 16);
|
| 399 |
$row[0][] = form_submit('Show by distance', 'x');
|
| 400 |
$form = theme('table', '', $row, $attributes = array('style'=>'width:100%;'));
|
| 401 |
$form = preg_replace('/edit\[(.*?)\]/i', '$1', $form);
|
| 402 |
$output = form($form, 'get');
|
| 403 |
return $output;
|
| 404 |
}
|
| 405 |
|
| 406 |
function theme_coach_location_head($text) {
|
| 407 |
return "<div class='coach-country-head'><h1>".$text."</h1></div>";
|
| 408 |
}
|
| 409 |
|
| 410 |
/**
|
| 411 |
* Find out if the user already has an entry.
|
| 412 |
*/
|
| 413 |
|
| 414 |
function coach_get_entry($cuid) {
|
| 415 |
$obj = db_fetch_object(db_query("SELECT nid FROM {coach} WHERE cuid = %d", $cuid));
|
| 416 |
return $obj->nid;
|
| 417 |
}
|
| 418 |
|
| 419 |
function coach_redirect() {
|
| 420 |
global $user;
|
| 421 |
$nid = coach_get_entry($user->uid);
|
| 422 |
if ($nid) {
|
| 423 |
drupal_set_message('Redirecting to your curent coach entry');
|
| 424 |
drupal_goto('node/'.$nid.'/edit');
|
| 425 |
return;
|
| 426 |
}
|
| 427 |
node_page();
|
| 428 |
}
|
| 429 |
|
| 430 |
function coach_search_form_page() {
|
| 431 |
print theme('page', coach_search_form());
|
| 432 |
}
|
| 433 |
|
| 434 |
|
| 435 |
/*
|
| 436 |
function coach_search_form() {
|
| 437 |
$edit = $_POST['edit'];
|
| 438 |
$from_get = FALSE; // boolean to remember if variables came from a get
|
| 439 |
global $user;
|
| 440 |
|
| 441 |
switch ($_POST['op']) {
|
| 442 |
case 'Show Alphabetically' :
|
| 443 |
$sort = 'alpha';
|
| 444 |
break;
|
| 445 |
case 'Show By Location' :
|
| 446 |
$sort = 'dist';
|
| 447 |
break;
|
| 448 |
}
|
| 449 |
|
| 450 |
// if there's no zip code, or the country is not a 'supported' country
|
| 451 |
|
| 452 |
//drupal_set_title('Fearless Living Certified Life Coaches');
|
| 453 |
|
| 454 |
$prefilled_values = array();
|
| 455 |
$prefilled_values['postal_code'] = isset($edit['location']['postal_code']) ? trim($edit['location']['postal_code']) : trim($user->location['postal_code']);
|
| 456 |
$prefilled_values['country'] = isset($edit['location']['country']) ? $edit['location']['country'] : $user->location['country'];
|
| 457 |
|
| 458 |
//is the country supported for distance calculations?
|
| 459 |
$supported = function_exists('location_latlon_rough_'.$prefilled_values['country']);
|
| 460 |
|
| 461 |
//how should we sort?
|
| 462 |
if (!$edit && !$sort) { //if this is the default page
|
| 463 |
if ($prefilled_values['postal_code'] && $supported) { // if they've got a zip and are in a covered country
|
| 464 |
$sort = 'dist'; // sort by distance
|
| 465 |
} else {
|
| 466 |
$sort = 'alpha';
|
| 467 |
}
|
| 468 |
}
|
| 469 |
// if there is a country, but the country is not 'supported', or if there is no zip code sort by country
|
| 470 |
if (($prefilled_values['country'] && !$supported) || $prefilled_values['country'] && !$prefilled_values['postal_code']) {
|
| 471 |
$sort = 'country';
|
| 472 |
}
|
| 473 |
|
| 474 |
$cell[] = array('data'=>t('<strong>Find coaches near you:</strong>'), 'colspan'=>3);
|
| 475 |
$row[] = $cell;
|
| 476 |
unset($cell);
|
| 477 |
$cell[] = form_select('Country', 'location][country', $prefilled_values['country'], coach_get_countries(), NULL, 0, FALSE, $required);
|
| 478 |
$cell[] = form_textfield('Postal Code', 'location][postal_code', isset($prefilled_values['postal_code']) ? $prefilled_values['postal_code'] : '', 16, 16);
|
| 479 |
$cell[] = array('data' => form_submit(t('Show By Location')), 'align'=>'right');
|
| 480 |
$row[] = $cell;
|
| 481 |
unset($cell);
|
| 482 |
$cell[] = array('data'=>t('<strong>or show coaches alphabetically:</strong>'), 'colspan'=>2);
|
| 483 |
$cell[] = form_submit(t('Show Alphabetically'));
|
| 484 |
$row[] = $cell;
|
| 485 |
$form = theme('table', '', $row, array('style'=>'width:100%'));
|
| 486 |
$form = '<p>'.form($form).'<br /></p>';
|
| 487 |
|
| 488 |
// THE SEARCH RESULTS ------------------------------------------
|
| 489 |
|
| 490 |
$search_results = '';
|
| 491 |
//if ($edit) {
|
| 492 |
//$edit['location']['postal_code'] = trim($edit['location']['postal_code']);
|
| 493 |
//if (strlen($edit['location']['postal_code']) && strlen($edit['location']['country']) && $edit['location']['country'] != 'xx') {
|
| 494 |
|
| 495 |
switch ($sort) {
|
| 496 |
case 'dist' :
|
| 497 |
//print 'going by distance';
|
| 498 |
$info = "Coaches sorted by distance:";
|
| 499 |
$coaches = coach_by_distance($prefilled_values['postal_code'], $prefilled_values['country']);
|
| 500 |
break;
|
| 501 |
case 'country' :
|
| 502 |
$info = "Coaches sorted by country:";
|
| 503 |
$coaches = coach_by_alpha($prefilled_values['country']);
|
| 504 |
break;
|
| 505 |
case 'alpha' :
|
| 506 |
default :
|
| 507 |
$info = "Coaches sorted by name:";
|
| 508 |
$coaches = coach_by_alpha();
|
| 509 |
break;
|
| 510 |
}
|
| 511 |
|
| 512 |
$country_index = _location_get_iso3166_list();
|
| 513 |
|
| 514 |
foreach ($coaches as $row) {
|
| 515 |
//make dividers
|
| 516 |
switch ($sort) {
|
| 517 |
case 'dist' :
|
| 518 |
case 'country' :
|
| 519 |
if ($lastcoach->country != $row->country) {
|
| 520 |
//this should be themed
|
| 521 |
$search_results .= '<div class="coach-country-head"><h1>'.$country_index[$row->country].'</h1></div>';
|
| 522 |
}
|
| 523 |
break;
|
| 524 |
case 'alpha' :
|
| 525 |
default :
|
| 526 |
// no dividers...
|
| 527 |
break;
|
| 528 |
}
|
| 529 |
$lastcoach = $row;
|
| 530 |
|
| 531 |
$city_index_key = $row->country . $row->postal_code;
|
| 532 |
$result_location = array('street' => strlen($row->street) ? $row->street : '',
|
| 533 |
'additional' => strlen($row->additional) ? $row->additional : '',
|
| 534 |
'city' => strlen($row->city) ? $row->city : $postal_codes[$city_index_key]['city'],
|
| 535 |
'province' => strlen($row->province) ? $row->province : $postal_codes[$city_index_key]['province'],
|
| 536 |
'postal_code' => $row->postal_code,
|
| 537 |
'country' => $row->country,
|
| 538 |
'distance' => $row->distance,
|
| 539 |
'distance_unit' => $row->distance_unit,
|
| 540 |
'lat' => $row->latitude,
|
| 541 |
'lon' => $row->longitude
|
| 542 |
);
|
| 543 |
$extra = array();
|
| 544 |
if ($sort == 'dist') {
|
| 545 |
if ($result_location['postal_code'] == $edit['location']['postal_code'] && $result_location['country'] == $edit['location']['country']) {
|
| 546 |
$extra['distance'] = t('Result is <strong>also from %postal_code</strong>', array('%postal_code' => $result_location['postal_code']));
|
| 547 |
} elseif (!$result_location['lat']||!$result_location['lon']) {
|
| 548 |
$extra['distance'] = t('<em>Unable to calculate distance</em>');
|
| 549 |
} else {
|
| 550 |
if (round($result_location['distance']) != 1) {
|
| 551 |
$distance_unit = ($result_location['distance_unit'] == 'km') ? t($distance_unit) : t('miles');
|
| 552 |
}
|
| 553 |
$extra['distance'] = t('Approximately %distance from %location', array('%distance' => '<strong>'. round($result_location['distance']) .' '. $distance_unit .'</strong>', '%location' => $prefilled_values['postal_code']));
|
| 554 |
}
|
| 555 |
}
|
| 556 |
$snippet = '<div class="coach-listing">'.l(theme('image', $row->picture, $row->title, $row->title, array('style'=>'float:left; margin-right: 5px; margin-bottom: 5px')), url('node/'.$row->nid), array(), NULL, NULL, FALSE, TRUE);
|
| 557 |
$snippet .= "<strong class='coach-cert'>$row->cert</strong><br /><strong class='coach-loc'>$row->city, $row->province, ".$country_index[$row->country]."</strong></div>";
|
| 558 |
$snippet .= coach_word_trim($row->body, variable_get('coach_list_trim', 20));
|
| 559 |
//$snippet .= ($row->teaser != $row->body) ? '...' : '';
|
| 560 |
$snippet .= '... <span class="coach-more">'.l(t('(more)'), url('node/'.$row->nid)).'</span>';
|
| 561 |
if ($extra) {$snippet .= '<div class="coach-dist">'.$extra['distance'].'</div>';}
|
| 562 |
$snippet .= '<br clear="all" />';
|
| 563 |
|
| 564 |
$item = array('link' => url('node/'. $row->nid),
|
| 565 |
'title' => $row->title,
|
| 566 |
'snippet' => $snippet,
|
| 567 |
);
|
| 568 |
$search_results .= theme('search_item', $item, 'coach');
|
| 569 |
}
|
| 570 |
if (!strlen($search_results)) {
|
| 571 |
$search_results .= theme('box', t('Your search yielded no results.'), '');
|
| 572 |
}
|
| 573 |
else {
|
| 574 |
$from = $_GET['from'] ? $_GET['from'] : '0';
|
| 575 |
$coach_limit = variable_get('coach_limit', 15);
|
| 576 |
$total = $GLOBALS['pager_total'][0];
|
| 577 |
$to = ($from+$coach_limit)<$total ? ($from+$coach_limit) : $total ;
|
| 578 |
$search_stats = "<div>Showing ".($from+1)." - ".$to." of ".$total."<br /><br /></div>";
|
| 579 |
$search_results = '<dl class="search-results">'.$search_stats.$search_results.'</dl>';
|
| 580 |
$search_results = theme('box', t($info), $search_results);
|
| 581 |
$search_results .= theme('pager', NULL, $coach_limit, 0, $edit['location']);
|
| 582 |
}
|
| 583 |
//}
|
| 584 |
//}
|
| 585 |
return $form . $search_results;
|
| 586 |
}
|
| 587 |
*/
|
| 588 |
|
| 589 |
function coach_cert_role($uid) {
|
| 590 |
$user = user_load(array('uid' => $uid));
|
| 591 |
$roles = $user->roles;
|
| 592 |
if (in_array('cflc3', $roles)) {
|
| 593 |
return "CFLC III";
|
| 594 |
}
|
| 595 |
elseif(in_array('cflc2', $roles)) {
|
| 596 |
return "CFLC II";
|
| 597 |
}
|
| 598 |
elseif(in_array('cflc', $roles)) {
|
| 599 |
return "CFLC";
|
| 600 |
}
|
| 601 |
}
|
| 602 |
|
| 603 |
function coach_word_trim($data, $count) {
|
| 604 |
$regex = "/(.*?[\s]){0,".$count."}/";
|
| 605 |
$matches = array();
|
| 606 |
preg_match($regex, $data, $matches);
|
| 607 |
return trim($matches[0]);
|
| 608 |
}
|
| 609 |
|
| 610 |
// return a list of countries used in coach table
|
| 611 |
// key is iso code and value is country
|
| 612 |
|
| 613 |
function coach_get_countries() {
|
| 614 |
$sql = 'SELECT country FROM {coach} c INNER JOIN {location} l ON c.nid = l.oid AND l.type = "node" GROUP BY country ORDER BY country';
|
| 615 |
$res = db_query($sql);
|
| 616 |
$countries = _location_get_iso3166_list();
|
| 617 |
$list = array(''=>'Choose...');
|
| 618 |
while ($row = db_fetch_object($res)) {
|
| 619 |
$list[$row->country] = $countries[$row->country];
|
| 620 |
}
|
| 621 |
return $list;
|
| 622 |
}
|
| 623 |
|
| 624 |
/**
|
| 625 |
* Returns an alphabetical array of coach objects
|
| 626 |
* if $country is defined, coaches from that country are returned first
|
| 627 |
*
|
| 628 |
*/
|
| 629 |
|
| 630 |
/*
|
| 631 |
function coach_by_alpha($country = FALSE) {
|
| 632 |
if ($country) {
|
| 633 |
$sql = 'SELECT n.nid AS nid, a.postal_code AS postal_code, a.city AS city, a.province AS province, a.country AS country, a.latitude AS latitude, a.longitude AS longitude, n.title AS title, n.teaser AS teaser, n.body AS body, n.type AS type, n.created AS created, n.uid AS uid, u.name AS name, u.picture AS picture, c.firstname AS firstname, c.lastname AS lastname, c.cert AS cert, c.cuid AS cuid, IF(a.country = \''.$country.'\', 0, 1) AS mycountry FROM {location} a INNER JOIN {node} n ON a.oid = n.nid INNER JOIN {coach} c ON n.nid = c.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = \'coach\' AND a.type = "node" ORDER BY mycountry, country, lastname ASC';
|
| 634 |
} else {
|
| 635 |
$sql = 'SELECT n.nid AS nid, a.postal_code AS postal_code, a.city AS city, a.province AS province, a.country AS country, a.latitude AS latitude, a.longitude AS longitude, n.title AS title, n.teaser AS teaser, n.body AS body, n.type AS type, n.created AS created, n.uid AS uid, u.name AS name, u.picture AS picture, c.firstname AS firstname, c.lastname AS lastname, c.cert AS cert, c.cuid AS cuid, IF(a.latitude<=>NULL, 1, 0) AS gotlat FROM {location} a INNER JOIN {node} n ON a.oid = n.nid INNER JOIN {coach} c ON n.nid = c.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = \'coach\' AND a.type = \'node\' ORDER BY lastname ASC';
|
| 636 |
}
|
| 637 |
$count_sql = 'SELECT COUNT(*) FROM {location} a INNER JOIN {node} n ON a.oid = n.nid INNER JOIN {coach} c ON n.nid = c.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = \'coach\' AND a.type = \'node\'';
|
| 638 |
$res = pager_query($sql, variable_get('coach_limit', 15), 0, $count_sql);
|
| 639 |
// for each one... do a distance check and stick 'em into an array with the distance
|
| 640 |
$coaches = array();
|
| 641 |
while ($row = db_fetch_object($res)) {
|
| 642 |
$coaches[] = $row;
|
| 643 |
}
|
| 644 |
// display
|
| 645 |
return $coaches;
|
| 646 |
}
|
| 647 |
*/
|
| 648 |
|
| 649 |
function coach_by_alpha_better($country = NULL) {
|
| 650 |
if (!trim($country)) {
|
| 651 |
$country = 'us';
|
| 652 |
}
|
| 653 |
if ($country) {
|
| 654 |
return pager_query('SELECT n.nid, IF(l.country = "'.$country.'", 0, 1) AS mycountry FROM {node} n INNER JOIN {coach} c ON c.nid = n.nid INNER JOIN {location} l ON l.oid = n.nid AND l.type = "node" ORDER BY mycountry, l.country, c.lastname ASC', 20);
|
| 655 |
}
|
| 656 |
else {
|
| 657 |
return pager_query('SELECT n.nid FROM {node} n INNER JOIN {coach} c ON c.nid = n.nid INNER JOIN {location} l ON l.oid = n.nid AND l.type = "node" ORDER BY l.country, c.lastname ASC', 20);
|
| 658 |
}
|
| 659 |
}
|
| 660 |
|
| 661 |
/**
|
| 662 |
* Returns an array of coach objects, listed by distance from $zip
|
| 663 |
* $country is required as well
|
| 664 |
*
|
| 665 |
*/
|
| 666 |
/*
|
| 667 |
function coach_by_distance($zip, $country) {
|
| 668 |
$mylatlon = location_latlon_rough(array('postal_code'=>$zip, 'country'=>$country, 'mi'));
|
| 669 |
// get all coach nodes with each's lat/long
|
| 670 |
$distsql = earth_distance_sql($mylatlon['lon'], $mylatlon['lat']);
|
| 671 |
$sql = 'SELECT n.nid AS nid, a.postal_code AS postal_code, a.city AS city, a.province AS province, a.country AS country, a.latitude AS latitude, a.longitude AS longitude, n.title AS title, n.teaser AS teaser, n.body as body, n.type AS type, n.created AS created, n.uid AS uid, u.name AS name, u.picture AS picture, c.firstname AS firstname, c.lastname AS lastname, c.cert AS cert, c.cuid AS cuid, IF(a.latitude<=>NULL, 1, 0) AS gotlat, '.$distsql.' AS distance FROM {location} a INNER JOIN {node} n ON a.oid = n.nid INNER JOIN {coach} c ON n.nid = c.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = \'coach\' AND a.type = \'node\' ORDER BY gotlat, distance, country, lastname ASC';
|
| 672 |
$count_sql = 'SELECT COUNT(*) FROM {location} a INNER JOIN {node} n ON a.oid = n.nid INNER JOIN {coach} c ON n.nid = c.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = \'coach\' AND a.type = \'node\'';
|
| 673 |
return pager_query($sql, variable_get('coach_limit', 15), 0, $count_sql);
|
| 674 |
// for each one... do a distance check and stick 'em into an array with the distance
|
| 675 |
}
|
| 676 |
*/
|
| 677 |
|
| 678 |
function coach_by_distance_better($zip, $country) {
|
| 679 |
$mylatlon = location_latlon_rough(array('postal_code'=>$zip, 'country'=>$country, 'mi'));
|
| 680 |
// get all coach nodes with each's lat/long
|
| 681 |
if ($mylatlon != NULL) {
|
| 682 |
$distsql = earth_distance_sql($mylatlon['lon'], $mylatlon['lat']);
|
| 683 |
$sql = 'SELECT n.nid AS nid, c.lastname AS lastname, a.postal_code AS postal_code, a.city AS city, a.province AS province, a.country AS country, a.latitude AS latitude, a.longitude AS longitude, IF(a.latitude<=>NULL, 1, 0) AS gotlat, '.$distsql.' AS distance FROM {location} a INNER JOIN {node} n ON a.oid = n.nid INNER JOIN {coach} c ON n.nid = c.nid WHERE n.type = \'coach\' AND a.type = \'node\' ORDER BY gotlat, distance, country, lastname ASC';
|
| 684 |
}
|
| 685 |
else {
|
| 686 |
$sql = 'SELECT n.nid AS nid, c.lastname AS lastname, a.postal_code AS postal_code, a.city AS city, a.province AS province, a.country AS country, a.latitude AS latitude, a.longitude AS longitude, IF(a.country="'.$country.'", 0, 1) AS samecountry FROM {location} a INNER JOIN {node} n ON a.oid = n.nid INNER JOIN {coach} c ON n.nid = c.nid WHERE n.type = \'coach\' ORDER BY samecountry, country, lastname ASC';
|
| 687 |
}
|
| 688 |
$count_sql = 'SELECT COUNT(*) FROM {location} a INNER JOIN {node} n ON a.oid = n.nid WHERE n.type = \'coach\' AND a.type = \'node\'';
|
| 689 |
|
| 690 |
return pager_query($sql, 20, 0, $count_sql);
|
| 691 |
}
|
| 692 |
|
| 693 |
// I don't know if this is used...
|
| 694 |
|
| 695 |
function theme_coach_search($header, $rows, $attributes = NULL) {
|
| 696 |
$output = '<table'. drupal_attributes($attributes) .">\n";
|
| 697 |
|
| 698 |
// Format the table header:
|
| 699 |
if (is_array($header)) {
|
| 700 |
$ts = tablesort_init($header);
|
| 701 |
$output .= ' <tr>';
|
| 702 |
foreach ($header as $cell) {
|
| 703 |
$cell = tablesort_header($cell, $header, $ts);
|
| 704 |
$output .= _theme_table_cell($cell, 1);
|
| 705 |
}
|
| 706 |
$output .= " </tr>\n";
|
| 707 |
}
|
| 708 |
|
| 709 |
// Format the table rows:
|
| 710 |
if (is_array($rows)) {
|
| 711 |
foreach ($rows as $number => $row) {
|
| 712 |
$attributes = array();
|
| 713 |
|
| 714 |
// Check if we're dealing with a simple or complex row
|
| 715 |
if (isset($row['data'])) {
|
| 716 |
foreach ($row as $key => $value) {
|
| 717 |
if ($key == 'data') {
|
| 718 |
$cells = $value;
|
| 719 |
}
|
| 720 |
else {
|
| 721 |
$attributes[$key] = $value;
|
| 722 |
}
|
| 723 |
}
|
| 724 |
}
|
| 725 |
else {
|
| 726 |
$cells = $row;
|
| 727 |
}
|
| 728 |
|
| 729 |
// Add light/dark class
|
| 730 |
$class = ($number % 2 == 1) ? 'light': 'dark';
|
| 731 |
if (isset($attributes['class'])) {
|
| 732 |
$attributes['class'] .= ' '. $class;
|
| 733 |
}
|
| 734 |
else {
|
| 735 |
$attributes['class'] = $class;
|
| 736 |
}
|
| 737 |
|
| 738 |
// Build row
|
| 739 |
$output .= ' <tr'. drupal_attributes($attributes) .'>';
|
| 740 |
$i = 0;
|
| 741 |
foreach ($cells as $cell) {
|
| 742 |
$cell = tablesort_cell($cell, $header, $ts, $i++);
|
| 743 |
$output .= _theme_table_cell($cell, 0);
|
| 744 |
}
|
| 745 |
$output .= " </tr>\n";
|
| 746 |
}
|
| 747 |
}
|
| 748 |
|
| 749 |
$output .= "</table>\n";
|
| 750 |
return $output;
|
| 751 |
}
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
/**
|
| 757 |
* Don't know if this is doing anything...
|
| 758 |
*
|
| 759 |
*/
|
| 760 |
|
| 761 |
function coach_search_item($item) {
|
| 762 |
$output = ' <div class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></div>';
|
| 763 |
$info = array();
|
| 764 |
if (is_array($item['extra'])) {
|
| 765 |
$info = array_merge($info, $item['extra']);
|
| 766 |
}
|
| 767 |
$output .= ' <div>'. ($item['snippet'] ? '<p>'. $item['snippet'] . '</p>' : '') . '<p>' . implode(' - ', $info) .'</p></div>';
|
| 768 |
return $output;
|
| 769 |
}
|
| 770 |
|
| 771 |
/**
|
| 772 |
* Don't know if this is doing anything either...
|
| 773 |
*
|
| 774 |
*/
|
| 775 |
function theme_coach_list($node) {
|
| 776 |
$output = '<div class="coach_list">';
|
| 777 |
if ($node->picture) {
|
| 778 |
$output .= theme('image', $node->picture, 'alt', 'title', 'style="float:left; margin-right: 5px; margin-bottom: 5px; margin-top: 3px;"');
|
| 779 |
}
|
| 780 |
if ($node->cert) {
|
| 781 |
$output .= '<div>certifications: '.$node->cert.'</div>';
|
| 782 |
}
|
| 783 |
$output .= '</div><br /></div>';
|
| 784 |
return $output;
|
| 785 |
}
|
| 786 |
|
| 787 |
?>
|