示例#1
0
 /**
  * Create a copy of all input units. Copy will be exactly the same object with just
  * a different primary key value from main rm database table.
  *
  * @param ids - array from REQUEST
  * @return array
  */
 public function copyJsonAction()
 {
     $unitIDs = $this->_getParam('ids', array());
     $model = new RM_Units();
     $total = $model->fetchAll()->count();
     $trueCopy = array();
     $falseCopy = array();
     foreach ($unitIDs as $unitID) {
         $unit = $model->find($unitID)->current();
         if ($unit !== null) {
             try {
                 $model->copyRow($unit);
                 $trueCopy[] = $unitID;
             } catch (RM_Exception $e) {
                 $falseCopy[$unitID] = $e->getMessage();
             }
         }
     }
     if (count($falseCopy) == 0) {
         $success = true;
         $message = $this->_translate->_('Admin.Units.List', 'CopySuccess');
     } else {
         $success = false;
         $message = $this->_translate->_('Admin.Units.List', 'CopyResult');
         if (count($trueCopy) > 0) {
             $message .= " " . $this->_translate->_('Success') . "(ID) : " . implode(', ', $trueCopy) . " .";
         }
         $message .= " " . $this->_translate->_('Failed') . " : ";
         foreach ($falseCopy as $unitID => $errorMessage) {
             $message .= $unitID . "(ID) - " . $errorMessage;
         }
     }
     return array('data' => array('success' => $success, 'message' => $message));
 }