[ Index ]

MailPress 7.1

[ 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  
  76          $Post_field['label']         = sanitize_text_field( $Post_field['label'] );
  77          $Post_field['description']     = sanitize_text_field( $Post_field['description'] );
  78  
  79          extract( $Post_field, EXTR_SKIP );
  80  
  81          if ( trim( $label ) == '' )
  82          {
  83              if ( ! $wp_error )    return 0;
  84              else                return new WP_Error( 'label', __( 'You did not enter a valid label.', 'MailPress' ) );
  85          }
  86  
  87          $type_prefixed     = MailPress_form::prefix . $_type;
  88          $settingss         = $$type_prefixed;
  89          $settings        = $settingss['settings'];
  90  
  91          foreach( $settings['attributes'] as $k => $v ) { $v = trim( $v ); continue; if ( empty( $v ) ) unset( $settings['attributes'][$k] ); }
  92          if ( isset( $settingss['textareas'] ) ) foreach ( $settingss['textareas'] as $t ) if ( isset( $settings['attributes'][$t] ) ) $settings['attributes'][$t] = base64_encode( $settings['attributes'][$t] );
  93  
  94          $data = $format = $where = $where_format = array();
  95  
  96          $data['form_id']         = $form_id;            $format[] = '%d';
  97          $data['ordre']         = ( int ) $ordre;        $format[] = '%d';
  98          $data['type']         = $_type;            $format[] = '%s';
  99  
 100          $data['template']        = $template;        $format[] = '%s';
 101          $data['label']         = $label;            $format[] = '%s';
 102          $data['description']     = $description;        $format[] = '%s';
 103          $data['settings']     = serialize( $settings );    $format[] = '%s';
 104  
 105          // Are we updating or creating?
 106          global $wpdb;
 107          $update = ( !empty ( $id ) ) ? true : false;
 108          if ( $update )
 109          {
 110              $where['id']     = ( int ) $id;        $where_format[] = '%d';
 111              $wpdb->update( $wpdb->mp_fields, $data, $where, $format, $where_format );
 112              return $id;
 113          }
 114          else
 115          {
 116              $wpdb->insert( $wpdb->mp_fields, $data, $format );
 117              return $wpdb->insert_id;
 118          }
 119      }
 120  
 121  	public static function duplicate( $id, $form_id = false )
 122      {
 123          $field = self::get( $id );
 124          if ( !$field ) return false;
 125  
 126          if ( !$form_id )
 127          {
 128              do 
 129              {
 130                  $sep = '_x';
 131                  $label = explode( $sep, $field->label );
 132                  $num = array_pop( $label );
 133                  $label[] = ( is_numeric( $num ) ) ? $num + 1 : $num . $sep . '2';
 134                  $field->label = implode( $sep, $label );
 135              } while ( self::exists( $field->label, $field->form_id ) );
 136          }
 137  
 138          $data = $format         = array();
 139  
 140          $data['form_id']         = ( $form_id ) ? $form_id : $field->form_id;$format[] = '%d';
 141          $data['ordre']         = $field->ordre;                    $format[] = '%d';
 142          $data['type']         = $field->type;                    $format[] = '%s';
 143  
 144          $data['template']        = $field->template;                $format[] = '%s';
 145          $data['label']         = $field->label;                    $format[] = '%s';
 146          $data['description']     = $field->description;                $format[] = '%s';
 147          $data['settings']     = serialize( $field->settings );        $format[] = '%s';    
 148          global $wpdb;
 149          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 150          return $wpdb->insert_id;
 151      }
 152  
 153  	public static function delete( $id )
 154      {
 155          global $wpdb;
 156          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_fields WHERE id = %d ;", $id ) );
 157          return true;
 158      }
 159  
 160  	public static function delete_all( $form_id )
 161      {
 162          global $wpdb;
 163          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_fields WHERE form_id = %d ;", $form_id ) );
 164          return true;
 165      }
 166  
 167  	public static function check_visitor( $form_id, $has_visitor, $tbc_visitor )
 168      {
 169          $fields = self::get_all( $form_id );
 170          if ( empty( $fields ) )
 171          {
 172              if ( $has_visitor ) self::insert_visitor( $form_id );
 173              if ( $tbc_visitor ) self::insert_visitor_mail( $form_id );
 174              return;
 175          }
 176          $have_email = $have_name = $have_tbc = array();
 177          foreach ( $fields as $field )
 178          {
 179              $field = self::get( $field->id );
 180              if ( isset( $field->settings['options']['visitor_email'] ) ) $have_email[] = $field->id;
 181              if ( isset( $field->settings['options']['visitor_name'] )  ) $have_name[]  = $field->id;
 182              if ( isset( $field->settings['options']['visitor_mail'] )  ) $have_tbc[]   = $field->id;
 183          }
 184  
 185          if ( $has_visitor )
 186          {
 187              if ( empty( $have_email ) ) self::insert_visitor_email( $form_id );
 188              if ( empty( $have_name )  ) self::insert_visitor_name( $form_id );
 189          }
 190          else
 191          {
 192              foreach( array_merge( $have_email, $have_name )  as $field_id ) self::delete( $field_id );
 193          }
 194  
 195          if ( $tbc_visitor )
 196          {
 197              if ( empty( $have_tbc ) )    self::insert_visitor_mail( $form_id );
 198          }
 199          else 
 200          {
 201              foreach( $have_tbc as $field_id ) self::delete( $field_id ); 
 202          }
 203      }
 204  
 205  	public static function insert_visitor( $form_id ) 
 206      {
 207          self::insert_visitor_email( $form_id );
 208          self::insert_visitor_name( $form_id );
 209      }
 210  
 211  	public static function insert_visitor_email( $form_id ) 
 212      {
 213          $data = $format         = array();
 214          $settings = array( 'attributes' => array( 'type' => 'text', 'size' => 22 ), 'controls' => array( 'required' => 1 ), 'options' => array( 'visitor_email' => 1, 'protected' =>1, 'incopy' => 1 ) );
 215  
 216          $data['form_id']         = $form_id;                    $format[] = '%d';
 217          $data['ordre']         = 1;                        $format[] = '%d';
 218          $data['type']         = 'email';                    $format[] = '%s';
 219  
 220          $data['template']        = 'standard';                $format[] = '%s';
 221          $data['label']         = __( 'Email', 'MailPress' );        $format[] = '%s';
 222          $data['description']     = __( 'Visitor email', 'MailPress' );    $format[] = '%s';
 223          $data['settings']     = serialize( $settings );            $format[] = '%s';
 224  
 225          global $wpdb;
 226          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 227          return $wpdb->insert_id;
 228      }
 229  
 230  	public static function insert_visitor_name( $form_id ) 
 231      {
 232          $data = $format         = array();
 233          $settings = array( 'attributes' => array( 'type' => 'text', 'size' => 22 ), 'controls' => array( 'required' => 1 ), 'options' => array( 'visitor_name' => 1, 'protected' =>1, 'incopy' => 1 ) );
 234  
 235          $data['form_id']         = $form_id;                    $format[] = '%d';
 236          $data['ordre']         = 1;                        $format[] = '%d';
 237          $data['type']         = 'text';                    $format[] = '%s';
 238  
 239          $data['template']        = 'standard';                $format[] = '%s';
 240          $data['label']         = __( 'Name', 'MailPress' );        $format[] = '%s';
 241          $data['description']     = __( 'Visitor name', 'MailPress' );    $format[] = '%s';
 242          $data['settings']     = serialize( $settings );            $format[] = '%s';
 243  
 244          global $wpdb;
 245          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 246          return $wpdb->insert_id;
 247      }
 248  
 249  	public static function insert_visitor_mail( $form_id ) 
 250      {
 251          $data = $format         = array();
 252          $settings = array( 'attributes' => array( 'type' => 'checkbox', 'checked' => 'checked' ), 'options' => array( 'visitor_mail' => 1, 'protected' =>1 ) );
 253  
 254          $data['form_id']         = $form_id;                        $format[] = '%d';
 255          $data['ordre']         = 1;                            $format[] = '%d';
 256          $data['type']         = 'checkbox';                    $format[] = '%s';
 257  
 258          $data['template']        = 'standard';                    $format[] = '%s';
 259          $data['label']         = __( 'Mail sent if checked', 'MailPress' );    $format[] = '%s';
 260          $data['description']     = __( 'Check to get a copy', 'MailPress' );    $format[] = '%s';
 261          $data['settings']     = serialize( $settings );                $format[] = '%s';
 262  
 263          global $wpdb;
 264          $wpdb->insert( $wpdb->mp_fields, $data, $format );
 265          return $wpdb->insert_id;
 266      }
 267  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1