/** * Delete the value from the DB * * @param int $id * @param string $name * @param array $options * @param array $pod * * @since 2.3 */ public function delete($id = null, $name = null, $options = null, $pod = null) { if (empty(self::$api)) { self::$api = pods_api(); } $simple_tableless_objects = $this->simple_objects(); // Bidirectional relationship requirement checks $related_object = pods_var(self::$type . '_object', $options, ''); // pod, post_type, taxonomy, etc.. $related_val = pods_var(self::$type . '_val', $options, $related_object, null, true); // pod name, post type name, taxonomy name, etc.. $related_sister_id = (int) pods_var('sister_id', $options, 0); if (!empty($related_sister_id) && !in_array($related_object, $simple_tableless_objects)) { $related_pod = self::$api->load_pod(array('name' => $related_val, 'table_info' => false), false); if (false !== $related_pod && ('pod' == $related_object || $related_object == $related_pod['type'])) { $related_field = false; // Ensure sister_id exists on related Pod foreach ($related_pod['fields'] as $related_pod_field) { if ('pick' == $related_pod_field['type'] && $related_sister_id == $related_pod_field['id']) { $related_field = $related_pod_field; break; } } if (!empty($related_field)) { $values = self::$api->lookup_related_items($options['id'], $pod['id'], $id, $options, $pod); if (!empty($values)) { $no_conflict = pods_no_conflict_check($related_pod['type']); if (!$no_conflict) { pods_no_conflict_on($related_pod['type']); } self::$api->delete_relationships($values, $id, $related_pod, $related_field); if (!$no_conflict) { pods_no_conflict_off($related_pod['type']); } } } } } }
/** * Remove an item from the values of a relationship field, remove a value from a number field (field-1), remove time to a date field * * @see PodsAPI::save_pod_item * * @param string $field Field name * @param mixed $value ID(s) to add, int|float to add to number field, string for dates (-1 week), or string for text * @param int $id (optional) ID of the pod item to update * * @return int The item ID * * @since 2.3.3 */ public function remove_from($field, $value = null, $id = null) { $pod =& $this; $fetch = false; if (null === $id) { $fetch = true; $id = $this->id(); } elseif ($id != $this->id()) { $pod = pods($this->pod, $id); } $this->do_hook('remove_from', $field, $value, $id); if (!isset($this->fields[$field])) { return $id; } // Tableless fields if (in_array($this->fields[$field]['type'], PodsForm::tableless_field_types())) { if (empty($value)) { $value = array(); } if (!empty($value)) { if (!is_array($value)) { $value = explode(',', $value); } if ('pick' == $this->fields[$field]['type'] && in_array($this->fields[$field]['pick_object'], PodsForm::field_method('pick', 'simple_objects'))) { $current_value = $pod->raw($field); if (!empty($current_value)) { $current_value = (array) $current_value; } foreach ($current_value as $k => $v) { if (in_array($v, $value)) { unset($current_value[$k]); } } $value = $current_value; } else { $related_ids = $this->api->lookup_related_items($this->fields[$field]['id'], $this->pod_data['id'], $id, $this->fields[$field], $this->pod_data); foreach ($value as $k => $v) { if (!preg_match('/[^0-9]/', $v)) { $value[$k] = (int) $v; } else { } } foreach ($related_ids as $k => $v) { if (in_array($v, $value)) { unset($related_ids[$k]); } } $value = $related_ids; } if (!empty($value)) { $value = array_filter(array_unique($value)); } else { $value = array(); } } } elseif (in_array($this->fields[$field]['type'], PodsForm::number_field_types())) { // Date fields don't support empty for removing if (empty($value)) { return $id; } $current_value = (double) $pod->raw($field); $value = $current_value - (double) $value; } elseif (in_array($this->fields[$field]['type'], PodsForm::date_field_types())) { // Date fields don't support empty for removing if (empty($value)) { return $id; } $current_value = $pod->raw($field); if (0 < strlen($current_value)) { $value = strtotime($value, strtotime($current_value)); } else { $value = strtotime($value); } $value = date_i18n('Y-m-d h:i:s', $value); } // @todo handle object fields and taxonomies $params = array('pod' => $this->pod, 'id' => $id, 'data' => array($field => $value)); $id = $this->api->save_pod_item($params); if (0 < $id && $fetch) { $pod->fetch($id, false); } return $id; }