Ejemplo n.º 1
0
 /**
  * Sets order of items in the currently loaded order to the order of item_ids as set in $pa_item_ids
  *
  * @param array $pa_item_ids A list of item_ids in the order, in the order in which they should be displayed in the order
  * @param array $pa_options An optional array of options. Supported options include:
  *			NONE
  * @return array An array of errors. If the array is empty then no errors occurred
  */
 public function reorderItems($pa_item_ids, $pa_options = null)
 {
     if (!($vn_order_id = $this->getPrimaryKey())) {
         return null;
     }
     $va_item_ranks = $this->getItemIDRanks($pa_options);
     // get current ranks
     $vn_i = 0;
     $o_trans = new Transaction();
     $t_item = new ca_commerce_order_items();
     $t_item->setTransaction($o_trans);
     $t_item->setMode(ACCESS_WRITE);
     $va_errors = array();
     // rewrite ranks
     foreach ($pa_item_ids as $vn_rank => $vn_item_id) {
         if (isset($va_item_ranks[$vn_item_id]) && $t_item->load(array('order_id' => $vn_order_id, 'item_id' => $vn_item_id))) {
             if ($va_item_ranks[$vn_item_id] != $vn_rank) {
                 $t_item->set('rank', $vn_rank);
                 $t_item->update();
                 if ($t_item->numErrors()) {
                     $va_errors[$vn_item_id] = _t('Could not reorder item %1: %2', $vn_item_id, join('; ', $t_item->getErrors()));
                 }
             }
         }
     }
     if (sizeof($va_errors)) {
         $o_trans->rollback();
     } else {
         $o_trans->commit();
     }
     return $va_errors;
 }