Ejemplo n.º 1
0
 public function handleActions()
 {
     // handle save listing
     if ($this->requestAction() == 'save_listing') {
         $this->saveListing();
     }
     // set account_id
     $account_id = isset($_REQUEST['auction']) ? WPLE_ListingQueryHelper::getAccountID($_REQUEST['auction']) : false;
     // handle verify action
     if ($this->requestAction() == 'verify') {
         $this->initEC($account_id);
         $this->EC->verifyItems($_REQUEST['auction']);
         $this->EC->closeEbay();
         if ($this->EC->isSuccess) {
             $this->showMessage(__('Selected items were verified with eBay.', 'wplister'));
         } else {
             $this->showMessage(__('There were some problems verifying your items.', 'wplister'), 1);
         }
     }
     // handle revise action
     if ($this->requestAction() == 'revise') {
         $this->initEC($account_id);
         $this->EC->reviseItems($_REQUEST['auction']);
         $this->EC->closeEbay();
         if ($this->EC->isSuccess) {
             $this->showMessage(__('Selected items were revised on eBay.', 'wplister'));
         } else {
             $this->showMessage(__('There were some problems revising your items.', 'wplister'), 1);
         }
     }
     // handle publish to eBay action
     if ($this->requestAction() == 'publish2e') {
         $this->initEC($account_id);
         $this->EC->sendItemsToEbay($_REQUEST['auction']);
         $this->EC->closeEbay();
         if ($this->EC->isSuccess) {
             $this->showMessage(__('Selected items were published on eBay.', 'wplister'));
         } else {
             $this->showMessage(__('Some items could not be published.', 'wplister'), 1);
         }
     }
     // handle relist action
     if ($this->requestAction() == 'relist') {
         $this->initEC($account_id);
         $this->EC->relistItems($_REQUEST['auction']);
         $this->EC->closeEbay();
         if ($this->EC->isSuccess) {
             $this->showMessage(__('Selected items were re-listed on eBay.', 'wplister'));
         } else {
             $this->showMessage(__('There were some problems relisting your items.', 'wplister'), 1);
         }
     }
     // handle end_item action
     if ($this->requestAction() == 'end_item') {
         $this->initEC($account_id);
         $this->EC->endItemsOnEbay($_REQUEST['auction']);
         $this->EC->closeEbay();
         $this->showMessage(__('Selected listings were ended.', 'wplister'));
     }
     // handle update from eBay action
     if ($this->requestAction() == 'update') {
         $this->initEC($account_id);
         $this->EC->updateItemsFromEbay($_REQUEST['auction']);
         $this->EC->closeEbay();
         $this->showMessage(__('Selected items were updated from eBay.', 'wplister'));
     }
     // handle delete action
     if (isset($_REQUEST['auction']) && $this->requestAction() == 'delete_listing') {
         $id = $_REQUEST['auction'];
         if (is_array($id)) {
             foreach ($id as $single_id) {
                 WPLE_ListingQueryHelper::deleteItem($single_id);
             }
         } else {
             WPLE_ListingQueryHelper::deleteItem($id);
         }
         $this->showMessage(__('Selected items were removed.', 'wplister'));
     }
     // handle archive action
     if ($this->requestAction() == 'archive') {
         $id = $_REQUEST['auction'];
         $data = array('status' => 'archived');
         if (is_array($id)) {
             foreach ($id as $single_id) {
                 ListingsModel::updateListing($single_id, $data);
             }
         } else {
             ListingsModel::updateListing($id, $data);
         }
         $this->showMessage(__('Selected items were archived.', 'wplister'));
     }
     // handle wple_reset_status action
     if ($this->requestAction() == 'wple_reset_status') {
         $lm = new ListingsModel();
         $id = $_REQUEST['auction'];
         $data = array('status' => 'prepared', 'ebay_id' => NULL, 'end_date' => NULL, 'date_published' => NULL, 'last_errors' => '');
         if (is_array($id)) {
             foreach ($id as $single_id) {
                 $status = WPLE_ListingQueryHelper::getStatus($single_id);
                 if (!in_array($status, array('ended', 'sold', 'archived'))) {
                     wple_show_message("Item with status <i>{$status}</i> was skipped. Only ended and sold items can have their status reset to <i>prepared</i>.", 'warn');
                     continue;
                 }
                 ListingsModel::updateListing($single_id, $data);
                 $lm->reapplyProfileToItem($single_id);
             }
             wple_show_message(__('Selected items had their status reset to prepared.', 'wplister'));
         }
     }
     // handle wple_clear_eps_data action
     if ($this->requestAction() == 'wple_clear_eps_data') {
         $id = $_REQUEST['auction'];
         if (is_array($id)) {
             foreach ($id as $single_id) {
                 ListingsModel::updateWhere(array('id' => $single_id), array('eps' => ''));
             }
             wple_show_message(__('EPS cache was cleared for selected items.', 'wplister'));
         }
     }
     // handle lock action
     if ($this->requestAction() == 'lock') {
         $id = $_REQUEST['auction'];
         $data = array('locked' => true);
         if (is_array($id)) {
             foreach ($id as $single_id) {
                 ListingsModel::updateListing($single_id, $data);
             }
         } else {
             ListingsModel::updateListing($id, $data);
         }
         $this->showMessage(__('Selected items were locked.', 'wplister'));
     }
     // handle unlock action
     if ($this->requestAction() == 'unlock') {
         $id = $_REQUEST['auction'];
         $data = array('locked' => false);
         if (is_array($id)) {
             foreach ($id as $single_id) {
                 ListingsModel::updateListing($single_id, $data);
             }
         } else {
             ListingsModel::updateListing($id, $data);
         }
         $this->showMessage(__('Selected items were unlocked.', 'wplister'));
     }
     // handle cancel_schedule action
     if ($this->requestAction() == 'cancel_schedule') {
         $id = $_REQUEST['auction'];
         $data = array('relist_date' => null);
         if (is_array($id)) {
             foreach ($id as $single_id) {
                 ListingsModel::updateListing($single_id, $data);
             }
         } else {
             ListingsModel::updateListing($id, $data);
         }
         $this->showMessage(__('Selected items were unscheduled from auto relist.', 'wplister'));
     }
     // clean listing archive
     if ($this->requestAction() == 'wpl_clean_listing_archive') {
         WPLE_ListingQueryHelper::cleanArchive();
         $this->showMessage(__('Archive was cleared.', 'wplister'));
     }
     // handle toolbar action - prepare listing from product
     if ($this->requestAction() == 'wpl_prepare_single_listing') {
         // get profile
         $profilesModel = new ProfilesModel();
         $profile = isset($_REQUEST['profile_id']) ? $profilesModel->getItem($_REQUEST['profile_id']) : false;
         if ($profile) {
             // prepare product
             $listingsModel = new ListingsModel();
             $listing_id = $listingsModel->prepareProductForListing($_REQUEST['product_id'], $profile['profile_id']);
             if ($listing_id) {
                 $listingsModel->applyProfileToNewListings($profile);
                 $this->showMessage(__('New listing was prepared from product.', 'wplister'));
             } else {
                 $msg = __('Could not create a new listing from this product.', 'wplister');
                 if ($listingsModel->errors) {
                     $msg .= '<br>' . join('<br>', $listingsModel->errors);
                 }
                 if ($listingsModel->warnings) {
                     $msg .= '<br>' . join('<br>', $listingsModel->warnings);
                 }
                 $this->showMessage($msg, 2);
             }
         } elseif (isset($_REQUEST['product_id'])) {
             // prepare product
             $listingsModel = new ListingsModel();
             $listingsModel->prepareProductForListing($_REQUEST['product_id']);
         }
     }
     // handle reapply profile action
     if ($this->requestAction() == 'reapply') {
         $listingsModel = new ListingsModel();
         $listingsModel->reapplyProfileToItems($_REQUEST['auction']);
         $this->showMessage(__('Profiles were re-applied to selected items.', 'wplister'));
     }
     // cancel (re-)selecting profile process
     if ($this->requestAction() == 'cancel_profile_selection') {
         ListingsModel::cancelSelectingListings();
     }
 }
Ejemplo n.º 2
0
 public function applyProfileToItem($profile, $item, $update_title = true)
 {
     global $wpdb;
     // get item data
     $id = $item['id'];
     $post_id = $item['post_id'];
     $status = WPLE_ListingQueryHelper::getStatus($id);
     $ebay_id = self::getEbayIDFromID($id);
     $post_title = get_the_title($item['post_id']);
     WPLE()->logger->info("applyProfileToItem() - listing_id: {$id} / post_id: {$post_id}");
     // WPLE()->logger->callStack( debug_backtrace() );
     // skip ended auctions - or not, if you want to relist them...
     // if ( $status == 'ended' ) return;
     // use parent title for single (split) variation
     if (ProductWrapper::isSingleVariation($post_id)) {
         $parent_id = ProductWrapper::getVariationParent($post_id);
         $post_title = get_the_title($parent_id);
         // check if parent product has a custom eBay title set
         if (get_post_meta($parent_id, '_ebay_title', true)) {
             $post_title = trim(get_post_meta($parent_id, '_ebay_title', true));
         }
         // get variations
         $variations = ProductWrapper::getVariations($parent_id);
         // find this variation in all variations of this parent
         foreach ($variations as $var) {
             if ($var['post_id'] == $post_id) {
                 // append attribute values to title
                 $post_title = self::processSingleVariationTitle($post_title, $var['variation_attributes']);
             }
         }
     }
     // gather profile data
     $data = array();
     $data['profile_id'] = $profile['profile_id'];
     $data['account_id'] = $profile['account_id'];
     $data['site_id'] = $profile['site_id'];
     $data['auction_type'] = $profile['type'];
     $data['listing_duration'] = $profile['listing_duration'];
     $data['template'] = $profile['details']['template'];
     $data['quantity'] = $profile['details']['quantity'];
     $data['date_created'] = date('Y-m-d H:i:s');
     $data['profile_data'] = self::encodeObject($profile);
     // echo "<pre>";print_r($data);echo"</pre>";die();
     // add prefix and suffix to product title
     if ($update_title) {
         // append space to prefix, prepend space to suffix
         // TODO: make this an option
         $title_prefix = trim($profile['details']['title_prefix']) . ' ';
         $title_suffix = ' ' . trim($profile['details']['title_suffix']);
         // custom post meta fields override profile values
         if (get_post_meta($post_id, 'ebay_title_prefix', true)) {
             $title_prefix = trim(get_post_meta($post_id, 'ebay_title_prefix', true)) . ' ';
         }
         if (get_post_meta($post_id, 'ebay_title_suffix', true)) {
             $title_prefix = trim(get_post_meta($post_id, 'ebay_title_suffix', true)) . ' ';
         }
         $data['auction_title'] = trim($title_prefix . $post_title . $title_suffix);
         // custom post meta title override
         if (get_post_meta($post_id, '_ebay_title', true)) {
             $data['auction_title'] = trim(get_post_meta($post_id, '_ebay_title', true));
         } elseif (get_post_meta($post_id, 'ebay_title', true)) {
             $data['auction_title'] = trim(get_post_meta($post_id, 'ebay_title', true));
         }
         // process attribute shortcodes in title - like [[attribute_Brand]]
         if (strpos($data['auction_title'], ']]') > 0) {
             $templatesModel = new TemplatesModel();
             WPLE()->logger->info('auction_title before processing: ' . $data['auction_title'] . '');
             $data['auction_title'] = $templatesModel->processAllTextShortcodes($item['post_id'], $data['auction_title'], 80);
         }
         WPLE()->logger->info('auction_title after processing : ' . $data['auction_title'] . '');
         // trim title to 255 characters - longer titles will break $wpdb->update()
         if (strlen($data['auction_title']) > 255) {
             $data['auction_title'] = self::mb_substr($data['auction_title'], 0, 80);
             // eBay titles can not be longer than 80 characters
         }
     }
     // apply profile price
     $data['price'] = ProductWrapper::getPrice($post_id);
     $data['price'] = self::applyProfilePrice($data['price'], $profile['details']['start_price']);
     // fetch product stock if no quantity set in profile - and apply max_quantity limit
     if (intval($data['quantity']) == 0) {
         $max = isset($profile['details']['max_quantity']) && intval($profile['details']['max_quantity']) > 0 ? $profile['details']['max_quantity'] : PHP_INT_MAX;
         $data['quantity'] = min($max, intval(ProductWrapper::getStock($post_id)));
         // update listing quantity properly - using setListingQuantity() which regards current quantity_sold
         self::setListingQuantity($post_id, $data['quantity']);
         unset($data['quantity']);
     }
     // default new status is 'prepared'
     $data['status'] = 'prepared';
     // except for already published items where it is 'changed'
     if (intval($ebay_id) > 0) {
         $data['status'] = 'changed';
     }
     // ended items stay 'ended' and sold items stay sold
     if ($status == 'ended') {
         $data['status'] = 'ended';
     }
     if ($status == 'sold') {
         $data['status'] = 'sold';
     }
     if ($status == 'archived') {
         $data['status'] = 'archived';
     }
     // locked items simply keep their status
     if (@$item['locked']) {
         $data['status'] = $status;
     }
     // but if apply_changes_to_all_locked checkbox is ticked, even locked published items will be marked as 'changed'
     if (@$item['locked'] && $status == 'published' && isset($_POST['wpl_e2e_apply_changes_to_all_locked'])) {
         $data['status'] = 'changed';
     }
     // except for selected items which shouldn't be locked in the first place
     if ($status == 'selected') {
         $data['status'] = 'prepared';
     }
     // and reselected items which have already been 'ended'
     if ($status == 'reselected') {
         $data['status'] = 'ended';
     }
     // and items which have already been 'changed' and now had a new profile applied
     if ($status == 'changed_profile') {
         $data['status'] = 'changed';
     }
     // debug
     if ($status != $data['status']) {
         WPLE()->logger->info('applyProfileToItem(' . $id . ') old status: ' . $status);
         WPLE()->logger->info('applyProfileToItem(' . $id . ') new status: ' . $data['status']);
     }
     // update auctions table
     $wpdb->update($this->tablename, $data, array('id' => $id));
     // WPLE()->logger->info('updating listing ID '.$id);
     // WPLE()->logger->info('data: '.print_r($data,1));
     // WPLE()->logger->info('sql: '.$wpdb->last_query);
     // WPLE()->logger->info('error: '.$wpdb->last_error);
 }