/** * Add or edit a single pod item * * $params['datatype'] string The datatype name * $params['columns'] array (optional) Associative array of column names + values * $params['data'] array (optional) Associative array of a set of associative arrays of column names + values (for bulk operations) * $params['pod_id'] int The item's ID from the wp_pod table (or alternatively use the tbl_row_id parameter instead) * $params['tbl_row_id'] int The item's ID from the wp_pod_tbl_* table (or alternatively use the pod_id parameter instead) * * @param array $params An associative array of parameters * @return int The table row ID * @since 1.7.9 */ function save_pod_item($params) { if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE) { $params = pods_sanitize($params); } $params = (object) str_replace('@wp_', '{prefix}', $params); // Support for multiple save_pod_item operations at the same time if (isset($params->data) && !empty($params->data) && is_array($params->data)) { $ids = array(); $new_params = $params; unset($new_params->data); foreach ($params->data as $columns) { $new_params->columns = $columns; $ids[] = $this->save_pod_item($new_params); } return $ids; } // Support for bulk edit if (isset($params->tbl_row_id) && !empty($params->tbl_row_id) && is_array($params->tbl_row_id)) { $ids = array(); $new_params = $params; foreach ($params->tbl_row_id as $tbl_row_id) { $new_params->tbl_row_id = $tbl_row_id; $ids[] = $this->save_pod_item($new_params); } return $ids; } // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls $bypass_helpers = false; if (isset($params->bypass_helpers) && false !== $params->bypass_helpers) { $bypass_helpers = true; } // Get array of datatypes $datatypes = $this->get_table_data(array('array_key' => 'name', 'columns' => 'id, name')); $params->datatype_id = (int) $datatypes[$params->datatype]['id']; // Get the datatype fields $opts = array('table' => 'fields', 'where' => "datatype = {$params->datatype_id}", 'orderby' => 'weight', 'array_key' => 'name'); $columns = $this->get_table_data($opts); // Find the active columns (loop through $params->columns to retain order) if (!empty($params->columns) && is_array($params->columns)) { foreach ($params->columns as $column_name => $column_val) { // Support for Pre Key/Value Parameters in previous Pods versions if (isset($params->name) && isset($params->{$column_val})) { $column_name = $column_val; $column_val = $params->{$column_name}; } if (isset($columns[$column_name])) { $columns[$column_name]['value'] = $column_val; $active_columns[] = $column_name; } } unset($params->columns); } // Load all helpers $result = pod_query("SELECT pre_save_helpers, post_save_helpers FROM @wp_pod_types WHERE id = {$params->datatype_id}"); $row = mysql_fetch_assoc($result); $params->pre_save_helpers = explode(',', $row['pre_save_helpers']); $params->post_save_helpers = explode(',', $row['post_save_helpers']); // Allow Helpers to know what's going on, are we adding or saving? $is_new_item = false; if (!empty($params->tbl_row_id)) { $result = pod_query("SELECT p.id FROM @wp_pod p INNER JOIN @wp_pod_types t ON t.id = p.datatype WHERE p.tbl_row_id = {$params->tbl_row_id} AND t.name = '{$params->datatype}' LIMIT 1", 'Pod item not found', null, 'Pod item not found'); $params->pod_id = mysql_result($result, 0); } elseif (!empty($params->pod_id)) { $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = {$params->pod_id} LIMIT 1", 'Item not found', null, 'Item not found'); $params->tbl_row_id = mysql_result($result, 0); } else { $is_new_item = true; } // Plugin hook do_action('pods_pre_save_pod_item', $params, $columns); // Call any pre-save helpers (if not bypassed) if (!$bypass_helpers && !empty($params->pre_save_helpers)) { foreach ($params->pre_save_helpers as $helper) { $function_or_file = $helper; $check_function = $function_or_file; if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FUNCTIONS') || !PODS_HELPER_FUNCTIONS)) { $check_function = false; } $check_file = null; if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FILES') || !PODS_HELPER_FILES)) { $check_file = false; } if (false !== $check_function && false !== $check_file) { $function_or_file = pods_function_or_file($function_or_file, $check_function, 'helper', $check_file); } else { $function_or_file = false; } $content = false; if (!$function_or_file) { $api = new PodAPI(); $params_helper = array('name' => $helper, 'type' => 'pre_save'); if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) { $params_helper = pods_sanitize($params_helper); } $content = $api->load_helper($params_helper); if (false !== $content && 0 < strlen(trim($content['phpcode']))) { $content = $content['phpcode']; } else { $content = false; } } if (false === $content && false !== $function_or_file && isset($function_or_file['function'])) { $function_or_file['function']($params, $columns, $this); } elseif (false === $content && false !== $function_or_file && isset($function_or_file['file'])) { locate_template($function_or_file['file'], true, true); } elseif (false !== $content) { if (!defined('PODS_DISABLE_EVAL') || PODS_DISABLE_EVAL) { eval("?>{$content}"); } else { echo $content; } } } } // Loop through each active column, validating and preparing the table data foreach ($active_columns as $key) { $val = $columns[$key]['value']; $type = $columns[$key]['coltype']; $label = $columns[$key]['label']; $label = empty($label) ? $key : $label; // Verify required fields if (1 == $columns[$key]['required']) { if ('' == $val || null == $val) { return $this->oh_snap("<e>{$label} is empty."); } elseif ('num' == $type && !is_numeric($val)) { return $this->oh_snap("<e>{$label} is not numeric."); } } // Verify unique fields if (1 == $columns[$key]['unique'] && !in_array($type, array('pick', 'file'))) { $exclude = ''; if (!empty($params->pod_id)) { $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = '{$params->pod_id}' AND datatype = '{$params->datatype_id}' LIMIT 1"); if (0 < mysql_num_rows($result)) { $exclude = 'AND id != ' . mysql_result($result, 0); } } // Trigger an error if not unique $sql = "SELECT id FROM `@wp_pod_tbl_{$params->datatype}` WHERE `{$key}` = '{$val}' {$exclude} LIMIT 1"; pod_query($sql, 'Not unique', "{$label} needs to be unique."); } // Verify slug columns if ('slug' == $type) { $slug_val = empty($val) ? $columns['name']['value'] : $val; $val = pods_unique_slug($slug_val, $key, $params); } // Prepare all table (non-relational) data if (!in_array($type, array('pick', 'file'))) { if ('num' == $type) { $val = floatval($val); } elseif ('bool' == $type) { $val = min(absint($val), 1); } if ('num' != $type) { $val = "'{$val}'"; } $table_data[] = "`{$key}` = {$val}"; } else { $rel_columns[$type][$key] = $val; } } // Create the pod_id if it doesn't exist if (empty($params->pod_id) && empty($params->tbl_row_id)) { $current_time = current_time('mysql'); $user = 0; if (is_user_logged_in()) { global $user_ID; get_currentuserinfo(); $user = $user_ID; } $name = $params->datatype; if (isset($params->name)) { $name = $params->name; } if (isset($params->columns) && is_array($params->columns) && isset($params->columns['name'])) { $name = $params->columns['name']; } $sql = "INSERT INTO @wp_pod (datatype, name, created, modified, author_id) VALUES ('{$params->datatype_id}', '{$name}', '{$current_time}', '{$current_time}', '{$user}')"; $params->pod_id = pod_query($sql, 'Cannot add new content'); $params->tbl_row_id = pod_query("INSERT INTO `@wp_pod_tbl_{$params->datatype}` (name) VALUES (NULL)", 'Cannot add new table row'); } // Save the table row if (isset($table_data)) { $table_data = implode(',', $table_data); pod_query("UPDATE `@wp_pod_tbl_{$params->datatype}` SET {$table_data} WHERE id = {$params->tbl_row_id} LIMIT 1"); } // Update wp_pod $item_name = isset($columns['name']['value']) ? ", name = '" . $columns['name']['value'] . "'" : ''; pod_query("UPDATE @wp_pod SET tbl_row_id = {$params->tbl_row_id}, datatype = {$params->datatype_id}, modified = '" . current_time('mysql') . "' {$item_name} WHERE id = {$params->pod_id} LIMIT 1"); // Save relational column data if (isset($rel_columns)) { // E.g. $rel_columns['pick']['related_events'] = '3,15'; foreach ($rel_columns as $rel_type => $rel_data) { foreach ($rel_data as $rel_name => $rel_values) { $field_id = $columns[$rel_name]['id']; // Convert values from a comma-separated string into an array if (empty($rel_values)) { $rel_values = array(); } elseif (!is_array($rel_values)) { $rel_values = explode(',', $rel_values); } // Remove existing relationships pod_query("DELETE FROM @wp_pod_rel WHERE pod_id = {$params->pod_id} AND field_id = {$field_id}"); // File relationships if ('file' == $rel_type) { $rel_weight = 0; foreach ($rel_values as $related_id) { $related_id = absint($related_id); if (empty($related_id)) { continue; } pod_query("INSERT INTO @wp_pod_rel (pod_id, field_id, tbl_row_id, weight) VALUES ({$params->pod_id}, {$field_id}, {$related_id}, {$rel_weight})"); $rel_weight++; } } elseif ('pick' == $rel_type) { $pickval = $columns[$rel_name]['pickval']; $sister_datatype_id = 0; $sister_field_id = 0; if (!in_array($pickval, array('wp_taxonomy', 'wp_post', 'wp_page', 'wp_user')) && isset($datatypes[$pickval])) { $sister_datatype_id = $datatypes[$pickval]['id']; if (!empty($columns[$rel_name]['sister_field_id'])) { $sister_field_id = $columns[$rel_name]['sister_field_id']; } } $sister_pod_ids = array(); // Delete parent and sister rels if (!empty($sister_field_id)) { // Get sister pod IDs (a sister pod's sister pod is the parent pod) $result = pod_query("SELECT pod_id FROM @wp_pod_rel WHERE sister_pod_id = {$params->pod_id}"); if (0 < mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $sister_pod_ids[] = $row['pod_id']; } $sister_pod_ids = implode(',', $sister_pod_ids); // Delete the sister pod relationship pod_query("DELETE FROM @wp_pod_rel WHERE pod_id IN ({$sister_pod_ids}) AND sister_pod_id = {$params->pod_id} AND field_id = {$sister_field_id}"); } } // Add rel values $rel_weight = 0; foreach ($rel_values as $related_id) { $related_id = absint($related_id); if (empty($related_id)) { continue; } $sister_pod_id = 0; if (!empty($sister_field_id) && !empty($sister_datatype_id)) { $result = pod_query("SELECT id FROM @wp_pod WHERE datatype = {$sister_datatype_id} AND tbl_row_id = {$related_id} LIMIT 1"); if (0 < mysql_num_rows($result)) { $sister_pod_id = mysql_result($result, 0); pod_query("INSERT INTO @wp_pod_rel (pod_id, sister_pod_id, field_id, tbl_row_id, weight) VALUES ({$sister_pod_id}, {$params->pod_id}, {$sister_field_id}, {$params->tbl_row_id}, {$rel_weight})", 'Cannot add sister relationships'); } } pod_query("INSERT INTO @wp_pod_rel (pod_id, sister_pod_id, field_id, tbl_row_id, weight) VALUES ({$params->pod_id}, {$sister_pod_id}, {$field_id}, {$related_id}, {$rel_weight})", 'Cannot add relationships'); $rel_weight++; } } } } } // Plugin hook do_action('pods_post_save_pod_item', $params, $columns); // Call any post-save helpers (if not bypassed) if (!$bypass_helpers && !empty($params->post_save_helpers)) { foreach ($params->post_save_helpers as $helper) { $function_or_file = $helper; $check_function = $function_or_file; if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FUNCTIONS') || !PODS_HELPER_FUNCTIONS)) { $check_function = false; } $check_file = null; if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FILES') || !PODS_HELPER_FILES)) { $check_file = false; } if (false !== $check_function && false !== $check_file) { $function_or_file = pods_function_or_file($function_or_file, $check_function, 'helper', $check_file); } else { $function_or_file = false; } $content = false; if (!$function_or_file) { $api = new PodAPI(); $params_helper = array('name' => $helper, 'type' => 'post_save'); if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) { $params_helper = pods_sanitize($params_helper); } $content = $api->load_helper($params_helper); if (false !== $content && 0 < strlen(trim($content['phpcode']))) { $content = $content['phpcode']; } else { $content = false; } } if (false === $content && false !== $function_or_file && isset($function_or_file['function'])) { $function_or_file['function']($params, $columns, $this); } elseif (false === $content && false !== $function_or_file && isset($function_or_file['file'])) { locate_template($function_or_file['file'], true, true); } elseif (false !== $content) { if (!defined('PODS_DISABLE_EVAL') || PODS_DISABLE_EVAL) { eval("?>{$content}"); } else { echo $content; } } } } // Success! Return the id if (false === $this->use_pod_id) { return $params->tbl_row_id; } return $params->pod_id; }
/** * Change the value or perform actions after validation but before saving to the DB * * @param mixed $value * @param int $id * @param string $name * @param array $options * @param array $fields * @param array $pod * @param object $params * * @return mixed|string * @since 2.0 */ public function pre_save($value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null) { $index = pods_var('pod_index', pods_var('options', $pod, $pod, null, true), 'id', null, true); if (empty($value) && isset($fields[$index])) { $value = $fields[$index]['value']; } $value = pods_unique_slug($value, $name, $pod, 0, $params->id, null, false); return $value; }