Example #1
1
 /**
  * @since 2.0.11
  */
 public static function update_single_field($atts)
 {
     if (empty($atts['entry_id'])) {
         return;
     }
     $field = $atts['field_id'];
     FrmField::maybe_get_field($field);
     if (!$field) {
         return;
     }
     if (isset($field->field_options['post_field']) && !empty($field->field_options['post_field'])) {
         $post_id = FrmDb::get_var('frm_items', array('id' => $atts['entry_id']), 'post_id');
     } else {
         $post_id = false;
     }
     global $wpdb;
     if (!$post_id) {
         $updated = FrmEntryMeta::update_entry_meta($atts['entry_id'], $field->id, null, $atts['value']);
         if (!$updated) {
             $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}frm_item_metas WHERE item_id = %d and field_id = %d", $atts['entry_id'], $field->id));
             $updated = FrmEntryMeta::add_entry_meta($atts['entry_id'], $field->id, '', $atts['value']);
         }
         wp_cache_delete($atts['entry_id'], 'frm_entry');
     } else {
         switch ($field->field_options['post_field']) {
             case 'post_custom':
                 $updated = update_post_meta($post_id, $field->field_options['custom_field'], maybe_serialize($atts['value']));
                 break;
             case 'post_category':
                 $taxonomy = !FrmField::is_option_empty($field, 'taxonomy') ? $field->field_options['taxonomy'] : 'category';
                 $updated = wp_set_post_terms($post_id, $atts['value'], $taxonomy);
                 break;
             default:
                 $post = get_post($post_id, ARRAY_A);
                 $post[$field->field_options['post_field']] = maybe_serialize($atts['value']);
                 $updated = wp_insert_post($post);
                 break;
         }
     }
     if ($updated) {
         // set updated_at time
         $wpdb->update($wpdb->prefix . 'frm_items', array('updated_at' => current_time('mysql', 1), 'updated_by' => get_current_user_id()), array('id' => $atts['entry_id']));
     }
     $atts['field_id'] = $field->id;
     $atts['field'] = $field;
     do_action('frm_after_update_field', $atts);
     return $updated;
 }
 /**
  * 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);
             }
         }
     }
 }
Example #3
0
 /**
  * After an entry is duplicated, also duplicate the sub entries
  * @since 2.0
  */
 public static function duplicate_sub_entries($entry_id, $form_id, $args)
 {
     $form_fields = FrmProFormsHelper::has_field('form', $form_id, false);
     $section_fields = FrmProFormsHelper::has_repeat_field($form_id, false);
     $form_fields = array_merge($section_fields, $form_fields);
     if (empty($form_fields)) {
         // there are no fields for child entries
         return;
     }
     $entry = FrmEntry::getOne($entry_id, true);
     $sub_ids = array();
     foreach ($form_fields as $field) {
         if (!isset($entry->metas[$field->id])) {
             continue;
         }
         $field_ids = array();
         $ids = maybe_unserialize($entry->metas[$field->id]);
         if (!empty($ids)) {
             // duplicate all entries for this field
             foreach ((array) $ids as $sub_id) {
                 $field_ids[] = FrmEntry::duplicate($sub_id);
                 unset($sub_id);
             }
             FrmEntryMeta::update_entry_meta($entry_id, $field->id, null, $field_ids);
             $sub_ids = array_merge($field_ids, $sub_ids);
         }
         unset($field, $field_ids);
     }
     if (!empty($sub_ids)) {
         // update the parent id for new entries
         global $wpdb;
         $where = array('id' => $sub_ids);
         FrmDb::get_where_clause_and_values($where);
         array_unshift($where['values'], $entry_id);
         $wpdb->query($wpdb->prepare('UPDATE ' . $wpdb->prefix . 'frm_items SET parent_item_id = %d ' . $where['where'], $where['values']));
     }
 }