[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/custom/affiliate/ -> affiliate.module (source)

   1  <?php
   2  // $Id$
   3  /**
   4   * @file
   5   *  Handles affiliate registration
   6   *  
   7   */
   8  function debug_local($message) {
   9    return;
  10    watchdog("affiliate",$message ,$variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL);
  11  }
  12  
  13  /**
  14   * Implementation of hook_perm().
  15   */
  16  
  17  function affiliate_perm() {
  18    debug_local("In affiliate_perm()");
  19    return array(
  20                 'administer affiliate',
  21                 'create affiliate profile',
  22                 'view own affiliate profile',
  23                 'view any affiliate profile',
  24                 'edit own affiliate profile',
  25                 'edit any affiliate profile',
  26                 );
  27  }
  28  
  29  /**
  30   * Implementation of hook_menu().
  31   */
  32  
  33  function affiliate_menu() {
  34    debug_local("In affiliate_menu()");
  35  
  36    $items['admin/settings/affiliate'] = array(
  37      'title' => 'Affiliate settings',
  38      'description' => 'Affiliate settings',
  39      'page callback' => 'drupal_get_form',
  40      'page arguments' => array('affiliate_admin_settings'),
  41      'access arguments' => array('administer site configuration'),
  42      'type' => MENU_NORMAL_ITEM,
  43      'file' => 'affiliate.admin.inc',
  44    );    
  45    $items['affiliate/register'] = array(
  46      'title' => 'Affiliate agreement',
  47      'page callback' => 'affiliate_page',
  48      'page arguments' => array('user selected'),
  49      'access callback' => '_register_access',
  50      'access arguments' => array('create affiliate profile'),
  51      'type' => MENU_CALLBACK,
  52    );
  53    $items['affiliate/show'] = array(
  54      'title' => 'Display Affiliate agreement',
  55      'page callback' => 'affiliate_show',
  56      'page arguments' => array(''),
  57      'access callback' => '_show_access',
  58      'access arguments' => array('view any affiliate profile'),
  59      'type' => MENU_CALLBACK,
  60    );
  61    $items['affiliate/show/%'] = array(
  62      'title' => 'Display Affiliate agreement',
  63      'page callback' => 'affiliate_show',
  64      'page arguments' => array(2),
  65      'access callback' => '_show_access',
  66      'access arguments' => array('view own affiliate profile'),
  67      'type' => MENU_CALLBACK,
  68    );
  69    return $items;
  70  }
  71  
  72  /**
  73   *  Access callbacks
  74   */
  75  
  76  function _register_access($account) {
  77    global $user;
  78    debug_local("In _register_access()");
  79    
  80    if ( $user->uid == 1 ) {
  81      return TRUE;
  82    }
  83    $roles = $user->roles;
  84    // allow only anonymous or authenticated user access
  85    $count = count(array_diff($roles,array('authenticated user','anonymous user')));
  86    if ( $count > 0 ) {
  87      debug_local("count of roles: $count");
  88      return FALSE;
  89    }
  90    return TRUE;
  91  }
  92  
  93  function _show_access($account) {
  94    
  95    return TRUE;
  96  }
  97  
  98  
  99  /**
 100   * Menu callback.
 101   * Called when user goes to affiliate/register
 102   */
 103  function affiliate_page($arg = '') {
 104    debug_local("In affiliate_page()");
 105    global $user;
 106    require_once(drupal_get_path('module', 'profile') .'/profile.module');
 107  
 108    if ($user->uid > 0 ) { // user is logged in
 109      $count = db_result(db_query('SELECT COUNT(*) FROM {affiliate} WHERE uid = %d',$user->uid));
 110      if ($count) { // the user has already registered as affiliate
 111        return '<div id="content-messages" class="content-messages block">
 112  <div id="content-messages-inner" class="content-messages-inner inner clearfix">
 113  <div class="messages error">You have already submitted an application.  If this request is for another service please log out and register another account.</div></div></div>';
 114      }
 115        
 116  
 117      if ($arg == 'new') {
 118        if( preg_match("/Affiliate/",$user->profile_servreq) ) {
 119          $output = drupal_get_form('affiliate_form');
 120          return $output;
 121        }
 122        else {
 123          drupal_goto('/', NULL, NULL);
 124        }
 125      }
 126      else {
 127        $output = drupal_get_form('affiliate_form');
 128        return $output;        
 129      }
 130    }
 131    else {
 132      $output = '<div id="content-messages" class="content-messages block">
 133  <div id="content-messages-inner" class="content-messages-inner inner clearfix">
 134  <div class="messages error">
 135  Please ' . l('Create an account','user/register') . ' before filling out the Affiliate application. </div></div></div>';
 136      return $output;
 137    }
 138  }
 139  
 140  /**
 141   * Define a form.
 142   */
 143  function affiliate_form($form_id, $form_state = NULL) {
 144  
 145    global $user;
 146    // dvr($user);
 147    debug_local("In affiliate_form()\n");
 148    $form['#cache'] = TRUE;
 149    $form['#attributes'] = array(
 150      'enctype' => "multipart/form-data");
 151    $form['#submit'][] = 'affiliate_form_submit';
 152    $form['#redirect'] = '/';
 153    $form['aff_accept'] = array(
 154      '#type' => 'fieldset',
 155      '#title' => t('Acceptance'),
 156      '#description' => t('Check to accept the conditions stated in the Release and Clearance above.'),
 157      '#required' => TRUE,
 158    );
 159  
 160    $form['aff_accept'] ['affiliate_accept'] = array(
 161      '#type' => 'checkbox',
 162      '#title' => t('I accept the conditions.'),
 163      '#description' => t(''),
 164    );
 165    
 166    $form['aff_ident'] = array(
 167      '#type' => 'fieldset',
 168      '#title' => t('Affiliate Identification'),
 169      '#description' => t(''),
 170    );
 171  
 172    $form['aff_ident'] ['affiliate_type'] = array(
 173      '#type' => 'select',
 174      '#title' => t('Type of Service'),
 175      '#options' => array(
 176        ''                           => t(''),                  
 177        'radio reading service'      => t('Radio Reading Service'),
 178        'cable system'               => t('Cable System'),
 179        'telecommunications entity'  => t('Telecommunications Entity'),
 180        'other'                      => t('Other'),
 181        ),                 
 182      '#description' => t('Please select the type of service you provide.'),
 183      '#required' => TRUE,
 184    );
 185  
 186    $form['aff_ident'] ['affiliate_name'] = array(
 187      '#type' => 'textfield',
 188      '#title' => t('Name of Service'),
 189      '#size' => 80,
 190      '#description' => t('Please enter the name of your broadcasting service'),
 191      '#required' => TRUE,
 192    );
 193      
 194    $form['aff_ident'] ['affiliate_address'] = array(
 195      '#type' => 'textarea',
 196      '#title' => t('Address of Service'),
 197      '#size' => 80,
 198      '#description' => t('Please enter your mailing address (Unit, Street, City, Country, if applicable)'),
 199      '#required' => TRUE,
 200    );
 201    
 202    $form['aff_ident'] ['affiliate_postalcode'] = array(
 203      '#type' => 'textfield',
 204      '#title' => t('Postal Code'),
 205      '#size' => 8,
 206      '#description' => t('Please enter your postal code (ZIP)'),
 207      '#required' => TRUE,
 208    );
 209  
 210    $form['aff_ident'] ['affiliate_fax'] = array(
 211      '#type' => 'textfield',
 212      '#title' => t('Fax'),
 213      '#size' => 16,
 214      '#description' => t('Please enter your Fax number (if any)'),
 215    );
 216  
 217    $form['aff_ident'] ['affiliate_url'] = array(
 218      '#type' => 'textfield',
 219      '#title' => t('Web-site URL'),
 220      '#size' => 80,
 221      '#default_value' => 'http://',
 222      '#description' => t('Please enter your web-site URL (if any)'),
 223    );
 224      
 225    $form['aff_ident'] ['affiliate_logo'] = array(
 226      '#type' => 'image_upload_element',
 227      '#title' => t('Upload your LOGO (Optional)'),
 228      '#size' => 40,
 229      '#description' => '',
 230    );
 231    
 232    $form['aff_primary'] = array(
 233      '#type' => 'fieldset',
 234      '#title' => t('Primary Contact Information'),
 235      '#description' => t(''),
 236    );
 237  
 238    $form['aff_primary'] ['affiliate_contact_title'] = array(
 239      '#type' => 'textfield',
 240      '#title' => t('Title'),
 241      '#size' => 80,
 242      '#description' => t('Please enter the title of the primary contact'),
 243      '#required' => TRUE,
 244    );
 245    
 246    $form['aff_primary'] ['affiliate_contact_name'] = array(
 247      '#type' => 'textfield',
 248      '#title' => t('Name'),
 249      '#size' => 80,
 250      '#description' => t('Please enter the full name of the primary contact'),
 251      '#default_value' => $user->profile_fullname,
 252      '#required' => TRUE,
 253    );
 254    
 255    $form['aff_primary'] ['affiliate_contact_telephone'] = array(
 256      '#type' => 'textfield',
 257      '#title' => t('Telephone'),
 258      '#size' => 16,
 259      '#description' => t('Please enter primary contact telephone number:'),
 260      '#required' => TRUE,
 261    );
 262  
 263    $form['aff_primary'] ['affiliate_contact_extension'] = array(
 264      '#type' => 'textfield',
 265      '#title' => t('Extension'),
 266      '#size' => 16,
 267      '#description' => t('Please enter primary contact extension (if not direct-dial)'),
 268    );
 269      
 270    $form['aff_primary'] ['affiliate_contact_email'] = array(
 271      '#type' => 'textfield',
 272      '#title' => t('E-mail'),
 273      '#size' => 80,
 274      '#default_value' => $user->mail,
 275      '#description' => t('Please enter the primary contact e-mail address.'),
 276      '#required' => TRUE,
 277    );
 278    
 279     $form['aff_survey'] = array(
 280      '#type' => 'fieldset',
 281      '#title' => t('Usage Survey - Part 1'),
 282      '#description' => 'Please answer all the questions in this part.  Enter 0 (zero) if not known or not applicable.  Please refer to <a href="http://iaais.org/ListenerCountGuide05.htm">IAAIS Guidelines</a> for information on determining the answers to this section.',
 283    );
 284  
 285    $form['aff_survey'] ['affiliate_survey_individuals'] = array(
 286      '#type' => 'textfield',
 287      '#title' => t('Individuals'),
 288      '#size' => 9,
 289      '#description' => t('How many individuals do you serve?'),
 290      '#required' => TRUE,
 291    );
 292  
 293    $form['aff_survey'] ['affiliate_survey_hosp'] = array(
 294      '#type' => 'textfield',
 295      '#title' => t('Hospitals'),
 296      '#size' => 9,
 297      '#description' => t('How many hospitals do you serve?'),
 298      '#required' => TRUE,
 299    );
 300  
 301    $form['aff_survey'] ['affiliate_survey_lic_beds'] = array(
 302      '#type' => 'textfield',
 303      '#title' => t('Licensed beds'),
 304      '#size' => 9,
 305      '#description' => t('How many licensed beds do you serve?'),
 306      '#required' => TRUE,
 307    );
 308  
 309    $form['aff_survey'] ['affiliate_survey_nurse_homes'] = array(
 310      '#type' => 'textfield',
 311      '#title' => t('Nursing Homes'),
 312      '#size' => 9,
 313      '#description' => t('How many nursing homes do you serve?'),
 314      '#required' => TRUE,
 315    );
 316  
 317    $form['aff_survey'] ['affiliate_survey_nurse_or_resid_units'] = array(
 318      '#type' => 'textfield',
 319      '#title' => t('Nursing Homes or Resid. units'),
 320      '#size' => 9,
 321      '#description' => t('How many nursing home or residential home units do you serve?'),
 322      '#required' => TRUE,
 323    );
 324  
 325    $form['aff_survey'] ['markup1'] = array(
 326      '#type' => 'markup',
 327      '#value' => '<div style="margin-top: 1em; padding-left: 2em; border: 1px solid rgb(166, 164, 152); width: 15em;">',
 328    );
 329    
 330    $form['aff_survey'] ['affiliate_survey_have_website'] = array(
 331      '#type' => 'radios',
 332      '#title' => t('Have Website'),
 333      '#options' => array( t('No'), t('Yes') ),
 334      '#description' => t('Do you have a website?'),
 335      '#default_value' => 0,
 336      '#required' => TRUE,
 337    );
 338  
 339    $form['aff_survey'] ['markup2'] = array(
 340      '#type' => 'markup',
 341      '#value' => '</div>',
 342    );
 343  
 344    $form['aff_survey'] ['affiliate_survey_website_users'] = array(
 345      '#type' => 'textfield',
 346      '#title' => t('Website users'),
 347      '#size' => 9,
 348      '#description' => t('How many registered Website users do you serve?'),
 349    );
 350  
 351    $form['aff_survey'] ['affiliate_survey_total_users'] = array(
 352      '#type' => 'textfield',
 353      '#title' => t('Total Users'),
 354      '#size' => 9,
 355      '#description' => t('How many total users do you serve?'),
 356      '#required' => TRUE,
 357    );
 358       
 359    $form['aff_survey2'] = array(
 360      '#type' => 'fieldset',
 361      '#title' => t('Usage Survey - Part 2'),
 362      '#description' => 'How will you be accessing the GATEWAVE feed? (Check all applicable methods.)',
 363    );
 364  
 365    $form['aff_survey2'] ['markup1'] = array(
 366      '#type' => 'markup',
 367      '#value' => '<div style="margin-top: 1em; padding-left: 2em; border: 1px solid rgb(166, 164, 152); width: 15em;">',
 368    );
 369  
 370    $form['aff_survey2'] ['affiliate_survey_acc_sat'] = array(
 371      '#type' => 'checkbox',
 372      '#title' => t('Satellite Feed'),
 373      '#description' => t(''),
 374      '#required' => TRUE,
 375    );
 376     
 377    $form['aff_survey2'] ['affiliate_survey_acc_stream'] = array(
 378      '#type' => 'checkbox',
 379      '#title' => t('Internet Stream'),
 380      '#description' => t(''),
 381      '#required' => TRUE,
 382    );
 383     
 384    $form['aff_survey2'] ['affiliate_survey_acc_ftp'] = array(
 385      '#type' => 'checkbox',
 386      '#title' => t('FTP download'),
 387      '#description' => t(''),
 388      '#required' => TRUE,
 389    );
 390  
 391    $form['aff_survey2'] ['markup2'] = array(
 392      '#type' => 'markup',
 393      '#value' => '</div>&nbsp;<br>How many programs will you be using daily?',
 394    );
 395  
 396    $form['aff_survey2'] ['affiliate_survey_prog_daily'] = array(
 397      '#type' => 'textfield',
 398      '#title' => t('Programs'),
 399      '#description' => t(''),
 400      '#size' => 9,
 401      '#required' => TRUE,
 402    );
 403  
 404    $form['aff_survey2'] ['markup3'] = array(
 405      '#type' => 'markup',
 406      '#value' => 'Name those, if possible.',
 407    );
 408  
 409    $form['aff_survey2'] ['affiliate_survey_prog_names'] = array(
 410      '#type' => 'textarea',
 411      '#description' => t(''),
 412      '#size' => 80,
 413    );
 414  
 415    $form['aff_survey2'] ['markup4'] = array(
 416      '#type' => 'markup',
 417      '#value' => 'What changes/additions would you like to see to the schedule?',
 418    );
 419  
 420    $form['aff_survey2'] ['affiliate_survey_additions'] = array(
 421      '#type' => 'textarea',
 422      '#description' => t(''),
 423      '#size' => 80,
 424    );
 425  
 426    $form['aff_survey2'] ['markup5'] = array(
 427      '#type' => 'markup',
 428      '#value' => 'Any other suggestions?',
 429    );
 430  
 431    $form['aff_survey2'] ['affiliate_survey_comments'] = array(
 432      '#type' => 'textarea',
 433      '#description' => t(''),
 434      '#size' => 80,
 435    );
 436  
 437    $form['submit'] = array(
 438      '#type' => 'submit',
 439      '#value' => t('Submit')
 440    );
 441    
 442    return $form;
 443  }
 444  
 445  /**
 446   *  Helper function for validation
 447   **/
 448  function aff_validate(&$form_state, $form, $aff_fieldset) {
 449    
 450    $message = array( 'aff_chk_numeric' => 'must be numeric',
 451                      'aff_chk_telep' => 'must be a valid telephone number (10 digits)', ); 
 452    $aff_fs = $aff_fieldset['name'];
 453    $aff_fields = $aff_fieldset['fields'];
 454    foreach ($aff_fields as $key => $val_function ) {
 455      $val = $form_state['values'][$key];
 456      if ($val && $val_function) {
 457        if (!$val_function($form_state['values'][$key])) {
 458          form_set_error($key, t('@field field @messg.', 
 459                                 array(
 460                                       '@field' => $form[$aff_fs][$key]['#title'],
 461                                       '@messg' => $message[$val_function])));
 462        }
 463      }
 464    }
 465  }
 466  /**
 467   *  Check for valid telephone no & reformat
 468   */
 469  function aff_chk_telep(&$arg) {
 470    $arg_r = $arg;
 471    $arg_r = preg_replace('/[()\-\s]/','',$arg);
 472    if (strlen($arg_r) == 10 ) {
 473      return is_numeric($arg_r);
 474    }
 475    else {
 476      return FALSE;
 477    } 
 478  }  
 479  function aff_chk_numeric(&$arg) {
 480    $arg_r = preg_replace('/^\s+/','', $arg);
 481    $arg_r = preg_replace('/\s+$/','', $arg_r);
 482    return is_numeric($arg_r);
 483  }
 484                             
 485  /**
 486   * Validate the form.
 487   */
 488  function affiliate_form_validate($form_id, &$form_state) {
 489    debug_local("In affiliate_form_validate()");
 490    $form_build_id = 'form_'.$form_id['#post']['form_build_id'];
 491    if ($cached = cache_get($form_build_id, 'cache_form')) {
 492      $form = $cached->data;
 493    }
 494    
 495    // Validate Acceptance  #1
 496    
 497    if ($form_state['values']['affiliate_accept'] == 0) {
 498      form_set_error('user_name', t('The Acceptance box must be checked.'));
 499    }
 500    // Validate Acceptance  #2
 501    // fields
 502    $aff_fs = 'aff_ident';
 503    $aff_fieldset = array('name' => 'aff_ident',
 504                          'fields' => array(
 505                                            'affiliate_type' => '',
 506                                            'affiliate_name' => '',
 507                                            'affiliate_address' => '',
 508                                            'affiliate_postalcode',
 509                                            'affiliate_fax' => 'aff_chk_telep',
 510                                            'affiliate_url' => '',
 511                                            ));
 512    
 513    $val = $form_state['values']['affiliate_url'];
 514    if (!empty($val) && $val != 'http://' && !valid_url($val, TRUE)) {
 515      form_set_error('affiliate_url', t('The URL %val is not valid.',
 516                                        array('%val' => $val)));
 517    }
 518    aff_validate($form_state, $form, $aff_fieldset);
 519    debug_local("verified #2");
 520          
 521    // Validate Acceptance  #3
 522    // fields
 523    $aff_fs = 'aff_primary';
 524    $aff_fieldset = array('name' => 'aff_primary',
 525                          'fields' => array(
 526                                            'affiliate_contact_title' => '',
 527                                            'affiliate_contact_name' => '',
 528                                            'affiliate_contact_telephone' => 'aff_chk_telep',
 529                                            'affiliate_contact_extension' => 'aff_chk_numeric',
 530                                            'affiliate_contact_email' => '',
 531                                            ));
 532  
 533    $val = $form_state['values']['affiliate_contact_email'];
 534    if (!empty($val) && !valid_email_address($val)) {
 535      form_set_error('affiliate_contact_email', t('The E-mail address %val is not valid.',
 536                                        array('%val' => $val)));
 537    }
 538    aff_validate($form_state, $form, $aff_fieldset);
 539    debug_local("verified #3");
 540   
 541  
 542    // Validate Acceptance  #4
 543    // fields
 544    $aff_fs = 'aff_survey';
 545    $aff_fieldset = array('name' => 'aff_survey',
 546                          'fields' => array(
 547                                            'affiliate_survey_individuals' => 'aff_chk_numeric',
 548                                            'affiliate_survey_hosp' => 'aff_chk_numeric',
 549                                            'affiliate_survey_lic_beds' => 'aff_chk_numeric',
 550                                            'affiliate_survey_nurse_homes' => 'aff_chk_numeric',
 551                                            'affiliate_survey_nurse_or_resid_units' => 'aff_chk_numeric',
 552                                            'affiliate_survey_have_website' => '',
 553                                            'affiliate_survey_website_users' => 'aff_chk_numeric',
 554                                            'affiliate_survey_total_users' => 'aff_chk_numeric',
 555                                            ));
 556  
 557    aff_validate($form_state, $form, $aff_fieldset);
 558     if ($form_state['values']['affiliate_survey_have_website'] == 1 &&
 559        !$form_state['values']['affiliate_survey_website_users'] ) {
 560         form_set_error('affiliate_survey_website_users', t('Website users must be specified.'));
 561    }
 562                            
 563    debug_local("verified #4");
 564    
 565    // Validate Acceptance  #5
 566    // fields
 567    $aff_fs = 'aff_survey2';
 568    $aff_fieldset = array('name' => 'aff_survey2',
 569                          'fields' => array(
 570                                            'affiliate_survey_acc_sat' => '',
 571                                            'affiliate_survey_acc_ftp' => '',
 572                                            'affiliate_survey_acc_stream' => '',
 573                                            'affiliate_survey_prog_daily' => 'aff_chk_numeric',
 574                                            'affiliate_survey_prog_names' => '',
 575                                            'affiliate_survey_additions' => '',
 576                                            'affiliate_survey_comments' => '',
 577                                            ));
 578    aff_validate($form_state, $form, $aff_fieldset);
 579    debug_local( 'Access checkboxes  |'. $form_state['values']['affiliate_survey_acc_sat'] . '|' . $form_state['values']['affiliate_survey_acc_ftp'] . '|' . $form_state['values']['affiliate_survey_acc_stream']);
 580    if(!($form_state['values']['affiliate_survey_acc_sat'] ||
 581         $form_state['values']['affiliate_survey_acc_ftp'] ||
 582         $form_state['values']['affiliate_survey_acc_stream'])) {
 583      form_set_error('affiliate_survey_acc_sat', t('Access methods must be specified.'));
 584    }
 585    debug_local("verified #5");
 586  }
 587      
 588  
 589  /**
 590   * Handle post-validation form submission.
 591   */
 592  function affiliate_form_submit($form_id, &$form_state) {
 593    debug_local("In affiliate_form_submit()");
 594    global $user;
 595    require_once(drupal_get_path('module', 'upload_element') .'/upload_element.module');  
 596  
 597    /*print "<pre>FORM_STATE<br>";
 598    var_dump($form_state);
 599    print '</pre>'; */
 600  
 601    // Post completed form to database
 602  
 603    // affiliate
 604    
 605    $fld_info   = array('uid',
 606                        'affiliate_application_status',
 607                        'affiliate_accept',
 608                        'affiliate_type',
 609                        'affiliate_name',
 610                        'affiliate_address',
 611                        'affiliate_postalcode',
 612                        'affiliate_fax',
 613                        'affiliate_url',                      
 614                        'affiliate_contact_title',
 615                        'affiliate_contact_name',
 616                        'affiliate_contact_telephone',
 617                        'affiliate_contact_extension',
 618                        'affiliate_contact_email',
 619                        'affiliate_creation_date',
 620                        );
 621    // prepare data
 622    
 623    $form_state['values']['uid']  = $user->uid;
 624    $form_state['values']['affiliate_application_status']  = 'pending';
 625  
 626    $table='affiliate';
 627    $flds=array();
 628    //print '</pre>';   
 629    foreach ($fld_info as $key => $fldname) {
 630      $flds[$fldname] = check_plain($form_state['values'][$fldname]);
 631    }
 632    // handle logo element
 633    $logo = $form_state['values']['affiliate_logo'];
 634    $image_id = 0;
 635    if ($logo) {
 636      debug_local('upload_element_save');
 637      $image_id = upload_element_save($logo, 'images', FILE_EXISTS_RENAME, 'save-original');
 638    }
 639    $flds['affiliate_logo'] = $image_id;
 640    $flds['affiliate_creation_date']  = db_result(db_query('SELECT now()'));
 641  
 642    drupal_write_record($table,$flds);
 643        
 644    $aaid =  db_result(db_query('SELECT aaid FROM {affiliate} WHERE uid = %d',$user->uid));
 645    $fld_survey = array('affiliate_survey_indiv',
 646                        'affiliate_survey_hosp',
 647                        'affiliate_survey_lic_beds',
 648                        'affiliate_survey_nurse_homes',
 649                        'affiliate_survey_nurse_or_resid_units',
 650                        'affiliate_survey_have_website',
 651                        'affiliate_survey_website_users',
 652                        'affiliate_survey_total_users',
 653                        'affiliate_survey_acc_sat',
 654                        'affiliate_survey_acc_ftp',
 655                        'affiliate_survey_acc_stream',
 656                        'affiliate_survey_prog_daily',
 657                        'affiliate_survey_prog_names',
 658                        'affiliate_survey_additions',
 659                        'affiliate_survey_comments',
 660                        );
 661    
 662  
 663    $table='affiliate_survey';
 664    $flds=array();
 665    foreach ($fld_survey as $key => $fldname) {
 666      //$out .= "key: $key  field: $fldname  |" . $form_state['values'][$fldname] . "\n";
 667      $flds[$fldname] =  check_plain($form_state['values'][$fldname]);
 668    }
 669    $flds['aaid'] = $aaid;
 670    drupal_write_record($table,$flds);
 671  
 672    return;
 673  
 674  }
 675  
 676  function affiliate_show($arg='') {
 677    debug_local("In affiliate_show()");
 678    global $user;
 679  
 680    if ($arg) {
 681      $aa = db_fetch_object(db_query('SELECT * from {affiliate} WHERE uid = %d',$arg));
 682    }
 683    else {
 684      // Print list if adminstrator
 685    }
 686  
 687    if (!$aa) {
 688      $output= 'The specified user did not submit an application for affiliate status.';
 689      return $output;
 690    }
 691    if ($arg == $user->uid) {
 692      $output = '<div id="aff_app_preamble">
 693  Your application has been submitted to the station administrator and will be reviewed.
 694  When the review is completed 
 695  you will receive an e-mail containing instructions for accessing the GATEWAVE feed.  
 696  </div>';
 697      drupal_set_message($output);
 698      $output = 'Here is a copy of the application for your records:';
 699    }
 700    else {
 701      $output = '';
 702    }
 703    $output .= '
 704  <div id="aff_app_body">&nbsp;<br>
 705  <div style="text-align: center">Application for Affiliate Status </div>
 706  <table>
 707    <tr>
 708      <td>Acceptance:</td>
 709      <td>CHECKED</td></tr>
 710    <tr>
 711      <td>Service Type:</td>
 712      <td>';
 713    $output .= ucwords($aa->affiliate_type);
 714    $address  = preg_replace('/\n/','<br>', $aa->affiliate_address);
 715    $output .= '</td></tr>
 716    <tr><td>Service Name:</td>
 717        <td>' . ucwords($aa->affiliate_name) . '</td></tr>
 718    <tr><td>Mailing Address:</td>
 719        <td>' . ucwords($address) . '</td></tr>
 720    <tr><td>Postal Code:</td>
 721        <td>' . $aa->affiliate_postalcode . '</td></tr>';
 722    if ($aa->affiliate_fax) {
 723      $output .= '
 724        <tr><td>Fax:</td>
 725        <td>' . $aa->affiliate_fax . '</td></tr>';
 726    }
 727    $output .= '
 728    <tr><td>Contact Name:</td>
 729        <td>' . ucwords($aa->affiliate_contact_name) . '</td></tr>
 730    <tr><td>Contact Title:</td>
 731        <td>' . ucwords($aa->affiliate_contact_title) . '</td></tr>
 732    <tr><td>Contact Telephone:</td>
 733        <td>' . $aa->affiliate_contact_telephone .
 734      ($aa->affiliate_contact_extension ? ' Extn: ' . $aa->affiliate_contact_extension : '')
 735      . '</td></tr>
 736    <tr><td>Contact E-mail:</td>
 737        <td>' . $aa->affiliate_contact_email . '</td></tr>
 738    <tr><td>Application Submitted:</td>
 739        <td>' . $aa->affiliate_creation_date . '</td></tr>
 740  </table><br>&nbsp;<br>';
 741    if ($arg == $user->uid) {
 742      $output .= 'Your Survey:';
 743    }
 744    else {
 745      $output .= 'Affilate Survey:';
 746    }
 747    
 748    $as = db_fetch_object(db_query('SELECT * from {affiliate_survey} WHERE aaid = %d ORDER BY affiliate_survey_date DESC LIMIT 1',$aa->aaid)); // latest survey
 749  
 750    $output .= '
 751  <table>
 752    <tr><td>Individual Users:</td>
 753        <td>' . $as->affiliate_survey_indiv . '</td></tr>
 754    <tr><td>Hospitals:</td>
 755        <td>' . $as->affiliate_survey_hosp . '</td></tr>
 756    <tr><td>Licensed Beds:</td>
 757        <td>' . $as->affiliate_survey_lic_beds . '</td></tr>
 758    <tr><td>Nursing Homes:</td>
 759        <td>' . $as->affiliate_survey_nurse_homes . '</td></tr>
 760    <tr><td>Nursing or Resid. Units:</td>
 761        <td>' . $as->affiliate_survey_nurse_or_resid_units . '</td></tr>';
 762    if ($as->affiliate_survey_have_website) {
 763      $output .= '
 764    <tr><td>Has Website:</td>
 765        <td>Yes</td></tr>
 766    <tr><td>Website Users:</td>
 767        <td>' . $as->affiliate_survey_website_users . '</td></tr>';
 768    }
 769    $output .= '
 770    <tr><td>Total Users:</td>
 771        <td>' . $as->affiliate_survey_total_users . '</td></tr>
 772    <tr><td>Access by Satellite:</td>
 773        <td>' . ($as->affiliate_survey_acc_sat? 'Yes' : 'No') . '</td></tr>
 774    <tr><td>Access by FTP:</td>
 775        <td>' . ($as->affiliate_survey_acc_ftp? 'Yes' : 'No')  . '</td></tr>
 776    <tr><td>Access by Stream:</td>
 777        <td>' . ($as->affiliate_survey_acc_stream? 'Yes' : 'No')  . '</td></tr>
 778    <tr><td>Programs used daily:</td>
 779        <td>' . $as->affiliate_survey_prog_daily . '</td></tr>
 780    <tr><td>Program Names:</td>
 781        <td>' . $as->affiliate_survey_prog_names . '</td></tr>
 782    <tr><td>Additions:</td>
 783        <td>' . preg_replace('/\n/','<br>', $as->affiliate_survey_additions) . '</td></tr>
 784    <tr><td>Comments:</td>
 785        <td>' . preg_replace('/\n/','<br>', $as->affiliate_survey_comments) . '</td></tr>
 786    <tr><td>Survey Date:</td>
 787        <td>' . preg_replace('/\n/','<br>', $as->affiliate_survey_date) . '</td></tr>
 788  </table>
 789  </div>';
 790  
 791    if (user_access('administer affiliate')) {
 792      //drupal_rebuild_form($form_id, &$form_state, $args, $form_build_id = NULL)
 793      $output .= drupal_get_form('affiliate_approve_form');
 794    }
 795    return $output ;
 796  }
 797  
 798  function affiliate_approve_form($form_id, $form_state = NULL)  {
 799    $user = user_load(arg(2));
 800    $form['affiliate_approve']['app_btn'] = array(
 801      '#type' => 'submit',
 802      '#value' => 'Approve affiliate application',
 803      '#submit' => array('affiliate_approve_submit'),
 804      '#prefix' => '<div #id="affiliate_approve_button" style="text-align: center; padding-top: 10px;">',
 805      '#suffix' => '</div>',
 806    );
 807    $form['affiliate_approve']['app_uid'] = array(
 808      '#type' => 'hidden',
 809      '#value' => arg(2),
 810      );    
 811    $form['affiliate_approve']['app_mail'] = array(
 812      '#type' => 'hidden',
 813      '#value' => $user->mail,
 814      );    
 815    return $form;
 816  } 
 817  
 818  function affiliate_approve_submit($form_id, &$form_state) {
 819    // validate
 820    $uid = $form_state['values']['app_uid'];  
 821    $aaid = db_result(db_query('SELECT aaid FROM {affiliate} WHERE uid = %d',$uid));
 822    if ($aaid) {
 823      $obj = db_fetch_object(db_query('SELECT affiliate_application_status, affiliate_name   FROM {affiliate} WHERE aaid = %d',$aaid));
 824      if ($obj) {
 825        if ($obj->affiliate_application_status == 'approved' ) {
 826          $output = "The application for this affiliate ($obj->affiliate_name) has already been approved.";
 827          drupal_set_message($output);
 828          return;
 829        }
 830        else {
 831          $user = user_load($uid);
 832          $role_list = user_roles(TRUE);
 833          $found = 0;
 834          foreach ($role_list as $rid  => $role ) {
 835            if ($role == 'affiliate') {
 836              $user->roles[$rid] = $role_list[$rid];
 837              user_save($user, array('roles' => $user->roles), NULL);
 838              $found = 1;
 839              break;
 840            }
 841          }
 842          if ( ! $found ) {
 843            $rid = 0;
 844            drupal_set_message('Cannot set user role to affilate','warning');
 845          }
 846          
 847          $record = new stdClass();
 848          $record->affiliate_application_status = 'approved';
 849          $record->aaid = $aaid;
 850          if(drupal_write_record('affiliate', $record, 'aaid')) {
 851            drupal_set_message('Application successfully approved.');
 852            return;
 853          }
 854          else {
 855            drupal_set_message('Application approval failed.','error');
 856            return;
 857          }
 858        }
 859      }
 860      else {
 861        drupal_set_message("Failed to retrieve application from database, please contact system administrator.",
 862                           'error');
 863        return;
 864      }
 865    }
 866    else {
 867      drupal_set_message("Application not found for user $uid.",'error');
 868      return;
 869    }
 870  }
 871  
 872  /**
 873   * Implementation of hook_help().
 874   */
 875  function affiliate_help($path,$arg) {
 876  
 877    global $user;
 878    
 879    switch($path) {
 880    case 'affiliate/register' :
 881      debug_local("In affiliate_help()");
 882      if (! $user->uid) {
 883        return '';
 884      }
 885      $count = db_result(db_query('SELECT COUNT(*) FROM {affiliate} WHERE uid = %d',$user->uid));
 886      if ($count  ) { 
 887        return '';
 888      }      
 889  
 890      $node = node_load(array('nid' => 69));    $body = $node->body;
 891        $from = array('$affiliateName$','$affiliateAddr$');
 892        $to   = array('(the User)','(the User\'s address)');
 893        $text = str_replace($from, $to, $body);
 894        return($text);
 895        
 896    case 'affiliate/show/%' :
 897      if ($arg[2] != $user->uid) {
 898        // only show this to the user it belongs to
 899        return;
 900      }
 901      else {
 902        $aa = db_fetch_object(db_query('SELECT * from {affiliate} WHERE uid = %d',$arg[2]));
 903        $node = node_load(array('nid' => 69));
 904        $body = $node->body;
 905        $from = array('$affiliateName$','$affiliateAddr$');
 906        $addr = $aa->affiliate_address;
 907        $addr = str_replace("\r\n",', ', $addr);
 908        $to   = array($aa->affiliate_name, $addr);
 909        $text = str_replace($from, $to, $body);
 910        return($text);
 911      }
 912    }
 913  } 
 914  
 915  
 916  
 917  function affiliate_token_values($type, $object = NULL, $options = array()) {
 918    $username = variable_get('affiliate_admin_username','admin');
 919    if ($user = user_load(array('name' => $username))) {
 920      $mail = $user->mail;
 921    }
 922    else {
 923      return array();
 924    }
 925    if ($type == 'user') {
 926      //$tokens['user:affiliate_admin_usermail_raw']  = $mail;
 927      $tokens['user:affiliate_admin_usermail']      = check_plain($mail);
 928      $message="Added user:affiliate_admin_usermail ($mail) to tokens";
 929      return $tokens;
 930    }
 931  }
 932  
 933  function affiliate_token_list($type = 'all') {
 934    if ($type == 'user' || $type == 'all') {
 935      $tokens['user']['user:affiliate_admin_usermail']      = t("The affiliate administrator's email address");
 936      //$tokens['user']['user:affiliate_admin_usermail_raw']  = t("The affiliate administrator's email address");
 937      return $tokens;
 938    }
 939  }
 940  
 941  // Hook for volunteer form
 942  
 943  function affiliate_form_webform_client_form_134_alter(&$form) {
 944    $form['submitted']['name_title']['#multicolumn'] = array('width' => 5);
 945    $form['submitted']['name_title']['#process'][] = 'multicolumncheckboxesradios_element_process';
 946    $form['submitted']['name_title']['#prefix'] = '<div class=" webform-align-radios webform-component webform-component-radios" id="webform-component-name_title">';
 947    $form['submitted']['name_title']['#suffix'] = '</div>';
 948  
 949    
 950  
 951    $form['submitted']['languages_spoken']['#multicolumn'] = array('width' => 3);
 952    $form['submitted']['languages_spoken']['#process'][] = 'multicolumncheckboxesradios_element_process';
 953  
 954    $form['submitted']['referred_by']['#multicolumn'] = array('width' => 3, 'row-major' => TRUE);
 955    $form['submitted']['referred_by']['#process'][] = 'multicolumncheckboxesradios_element_process';
 956  }
 957  
 958  // Hook for user application
 959  function affiliate_form_webform_client_form_135_alter(&$form) {
 960    $form['submitted']['name_title']['#multicolumn'] = array('width' => 5);
 961    $form['submitted']['name_title']['#process'][] = 'multicolumncheckboxesradios_element_process';
 962    $form['submitted']['name_title']['#prefix'] = '<div class="webform-align-radios webform-component webform-component-radios" id="webform-component-name_title">';
 963    $form['submitted']['name_title']['#suffix'] = '</div>';
 964  
 965    $form['submitted']['brdcst_sch']['#multicolumn'] = array('width' => 5, 'row-major' => TRUE);
 966    $form['submitted']['brdcst_sch']['#process'][] = 'multicolumncheckboxesradios_element_process';
 967  
 968    $form['submitted']['service_req']['#multicolumn'] = array('width' => 3, 'row-major' => TRUE);
 969    $form['submitted']['service_req']['#process'][] = 'multicolumncheckboxesradios_element_process';
 970  
 971    $form['submitted']['gender']['#multicolumn'] = array('width' => 2);
 972    $form['submitted']['gender']['#process'][] = 'multicolumncheckboxesradios_element_process';
 973    $form['submitted']['gender']['#prefix'] = '<div class="webform-align-radios webform-component webform-component-radios" id="webform-component-gender">';
 974    $form['submitted']['gender']['#suffix'] = '</div>';
 975    
 976    $form['submitted']['education']['#multicolumn'] = array('width' => 5);
 977    $form['submitted']['education']['#process'][] = 'multicolumncheckboxesradios_element_process';
 978    $form['submitted']['education']['#prefix'] = '<div class="webform-align-radios webform-component webform-component-radios" id="webform-component-education">';
 979    $form['submitted']['education']['#suffix'] = '</div>';
 980    
 981    $form['submitted']['referer']['#multicolumn'] = array('width' => 3, 'row-major' => TRUE);
 982    $form['submitted']['referer']['#process'][] = 'multicolumncheckboxesradios_element_process';
 983    //  $form['submitted']['referer']['#prefix'] = '<div class="webform-align-radios webform-component webform-component-radios" id="webform-component-referer">';
 984    // $form['submitted']['referer']['#suffix'] = '</div>';
 985    
 986    $form['submitted']['domicile']['#multicolumn'] = array('width' => 2, 'row-major' => TRUE);
 987    $form['submitted']['domicile']['#process'][] = 'multicolumncheckboxesradios_element_process';
 988    $form['submitted']['domicile']['#prefix'] = '<div class="webform-align-radios webform-component webform-component-radios" id="webform-component-domicile">';
 989    $form['submitted']['domicile']['#suffix'] = '</div>';
 990    
 991    $form['submitted']['i_agree']['#multicolumn'] = array('width' => 2, 'row-major' => TRUE);
 992    $form['submitted']['i_agree']['#process'][] = 'multicolumncheckboxesradios_element_process';
 993    $form['submitted']['i_agree']['#prefix'] = '<div class="webform-align-radios webform-component webform-component-radios" id="webform-component-i_agree">';
 994    $form['submitted']['i_agree']['#suffix'] = '</div>';
 995  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7