Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Updatess the invitees database table
- */
- function do_add_invitees_in_database(){
- /** Make sure there are invitees to add */
- if(empty($this->invitees['new'])) :
- return $_POST['add_successfull'] = true;
- endif;
- global $wpdb;
- /** Set the names of the fields to use */
- $fields = array(
- 'invitee_created_by',
- 'invitee_created_date',
- 'invitee_last_edited_by',
- 'invitee_last_edited_date',
- 'event_id',
- 'email',
- 'first_name',
- 'surname',
- 'custom',
- 'invited'
- );
- /** Get the creator ID */
- $creator_id = get_current_user_id();
- /** Make the chart creation date */
- $date_added = date('Y-m-d H:i:s', time());
- /** Insert the new invitees in to the database */
- $all_data = array();
- $values = array();
- foreach($this->invitees['new'] as $invitee) :
- $row = array(
- $creator_id,
- $date_added,
- '0',
- '0000-00-00 00:00:00',
- $_POST['event_id'],
- $invitee['email'],
- $invitee['first_name'],
- $invitee['surname'],
- $invitee['custom'],
- 0
- );
- /** Make the data array */
- $data = array_combine($fields, $row);
- /** Update the array containing all data passed to the query (for debugging purposes) */
- $all_data[] = $data;
- /** Grab the VALUES() portion of the query (for this invitee) */
- $values[] = $this->_insert_replace_invitee_values($data);
- endforeach;
- /** Insert the new invitees in to the database */
- $result = $this->_insert_replace_invitee($wpdb->event_invitees, $values, $fields);
- /** Debug the query run by $wpdb->query() */
- $this->debug($all_data);
- /** If there were no errors, mark the invitaion additions as successfull */
- return $_POST['add_successfull'] = (!$this->error_found) ? true : false;
- }
- /**
- * Runs a query to either insert new rows into the database, or to replace existing rows (if ID is specified)
- *
- * @param required string $table The name of the table that is being queried
- * @param required array $values The values to insert or replace in the table
- * @param required array $fields The fields that are to be populated by the $values
- * @param string $type The type of job that is taking place ('INSERT' or 'REPLACE')
- * @return integer The number of rows affected by the query
- */
- function _insert_replace_invitee($table, $values, $fields, $type = 'INSERT'){
- /** Make sure that we should be using this helper */
- if(!in_array(strtoupper($type), array('INSERT', 'REPLACE'))) :
- return false;
- endif;
- global $wpdb;
- $query = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES " . join(','."\n", $values);
- $query = $wpdb->prepare('%1$s INTO %2$s'."\n\t".'(`%3$s`)'."\n".'VALUES'."\n\t", $type, $table, implode('`,`', $fields)).join(','."\n\t", $values);
- return $wpdb->query($query);
- }
- /**
- * Creates a string of values that should be inserted in to database for an invitee, or to replace existing rows (if ID is specified)
- *
- * @param required array $data The data to use for the insert ('$key => $value' muse be defined)
- * @param required array $format The formats to use for specific data
- * @param string $type The type of job that is taking place ('INSERT' or 'REPLACE')
- * @return string The values for this invitee
- */
- function _insert_replace_invitee_values($data, $format = null, $type = 'INSERT'){
- /** Make sure that we should be using this helper */
- if(!in_array(strtoupper($type), array('INSERT', 'REPLACE'))) :
- return false;
- endif;
- global $wpdb;
- /** Set up the formtas array */
- $formats = $format = (array)$format; // Setup a $formats array (and ensure $format is also an array)
- $fields = array_keys($data); // Grab the fields that are to be used from $data
- /** Work out which fields are to be formatted (and add default fromating to those not defined) */
- $formatted_fields = array();
- foreach($fields as $field) :
- if(!empty($format)) : // If $format is defined, check
- $filed_format = ($filed_format = array_shift($formats)) ? $filed_format : $format[0];
- else : // Set the format as defult to string
- $filed_format = '%s';
- endif;
- $formatted_fields[] = $filed_format;
- endforeach;
- return $wpdb->prepare(sprintf('(%1$s)', implode(",", $formatted_fields)), $data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement