[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Form_field.class.php (source)

   1  <?php
   2  class MP_Form_field
   3  {
   4  	public static function get($field, $output = OBJECT) 
   5      {
   6          global $wpdb;
   7  
   8          switch (true)
   9          {
  10              case ( empty($field) ) :
  11                  if ( isset($GLOBALS['mp_field']) )     $_field = & $GLOBALS['mp_field'];
  12                  else                        $_field = null;
  13              break;
  14              case ( is_object($field) ) :
  15                  wp_cache_add($field->id, $field, 'mp_field');
  16                  $_field = $field;
  17              break;
  18              default :
  19                  if ( isset($GLOBALS['mp_field']) && ($GLOBALS['mp_field']->id == $field) ) 
  20                  {
  21                      $_field = & $GLOBALS['mp_field'];
  22                  } 
  23                  elseif ( ! $_field = wp_cache_get($field, 'mp_field') ) 
  24                  {
  25                      $_field = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->mp_fields WHERE id = %d LIMIT 1", $field));
  26                      if ($_field) wp_cache_add($_field->id, $_field, 'mp_field');
  27                  }
  28              break;
  29          }
  30  
  31          if ($_field && isset($_field->settings)) $_field->settings = unserialize($_field->settings);
  32  
  33          if ( $output == OBJECT ) {
  34              return $_field;
  35          } elseif ( $output == ARRAY_A ) {
  36              return get_object_vars($_field);
  37          } elseif ( $output == ARRAY_N ) {
  38              return array_values(get_object_vars($_field));
  39          } else {
  40              return $_field;
  41          }
  42      }
  43  
  44  	public static function get_all($form_id, $all = false) 
  45      {
  46          global $wpdb;
  47          $columns = ($all) ? '*' : 'a.id, a.ordre';
  48          return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT $columns FROM $wpdb->mp_fields a WHERE a.form_id = %s ORDER BY a.ordre", $form_id) );
  49      }
  50  
  51      // file uploading ?
  52  	public static function have_file($form_id) 
  53      {
  54          global $wpdb;
  55  
  56          $fields = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT type FROM $wpdb->mp_fields a WHERE a.form_id = %s ;", $form_id) );
  57  
  58          if (!$fields) return false;
  59  
  60          $have_file = false;
  61          foreach ($fields as $field) $have_file = MP_Form_field_types::have_file($have_file, $field->type);
  62          return $have_file;
  63      }
  64  
  65  	public static function exists($label, $form_id) 
  66      {
  67          global $wpdb;
  68          return $wpdb->get_var($wpdb->prepare("SELECT id FROM $wpdb->mp_fields WHERE label = %s AND form_id = %d LIMIT 1", $label, $form_id));
  69      }
  70  
  71  	public static function insert($_post_field) 
  72      {
  73          $_post_defaults = array('id' => 0, 'form_id' => 0);
  74          $_post_field = wp_parse_args(stripslashes_deep($_post_field), $_post_defaults);
  75          extract($_post_field, EXTR_SKIP);
  76  
  77          if ( trim( $label ) == '' )
  78          {
  79              if ( ! $wp_error )    return 0;
  80              else                return new WP_Error( 'label', __('You did not enter a valid label.', MP_TXTDOM) );
  81          }
  82  
  83          $type_prefixed     = MailPress_form::prefix . $_type;
  84          $settingss         = $$type_prefixed;
  85          $settings        = $settingss['settings'];
  86  
  87          foreach($settings['attributes'] as $k => $v) { $v = trim($v); continue; if (empty($v)) unset($settings['attributes'][$k]); }
  88          if (isset($settingss['textareas'])) foreach ($settingss['textareas'] as $t) if (isset($settings['attributes'][$t])) $settings['attributes'][$t] = base64_encode($settings['attributes'][$t]);
  89  
  90          $data = $format = $where = $where_format = array();
  91  
  92          $data['form_id']         = $form_id;            $format[] = '%d';
  93          $data['ordre']         = (int) $ordre;        $format[] = '%d';
  94          $data['type']         = $_type;            $format[] = '%s';
  95  
  96          $data['template']        = $template;        $format[] = '%s';
  97          $data['label']         = $label;            $format[] = '%s';
  98          $data['description']     = $description;        $format[] = '%s';
  99          $data['settings']     = serialize($settings);    $format[] = '%s';
 100  
 101          // Are we updating or creating?
 102          global $wpdb;
 103          $update = (!empty ($id) ) ? true : false;
 104          if ( $update )
 105          {
 106              $where['id']     = (int) $id;        $where_format[] = '%d';
 107              $wpdb->update( $wpdb->mp_fields, $data, $where, $format, $where_format );
 108              return $id;
 109          }
 110          else
 111          {
 112              $wpdb->insert( $wpdb->mp_fields, $data, $format );
 113              return $wpdb->insert_id;
 114          }
 115      }
 116  
 117  	public static function duplicate($id, $form_id = false)
 118      {
 119          $field = self::get($id);
 120          if (!$field) return false;
 121  
 122          if (!$form_id)
 123          {
 124              do 
 125              {
 126                  $sep = '_x';
 127                  $label = explode($sep, $field->label);
 128                  $num = array_pop($label);
 129                  $label[] = (is_numeric($num)) ? $num + 1 : $num . $sep . '2';
 130                  $field->label = implode($sep, $label);
 131              } while (self::exists($field->label, $field->form_id));
 132          }
 133  
 134          $data = $format         = array();
 135  
 136          $data['form_id']         = ($form_id) ? $form_id : $field->form_id;$format[] = '%d';
 137          $data['ordre']         = $field->ordre;                    $format[] = '%d';
 138          $data['type']         = $field->type;                    $format[] = '%s';
 139  
 140          $data['template']        = $field->template;                $format[] = '%s';
 141          $data['label']         = $field->label;                    $format[] = '%s';
 142          $data['description']     = $field->description;                $format[] = '%s';
 143          $data['settings']     = serialize($field->settings);        $format[] = '%s';    
 144          global $wpdb;
 145          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 146          return $wpdb->insert_id;
 147      }
 148  
 149  	public static function delete($id)
 150      {
 151          global $wpdb;
 152          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_fields WHERE id = %d ;", $id) );
 153          return true;
 154      }
 155  
 156  	public static function delete_all($form_id)
 157      {
 158          global $wpdb;
 159          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_fields WHERE form_id = %d ;", $form_id) );
 160          return true;
 161      }
 162  
 163  	public static function check_visitor($form_id, $has_visitor, $tbc_visitor)
 164      {
 165          $fields = self::get_all($form_id);
 166          if (empty($fields))
 167          {
 168              if ($has_visitor) self::insert_visitor($form_id);
 169              if ($tbc_visitor) self::insert_visitor_mail($form_id);
 170              return;
 171          }
 172          $have_email = $have_name = $have_tbc = array();
 173          foreach ($fields as $field)
 174          {
 175              $field = self::get($field->id);
 176              if ( isset($field->settings['options']['visitor_email']) ) $have_email[] = $field->id;
 177              if ( isset($field->settings['options']['visitor_name'])  ) $have_name[]  = $field->id;
 178              if ( isset($field->settings['options']['visitor_mail'])  ) $have_tbc[]   = $field->id;
 179          }
 180  
 181          if ($has_visitor)
 182          {
 183              if ( empty($have_email) ) self::insert_visitor_email($form_id);
 184              if ( empty($have_name)  ) self::insert_visitor_name($form_id);
 185          }
 186          else
 187          {
 188              foreach(array_merge($have_email, $have_name)  as $field_id) self::delete($field_id);
 189          }
 190  
 191          if ($tbc_visitor)
 192          {
 193              if ( empty($have_tbc) )    self::insert_visitor_mail($form_id);
 194          }
 195          else 
 196          {
 197              foreach($have_tbc as $field_id) self::delete($field_id); 
 198          }
 199      }
 200  
 201  	public static function insert_visitor($form_id) 
 202      {
 203          self::insert_visitor_email($form_id);
 204          self::insert_visitor_name($form_id);
 205      }
 206  
 207  	public static function insert_visitor_email($form_id) 
 208      {
 209          $data = $format         = array();
 210          $settings = array('attributes' => array('type' => 'text', 'size' => 22), 'controls' => array('required' => 1), 'options' => array('visitor_email' => 1, 'protected' =>1, 'incopy' => 1));
 211  
 212          $data['form_id']         = $form_id;                    $format[] = '%d';
 213          $data['ordre']         = 1;                        $format[] = '%d';
 214          $data['type']         = 'email';                    $format[] = '%s';
 215  
 216          $data['template']        = 'standard';                $format[] = '%s';
 217          $data['label']         = __('Email', MP_TXTDOM);        $format[] = '%s';
 218          $data['description']     = __('Visitor email', MP_TXTDOM);    $format[] = '%s';
 219          $data['settings']     = serialize($settings);            $format[] = '%s';
 220  
 221          global $wpdb;
 222          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 223          return $wpdb->insert_id;
 224      }
 225  
 226  	public static function insert_visitor_name($form_id) 
 227      {
 228          $data = $format         = array();
 229          $settings = array('attributes' => array('type' => 'text', 'size' => 22), 'controls' => array('required' => 1), 'options' => array('visitor_name' => 1, 'protected' =>1, 'incopy' => 1));
 230  
 231          $data['form_id']         = $form_id;                    $format[] = '%d';
 232          $data['ordre']         = 1;                        $format[] = '%d';
 233          $data['type']         = 'text';                    $format[] = '%s';
 234  
 235          $data['template']        = 'standard';                $format[] = '%s';
 236          $data['label']         = __('Name', MP_TXTDOM);        $format[] = '%s';
 237          $data['description']     = __('Visitor name', MP_TXTDOM);    $format[] = '%s';
 238          $data['settings']     = serialize($settings);            $format[] = '%s';
 239  
 240          global $wpdb;
 241          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 242          return $wpdb->insert_id;
 243      }
 244  
 245  	public static function insert_visitor_mail($form_id) 
 246      {
 247          $data = $format         = array();
 248          $settings = array('attributes' => array('type' => 'checkbox', 'checked' => 'checked'), 'options' => array('visitor_mail' => 1, 'protected' =>1));
 249  
 250          $data['form_id']         = $form_id;                        $format[] = '%d';
 251          $data['ordre']         = 1;                            $format[] = '%d';
 252          $data['type']         = 'checkbox';                    $format[] = '%s';
 253  
 254          $data['template']        = 'standard';                    $format[] = '%s';
 255          $data['label']         = __('Mail sent if checked', MP_TXTDOM);    $format[] = '%s';
 256          $data['description']     = __('Check to get a copy', MP_TXTDOM);    $format[] = '%s';
 257          $data['settings']     = serialize($settings);                $format[] = '%s';
 258  
 259          global $wpdb;
 260          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 261          return $wpdb->insert_id;
 262      }
 263  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1