private function saveProfile()
 {
     // init profile
     $profile_id = $this->getValueFromPost('profile_id');
     $profile = new WPLA_AmazonProfile($profile_id);
     // fill in post data
     $post_data = $this->getPreprocessedPostData();
     $profile->fillFromArray($post_data);
     // add field data
     $profile->fields = maybe_serialize($this->getPreprocessedPostData('tpl_col_', true));
     // insert or update
     if ($profile_id) {
         $profile->update();
         $this->showMessage(__('Profile updated.', 'wpla'));
     } else {
         $profile->add();
         $this->showMessage(__('Profile added.', 'wpla'));
     }
     // error handling
     // if ($result===false) {
     // 	$this->showMessage( "There was a problem saving your profile.<br>SQL:<pre>".$wpdb->last_query.'</pre>'.$wpdb->last_error, true );
     // } else {
     // }
     // prepare for updating items
     // $profile    = new WPLA_AmazonProfile( $profile_id );
     $listingsModel = new WPLA_ListingsModel();
     // re-apply profile to all published
     if (!$profile_id) {
         return;
     }
     $items = $listingsModel->getWhere('profile_id', $profile_id);
     $listingsModel->applyProfileToListings($profile, $items);
     $this->showMessage(sprintf(__('%s items updated.', 'wplister'), count($items)));
 }
 public function ajax_wpla_select_profile()
 {
     // TODO: check nonce
     if (isset($_REQUEST['profile_id']) && isset($_REQUEST['product_ids'])) {
         $profile_id = $_REQUEST['profile_id'];
         $product_ids = $_REQUEST['product_ids'];
         $select_mode = $_REQUEST['select_mode'];
         $default_account_id = get_option('wpla_default_account_id', 1);
         $lm = new WPLA_ListingsModel();
         if ('products' == $select_mode) {
             // prepare new listings from products
             $response = $lm->prepareListings($product_ids, $profile_id);
         } elseif ('listings' == $select_mode) {
             // remove profile?
             if ($profile_id == '_NONE_') {
                 $lm->removeProfileFromListings($product_ids);
                 // build response
                 $response = new stdClass();
                 $response->success = true;
                 $response->msg = sprintf(__('Profile was removed from %s items.', 'wpla'), count($product_ids));
                 $this->returnJSON($response);
                 exit;
             }
             // change profile for existing listings
             // $profile = WPLA_AmazonProfile::getProfile( $profile_id ); // doesn't work
             $profile = new WPLA_AmazonProfile($profile_id);
             $items = $lm->applyProfileToListings($profile, $product_ids);
             // build response
             $response = new stdClass();
             // $response->success     = $prepared_count ? true : false;
             $response->success = true;
             $response->msg = sprintf(__('Profile "%s" was applied to %s items.', 'wpla'), $profile->profile_name, count($items));
             $this->returnJSON($response);
             exit;
         } else {
             die('invalid select mode: ' . $select_mode);
         }
         if ($response->success) {
             // store ASIN as product meta
             // update_post_meta( $post_id, '_wpla_asin', $asin );
             $response->msg = sprintf(__('%s product(s) have been prepared.', 'wpla'), $response->prepared_count);
             if ($response->skipped_count) {
                 $response->msg = sprintf(__('%s product(s) have been prepared and %s products were skipped.', 'wpla'), $response->prepared_count, $response->skipped_count);
             }
             if (!$response->prepared_count) {
                 $response->msg = sprintf(__('%s products have been skipped.', 'wpla'), $response->skipped_count);
             }
             if ($response->errors) {
                 $response->msg .= '<br>' . join('<br>', $response->errors);
             }
             if ($response->warnings) {
                 $response->msg .= '<br>' . join('<br>', $response->warnings);
             }
             $this->returnJSON($response);
             exit;
         } else {
             if (isset($lm->lastError)) {
                 echo $lm->lastError . "\n";
             }
             echo "Failed to prepare product!";
             exit;
         }
     }
 }