/**
  * Create/update a batch based on input data submitted by user from the
  * Edit Batch page.
  *
  * @param Batch $batch
  * @param array $request_data Input data from the user. Should contain
  * two array keys:
  * 'batch_title' - Title of this batch.
  * 'posts' - Posts to include in this batch.
  */
 private function handle_edit_batch_form_data(Batch $batch, $request_data)
 {
     // Check if a title has been set.
     if (isset($request_data['batch_title']) && $request_data['batch_title']) {
         $batch->set_title($request_data['batch_title']);
     } else {
         $batch->set_title('Batch ' . date('Y-m-d H:i:s'));
     }
     if ($batch->get_id() <= 0) {
         // Create new batch.
         $this->batch_dao->insert($batch);
     } else {
         // Update existing batch.
         $batch->set_status('publish');
         $this->batch_dao->update_batch($batch);
     }
     // IDs of posts user has selected to include in this batch.
     $selected_post_ids = array();
     // Check if any posts to include in batch has been selected.
     if (isset($request_data['post_ids']) && $request_data['post_ids']) {
         $selected_post_ids = array_map('intval', explode(',', $request_data['post_ids']));
     }
     // Posts that was previously in this batch.
     $old_post_ids = $this->batch_dao->get_post_meta($batch->get_id(), 'sme_selected_post');
     // Post IDs to add to this batch.
     $add_post_ids = array_diff($selected_post_ids, $old_post_ids);
     // Post IDs to remove from this batch.
     $remove_post_ids = array_diff($old_post_ids, $selected_post_ids);
     // Add post IDs to batch.
     foreach ($add_post_ids as $post_id) {
         $this->batch_dao->add_post_meta($batch->get_id(), 'sme_selected_post', $post_id);
     }
     // Remove post IDs from batch.
     foreach ($remove_post_ids as $post_id) {
         $this->batch_dao->delete_post_meta($batch->get_id(), 'sme_selected_post', $post_id);
     }
 }
 /**
  * @param array $raw
  * @return Batch
  */
 protected function do_create_object(array $raw, $args = array())
 {
     $obj = new Batch($raw['ID']);
     $user = $this->user_dao->find($raw['post_author']);
     $obj->set_guid($raw['guid']);
     $obj->set_title($raw['post_title']);
     $obj->set_creator($user);
     $obj->set_date($raw['post_date']);
     $obj->set_date_gmt($raw['post_date_gmt']);
     $obj->set_modified($raw['post_modified']);
     $obj->set_modified_gmt($raw['post_modified_gmt']);
     $obj->set_status($raw['post_status']);
     // Skip loading batch content?
     $skip_content = isset($args['skip_content']) ? $args['skip_content'] : false;
     $content = $this->get_batch_content($raw, $skip_content);
     $content = unserialize(base64_decode($content));
     /*
      * Previously $content would have contained a Batch object. This check
      * deals with legacy batches.
      */
     if (!is_array($content)) {
         $content = array();
     }
     if (isset($content['attachments'])) {
         $obj->set_attachments($content['attachments']);
     }
     if (isset($content['users'])) {
         $obj->set_users($content['users']);
     }
     if (isset($content['posts'])) {
         $obj->set_posts($content['posts']);
     }
     if (isset($content['custom_data'])) {
         $obj->set_custom_data($content['custom_data']);
     }
     if (isset($content['post_rel_keys'])) {
         $obj->set_post_rel_keys($content['post_rel_keys']);
     }
     return $obj;
 }