/** * Move entries from parent form to child form * * @since 2.0.09 */ private static function move_entries_to_child_form($args) { global $wpdb; // get the ids of the entries saved in these fields $item_ids = FrmDb::get_col('frm_item_metas', array('field_id' => $args['children']), 'item_id', array('group_by' => 'item_id')); foreach ($item_ids as $old_id) { // Create a new entry in the child form $new_id = FrmEntry::create(array('form_id' => $args['form_id'], 'parent_item_id' => $old_id)); // Move the parent item_metas to the child form $where = array('item_id' => $old_id, 'field_id' => $args['children']); FrmDb::get_where_clause_and_values($where); array_unshift($where['values'], $new_id); $c = $wpdb->query($wpdb->prepare('UPDATE ' . $wpdb->prefix . 'frm_item_metas SET item_id = %d ' . $where['where'], $where['values'])); if ($c) { // update the section field meta with the new entry ID $u = FrmEntryMeta::update_entry_meta($old_id, $args['field_id'], null, $new_id); if (!$u) { // add the row if it wasn't there to update FrmEntryMeta::add_entry_meta($old_id, $args['field_id'], null, $new_id); } } } }
public static function update_entry_metas($entry_id, $values) { global $wpdb; $prev_values = FrmDb::get_col($wpdb->prefix . 'frm_item_metas', array('item_id' => $entry_id, 'field_id !' => 0), 'field_id'); foreach ($values as $field_id => $meta_value) { // set the value for the file upload field and add new tags (in Pro version) $values = apply_filters('frm_prepare_data_before_db', $values, $field_id, $entry_id); if ($prev_values && in_array($field_id, $prev_values)) { if (is_array($meta_value) && empty($meta_value) || !is_array($meta_value) && trim($meta_value) == '') { // remove blank fields unset($values[$field_id]); } else { // if value exists, then update it self::update_entry_meta($entry_id, $field_id, '', $values[$field_id]); } } else { // if value does not exist, then create it self::add_entry_meta($entry_id, $field_id, '', $values[$field_id]); } } if (empty($prev_values)) { return; } $prev_values = array_diff($prev_values, array_keys($values)); if (empty($prev_values)) { return; } // prepare the query $where = array('item_id' => $entry_id, 'field_id' => $prev_values); FrmDb::get_where_clause_and_values($where); // Delete any leftovers $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'])); self::clear_cache(); }
public static function prepend_and_or_where($starts_with = ' WHERE ', $where = '') { if (empty($where)) { return ''; } if (is_array($where)) { global $wpdb; FrmDb::get_where_clause_and_values($where, $starts_with); $where = $wpdb->prepare($where['where'], $where['values']); } else { $where = $starts_with . $where; } return $where; }
function get_records($table, $args = array(), $order_by = '', $limit = '', $fields = '*') { global $wpdb; extract(FrmDb::get_where_clause_and_values($args)); if (!empty($order_by)) { $order_by = " ORDER BY {$order_by}"; } if (!empty($limit)) { $limit = " LIMIT {$limit}"; } $query = "SELECT {$fields} FROM {$table}{$where}{$order_by}{$limit}"; $query = $wpdb->prepare($query, $values); return $wpdb->get_results($query); }
/** * @since 2.0.8 */ private static function add_where_to_query($add_where, &$where_clause) { if (is_array($where_clause)) { $where_clause[] = $add_where; } else { global $wpdb; $where = ''; $values = array(); FrmDb::parse_where_from_array($add_where, '', $where, $values); FrmDb::get_where_clause_and_values($add_where); $where_clause .= ' AND (' . $wpdb->prepare($where, $values) . ')'; } }
/** * @param string $status * @return int|boolean */ public static function set_status($id, $status) { if ('trash' == $status) { return self::trash($id); } $statuses = array('published', 'draft', 'trash'); if (!in_array($status, $statuses)) { return false; } global $wpdb; if (is_array($id)) { $where = array('id' => $id); FrmDb::get_where_clause_and_values($where); array_unshift($where['values'], $status); $query_results = $wpdb->query($wpdb->prepare('UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'])); } else { $query_results = $wpdb->update($wpdb->prefix . 'frm_forms', array('status' => $status), array('id' => $id)); } if ($query_results) { self::clear_form_cache(); } return $query_results; }
/** * delete entry meta so it won't be duplicated */ private static function delete_duplicated_meta($action, $entry) { global $wpdb; $field_ids = array(); foreach ($action->post_content as $name => $value) { // Don't try to delete meta for the display ID since this is never a field ID if ($name == 'display_id') { continue; } if (is_numeric($value)) { $field_ids[] = $value; } else { if (is_array($value) && isset($value['field_id']) && is_numeric($value['field_id'])) { $field_ids[] = $value['field_id']; } } unset($name, $value); } if (!empty($field_ids)) { $where = array('item_id' => $entry->id, 'field_id' => $field_ids); FrmDb::get_where_clause_and_values($where); $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->prefix . 'frm_item_metas' . $where['where'], $where['values'])); } }
function widget($args, $instance) { global $wpdb; $display = FrmProDisplay::getOne($instance['display_id'], false, true); $title = apply_filters('widget_title', empty($instance['title']) && $display ? $display->post_title : $instance['title']); $limit = empty($instance['limit']) ? ' LIMIT 100' : " LIMIT {$instance['limit']}"; $post_id = !$display || empty($display->frm_post_id) ? $instance['post_id'] : $display->frm_post_id; $page_url = get_permalink($post_id); $order_by = ''; $cat_field = false; if ($display && is_numeric($display->frm_form_id) && !empty($display->frm_form_id)) { //Set up order for Entries List Widget if (isset($display->frm_order_by) && !empty($display->frm_order_by)) { //Get only the first order field and order $order_field = reset($display->frm_order_by); $order = reset($display->frm_order); FrmAppHelper::esc_order_by($order); if ($order_field == 'rand') { //If random is set, set the order to random $order_by = ' RAND()'; } else { if (is_numeric($order_field)) { //If ordering by a field //Get all post IDs for this form $posts = FrmDb::get_results($wpdb->prefix . 'frm_items', array('form_id' => $display->frm_form_id, 'post_id >' => 1, 'is_draft' => 0), 'id, post_id'); $linked_posts = array(); foreach ($posts as $post_meta) { $linked_posts[$post_meta->post_id] = $post_meta->id; } //Get all field information $o_field = FrmField::getOne($order_field); $query = 'SELECT m.id FROM ' . $wpdb->prefix . 'frm_items m INNER JOIN '; $where = array(); //create query with ordered values //if field is some type of post field if (isset($o_field->field_options['post_field']) && $o_field->field_options['post_field']) { if ($o_field->field_options['post_field'] == 'post_custom' && !empty($linked_posts)) { //if field is custom field $where['pm.post_id'] = array_keys($linked_posts); FrmDb::get_where_clause_and_values($where); array_unshift($where['values'], $o_field->field_options['custom_field']); $query .= $wpdb->postmeta . ' pm ON pm.post_id=m.post_id AND pm.meta_key=%s ' . $where['where'] . ' ORDER BY CASE when pm.meta_value IS NULL THEN 1 ELSE 0 END, pm.meta_value ' . $order; } else { if ($o_field->field_options['post_field'] != 'post_category' && !empty($linked_posts)) { //if field is a non-category post field $where['p.ID'] = array_keys($linked_posts); FrmDb::get_where_clause_and_values($where); $query .= $wpdb->posts . ' p ON p.ID=m.post_id ' . $where['where'] . ' ORDER BY CASE p.' . sanitize_title($o_field->field_options['post_field']) . ' WHEN "" THEN 1 ELSE 0 END, p.' . sanitize_title($o_field->field_options['post_field']) . ' ' . $order; } } } else { //if field is a normal, non-post field $where['em.field_id'] = $o_field->id; FrmDb::get_where_clause_and_values($where); $query .= $wpdb->prefix . 'frm_item_metas em ON em.item_id=m.id ' . $where['where'] . ' ORDER BY CASE when em.meta_value IS NULL THEN 1 ELSE 0 END, em.meta_value' . ($o_field->type == 'number' ? ' +0 ' : '') . ' ' . $order; } //Get ordered values if (!empty($where)) { $metas = $wpdb->get_results($wpdb->prepare($query, $where['values'])); } else { $metas = false; } unset($query, $where); if (!empty($metas)) { $order_by_array = array(); foreach ($metas as $meta) { $order_by_array[] = $wpdb->prepare('it.id=%d DESC', $meta->id); } $order_by = implode(', ', $order_by_array); unset($order_by_array); } else { $order_by .= 'it.created_at ' . $order; } unset($metas); } else { if (!empty($order_field)) { //If ordering by created_at or updated_at $order_by = 'it.' . sanitize_title($order_field) . ' ' . $order; } } } if (!empty($order_by)) { $order_by = ' ORDER BY ' . $order_by; } } if (isset($instance['cat_list']) && (int) $instance['cat_list'] == 1 && is_numeric($instance['cat_id'])) { if ($cat_field = FrmField::getOne($instance['cat_id'])) { $categories = maybe_unserialize($cat_field->options); } } } echo $args['before_widget']; if ($title) { echo $args['before_title'] . $title . $args['after_title']; } echo '<ul id="frm_entry_list' . ($display ? $display->frm_form_id : '') . '">' . "\n"; //if Listing entries by category if (isset($instance['cat_list']) && (int) $instance['cat_list'] == 1 && isset($categories) && is_array($categories)) { foreach ($categories as $cat_order => $cat) { if ($cat == '') { continue; } echo '<li>'; if (isset($instance['cat_name']) && (int) $instance['cat_name'] == 1 && $cat_field) { echo '<a href="' . esc_url(add_query_arg(array('frm_cat' => $cat_field->field_key, 'frm_cat_id' => $cat_order), $page_url)) . '">'; } echo $cat; if (isset($instance['cat_count']) && (int) $instance['cat_count'] == 1) { echo ' (' . FrmProFieldsHelper::get_field_stats($instance['cat_id'], 'count', false, $cat) . ')'; } if (isset($instance['cat_name']) && (int) $instance['cat_name'] == 1) { echo '</a>'; } else { $entry_ids = FrmEntryMeta::getEntryIds(array('meta_value like' => $cat, 'fi.id' => $instance['cat_id'])); $items = false; if ($entry_ids) { $items = FrmEntry::getAll(array('it.id' => $entry_ids, 'it.form_id' => (int) $display->frm_form_id), $order_by, $limit); } if ($items) { echo '<ul>'; foreach ($items as $item) { $url_id = $display->frm_type == 'id' ? $item->id : $item->item_key; $current = FrmAppHelper::simple_get($display->frm_param) == $url_id ? ' class="current_page"' : ''; if ($item->post_id) { $entry_link = get_permalink($item->post_id); } else { $entry_link = add_query_arg(array($display->frm_param => $url_id), $page_url); } echo '<li' . $current . '><a href="' . esc_url($entry_link) . '">' . FrmAppHelper::kses($item->name) . '</a></li>' . "\n"; } echo '</ul>'; } } echo '</li>'; } } else { // if not listing entries by category if ($display) { $items = FrmEntry::getAll(array('it.form_id' => $display->frm_form_id, 'is_draft' => '0'), $order_by, $limit); } else { $items = array(); } foreach ($items as $item) { $url_id = $display->frm_type == 'id' ? $item->id : $item->item_key; $current = FrmAppHelper::simple_get($display->frm_param) == $url_id ? ' class="current_page"' : ''; echo '<li' . $current . '><a href="' . esc_url(add_query_arg(array($display->frm_param => $url_id), $page_url)) . '">' . FrmAppHelper::kses($item->name) . '</a></li>' . "\n"; } } echo "</ul>\n"; echo $args['after_widget']; }
/** * After the sub entry and parent entry are created, we can update the parent id field * @since 2.0 */ public static function update_parent_id($entry_id, $form_id) { $form_fields = FrmProFormsHelper::has_field('form', $form_id, false); $section_fields = FrmProFormsHelper::has_repeat_field($form_id, false); if (!$form_fields && !$section_fields) { return; } $form_fields = array_merge($section_fields, $form_fields); $entry = FrmEntry::getOne($entry_id, true); if (!$entry || $entry->form_id != $form_id) { return; } $sub_ids = array(); foreach ($form_fields as $field) { if (!isset($entry->metas[$field->id])) { continue; } $ids = maybe_unserialize($entry->metas[$field->id]); if (!empty($ids)) { $sub_ids = array_merge($ids, $sub_ids); } unset($field); } if (!empty($sub_ids)) { $where = array('id' => $sub_ids); FrmDb::get_where_clause_and_values($where); array_unshift($where['values'], $entry_id); global $wpdb; $wpdb->query($wpdb->prepare('UPDATE ' . $wpdb->prefix . 'frm_items SET parent_item_id = %d' . $where['where'], $where['values'])); } }