Beispiel #1
0
 public function ajax_wple_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('wplister_default_account_id', 1);
         $lm = new ListingsModel();
         if ('products' == $select_mode) {
             // get profile
             $pm = new ProfilesModel();
             $profile = $pm->getItem($profile_id);
             // prepare new listings from products
             // $response = $lm->prepareListings( $product_ids, $profile_id );
             $response = $lm->prepareListings($product_ids, $profile_id);
             $lm->applyProfileToNewListings($profile);
         } elseif ('listings' == $select_mode) {
             // change profile for existing listings
             // $profile = WPLE_AmazonProfile::getProfile( $profile_id ); // doesn't work
             $pm = new ProfilesModel();
             $profile = $pm->getItem($profile_id);
             // $items = $lm->applyProfileToListings( $profile, $product_ids );
             foreach ($product_ids as $listing_id) {
                 $item = ListingsModel::getItem($listing_id);
                 $lm->applyProfileToItem($profile, $item);
             }
             // build response
             $response = new stdClass();
             // $response->success     = $prepared_count ? true : false;
             $response->success = true;
             $response->msg = sprintf(__('Profile "%s" was applied to %s items.', 'wplister'), $profile['profile_name'], count($product_ids));
             $this->returnJSON($response);
             exit;
         } else {
             die('invalid select mode: ' . $select_mode);
         }
         if ($response->success) {
             // store ASIN as product meta
             // update_post_meta( $post_id, '_wple_asin', $asin );
             // show message
             if ($response->skipped_count) {
                 $response->msg = sprintf(__('%s product(s) have been prepared and %s products were skipped.', 'wplister'), $response->prepared_count, $response->skipped_count);
             } else {
                 $response->msg = sprintf(__('%s product(s) have been prepared.', 'wplister'), $response->prepared_count);
             }
             // include link to prepared listings
             $response->msg .= '&nbsp; <a href="admin.php?page=wplister&listing_status=prepared" class="button button-small">' . __('View prepared listings', 'wplister') . '</a>';
             // show shorter message if no listings were prepared
             if (!$response->prepared_count) {
                 $response->msg = sprintf(__('%s products have been skipped.', 'wplister'), $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;
         }
     }
 }