[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/webform/components/ -> email.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Webform module email component.
   6   */
   7  
   8  /**
   9   * Implements _webform_defaults_component().
  10   */
  11  function _webform_defaults_email() {
  12    return array(
  13      'name' => '',
  14      'form_key' => NULL,
  15      'pid' => 0,
  16      'weight' => 0,
  17      'value' => '',
  18      'mandatory' => 0,
  19      'extra' => array(
  20        'width' => '',
  21        'unique' => 0,
  22        'disabled' => 0,
  23        'title_display' => 0,
  24        'description' => '',
  25        'attributes' => array(),
  26        'private' => FALSE,
  27      ),
  28    );
  29  }
  30  
  31  /**
  32   * Implements _webform_theme_component().
  33   */
  34  function _webform_theme_email() {
  35    return array(
  36      'webform_email' => array(
  37        'arguments' => array('element' => NULL),
  38        'file' => 'components/email.inc',
  39      ),
  40      'webform_display_email' => array(
  41        'arguments' => array('component' => NULL, 'value' => NULL, 'format' => 'plain'),
  42        'file' => 'components/email.inc',
  43      ),
  44    );
  45  }
  46  
  47  /**
  48   * Implements _webform_edit_component().
  49   */
  50  function _webform_edit_email($component) {
  51    $form['value'] = array(
  52      '#type' => 'textfield',
  53      '#title' => t('Default value'),
  54      '#default_value' => $component['value'],
  55      '#description' => t('The default value of the field.') . theme('webform_token_help'),
  56      '#size' => 60,
  57      '#maxlength' => 127,
  58      '#weight' => 0,
  59      '#attributes' => ($component['value'] == '%useremail' && count(form_get_errors()) == 0) ? array('disabled' => TRUE) : array(),
  60      '#id' => 'email-value',
  61    );
  62    $form['user_email'] = array(
  63      '#type' => 'checkbox',
  64      '#title' => t('User email as default'),
  65      '#default_value' => $component['value'] == '%useremail' ? 1 : 0,
  66      '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
  67      '#attributes' => array('onclick' => 'getElementById("email-value").value = (this.checked ? "%useremail" : ""); getElementById("email-value").disabled = this.checked;'),
  68      '#weight' => 0,
  69      '#element_validate' => array('_webform_edit_email_validate'),
  70    );
  71    $form['display']['width'] = array(
  72      '#type' => 'textfield',
  73      '#title' => t('Width'),
  74      '#default_value' => $component['extra']['width'],
  75      '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  76      '#size' => 5,
  77      '#maxlength' => 10,
  78      '#parents' => array('extra', 'width'),
  79    );
  80    $form['display']['disabled'] = array(
  81      '#type' => 'checkbox',
  82      '#title' => t('Disabled'),
  83      '#return_value' => 1,
  84      '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
  85      '#weight' => 11,
  86      '#default_value' => $component['extra']['disabled'],
  87      '#parents' => array('extra', 'disabled'),
  88    );
  89    $form['validation']['unique'] = array(
  90      '#type' => 'checkbox',
  91      '#title' => t('Unique'),
  92      '#return_value' => 1,
  93      '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
  94      '#weight' => 1,
  95      '#default_value' => $component['extra']['unique'],
  96      '#parents' => array('extra', 'unique'),
  97    );
  98    return $form;
  99  }
 100  
 101  /**
 102   * Element validation function for the email edit form.
 103   */
 104  function _webform_edit_email_validate($element, &$form_state) {
 105    if ($form_state['values']['user_email']) {
 106      $form_state['values']['value'] = '%useremail';
 107    }
 108  }
 109  
 110  /**
 111   * Implements _webform_render_component().
 112   */
 113  function _webform_render_email($component, $value = NULL, $filter = TRUE) {
 114    global $user;
 115    $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
 116  
 117    $element = array(
 118      '#type' => 'webform_email',
 119      '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
 120      '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
 121      '#default_value' => $filter ? _webform_filter_values($component['value'], $node) : $component['value'],
 122      '#required' => $component['mandatory'],
 123      '#weight' => $component['weight'],
 124      '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
 125      '#attributes' => $component['extra']['attributes'],
 126      '#element_validate'  => array('_webform_validate_email'),
 127      '#theme_wrappers' => array('webform_element_wrapper'),
 128      '#pre_render' => array('webform_element_title_display'),
 129      '#post_render' => array('webform_element_wrapper'),
 130      '#translatable' => array('title', 'description'),
 131    );
 132  
 133    // Add an e-mail class for identifying the difference from normal textfields.
 134    $element['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' email' : 'email';
 135  
 136    // Enforce uniqueness.
 137    if ($component['extra']['unique']) {
 138      $element['#element_validate'][] = 'webform_validate_unique';
 139    }
 140  
 141    if (isset($value)) {
 142      $element['#default_value'] = $value[0];
 143    }
 144  
 145    if ($component['extra']['disabled']) {
 146      if ($filter) {
 147        $element['#attributes']['readonly'] = 'readonly';
 148      }
 149      else {
 150        $element['#disabled'] = TRUE;
 151      }
 152    }
 153  
 154    // Change the 'width' option to the correct 'size' option.
 155    if ($component['extra']['width'] > 0) {
 156      $element['#size'] = $component['extra']['width'];
 157    }
 158  
 159    return $element;
 160  }
 161  
 162  /**
 163   * Theme function to render a number component.
 164   */
 165  function theme_webform_email($element) {
 166    // This IF statement is mostly in place to allow our tests to set type="text"
 167    // because SimpleTest does not support type="number".
 168    if (!isset($element['#attributes']['type'])) {
 169      $element['#attributes']['type'] = 'email';
 170    }
 171  
 172    // Convert properties to attributes on the element if set.
 173    foreach (array('id', 'name', 'value', 'size') as $property) {
 174      if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
 175        $element['#attributes'][$property] = $element['#' . $property];
 176      }
 177    }
 178    _form_set_class($element, array('form-text', 'form-email'));
 179  
 180    $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
 181  
 182    return theme('form_element', $element, $output);
 183  }
 184  
 185  /**
 186   * A Drupal Form API Validation function. Validates the entered values from
 187   * email components on the client-side form.
 188   *
 189   * @param $form_element
 190   *   The e-mail form element.
 191   * @param $form_state
 192   *   The full form state for the webform.
 193   * @return
 194   *   None. Calls a form_set_error if the e-mail is not valid.
 195   */
 196  function _webform_validate_email($form_element, &$form_state) {
 197    $component = $form_element['#webform_component'];
 198    $value = trim($form_element['#value']);
 199    if ($value !== '' && !valid_email_address($value)) {
 200      form_error($form_element, t('%value is not a valid email address.', array('%value' => $value)));
 201    }
 202    else {
 203      form_set_value($form_element, $value, $form_state);
 204    }
 205  }
 206  
 207  /**
 208   * Implements _webform_display_component().
 209   */
 210  function _webform_display_email($component, $value, $format = 'html') {
 211    return array(
 212      '#title' => $component['name'],
 213      '#weight' => $component['weight'],
 214      '#theme' => 'webform_display_email',
 215      '#theme_wrappers' => $format == 'html' ? array('webform_element', 'webform_element_wrapper') : array('webform_element_text'),
 216      '#post_render' => array('webform_element_wrapper'),
 217      '#format' => $format,
 218      '#value' => isset($value[0]) ? $value[0] : '',
 219      '#translatable' => array('title'),
 220    );
 221  }
 222  
 223  /**
 224   * Format the text output for this component.
 225   */
 226  function theme_webform_display_email($element) {
 227    $element['#value'] = empty($element['#value']) ? ' ' : $element['#value'];
 228    return $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
 229  }
 230  
 231  /**
 232   * Implements _webform_analysis_component().
 233   */
 234  function _webform_analysis_email($component, $sids = array()) {
 235    $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
 236    $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : "";
 237    $query = 'SELECT data ' .
 238      ' FROM {webform_submitted_data} ' .
 239      ' WHERE nid = %d ' .
 240      ' AND cid = %d ' . $sidfilter;
 241    $nonblanks = 0;
 242    $submissions = 0;
 243    $wordcount = 0;
 244  
 245    $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids));
 246    while ($data = db_fetch_array($result)) {
 247      if (drupal_strlen(trim($data['data'])) > 0) {
 248        $nonblanks++;
 249        $wordcount += str_word_count(trim($data['data']));
 250      }
 251      $submissions++;
 252    }
 253  
 254    $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
 255    $rows[1] = array(t('User entered value'), $nonblanks);
 256    $rows[2] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount/$nonblanks, 2) : '0'));
 257    return $rows;
 258  }
 259  
 260  /**
 261   * Implements _webform_table_component().
 262   */
 263  function _webform_table_email($component, $value) {
 264    return check_plain(empty($value[0]) ? '' : $value[0]);
 265  }
 266  
 267  
 268  /**
 269   * Implements _webform_csv_headers_component().
 270   */
 271  function _webform_csv_headers_email($component, $export_options) {
 272    $header = array();
 273    $header[0] = '';
 274    $header[1] = '';
 275    $header[2] = $component['name'];
 276    return $header;
 277  }
 278  
 279  /**
 280   * Implements _webform_csv_data_component().
 281   */
 282  function _webform_csv_data_email($component, $export_options, $value) {
 283    return empty($value[0]) ? '' : $value[0];
 284  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7