Exemplo n.º 1
0
 /**
  * Sets order of stops in the currently loaded tour to the order of stop_ids as set in $pa_stop_ids
  *
  * @param array $pa_stop_ids A list of stop_ids in the tour, in the order in which they should be displayed in the ui
  * @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 reorderStops($pa_stop_ids, $pa_options = null)
 {
     if (!($vn_tour_id = $this->getPrimaryKey())) {
         return null;
     }
     $va_stop_ranks = $this->getStopIDRanks($pa_options);
     // get current ranks
     $vn_i = 0;
     $vb_web_set_transaction = false;
     if (!$this->inTransaction()) {
         $o_trans = new Transaction($this->getDb());
         $vb_web_set_transaction = true;
     } else {
         $o_trans = $this->getTransaction();
     }
     $t_stop = new ca_tour_stops();
     $t_stop->setTransaction($o_trans);
     $t_stop->setMode(ACCESS_WRITE);
     $va_errors = array();
     // delete rows not present in $pa_stop_ids
     $va_to_delete = array();
     foreach ($va_stop_ranks as $vn_stop_id => $va_rank) {
         if (!in_array($vn_stop_id, $pa_stop_ids)) {
             if ($t_stop->load(array('tour_id' => $vn_tour_id, 'stop_id' => $vn_stop_id))) {
                 $t_stop->delete(true);
             }
         }
     }
     // rewrite ranks
     foreach ($pa_stop_ids as $vn_rank => $vn_stop_id) {
         if (isset($va_stop_ranks[$vn_stop_id]) && $t_stop->load(array('tour_id' => $vn_tour_id, 'stop_id' => $vn_stop_id))) {
             if ($va_stop_ranks[$vn_stop_id] != $vn_rank) {
                 $t_stop->set('rank', $vn_rank);
                 $t_stop->update();
                 if ($t_stop->numErrors()) {
                     $va_errors[$vn_stop_id] = _t('Could not reorder stop %1: %2', $vn_stop_id, join('; ', $t_stop->getErrors()));
                 }
             }
         }
     }
     if (sizeof($va_errors)) {
         if ($vb_web_set_transaction) {
             $o_trans->rollback();
         }
     } else {
         if ($vb_web_set_transaction) {
             $o_trans->commit();
         }
     }
     return $va_errors;
 }