Example #1
0
 public function create()
 {
     if ('' == $this->getOption('receiver_name', '') || '' == $this->getOption('receiver_street', '') || '' == $this->getOption('receiver_city', '') || '' == $this->getOption('receiver_postal', '') || '' == $this->getOption('package_weight', '') || '' == $this->getOption('package_width', '') || '' == $this->getOption('package_height', '') || '' == $this->getOption('package_depth', '') || '' == $this->getOption('package_payment_method', '') || '' == $this->getOption('sender_name', '') || '' == $this->getOption('sender_street', '') || '' == $this->getOption('sender_city', '') || '' == $this->getOption('sender_postal', '')) {
         throw new Exception('Required parameters not found');
     }
     $this->_oDbAdapter->beginTransaction();
     try {
         // 			$query = "
         // 				INSERT INTO `client` (
         // 					`client_name`,
         // 					`client_street`,
         // 					`client_city`,
         // 					`client_postal`,
         // 					`client_email`,
         // 					`client_phone`
         // 				) VALUES (
         // 					" . $this->_oDbAdapter->quote($this->getOption('receiver_name')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('receiver_street')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('receiver_city')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('receiver_postal')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('receiver_email')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('receiver_phone')) . "
         // 				)
         // 			";
         // 			$this->_oDbAdapter->query($query);
         // 			$iReceiverId = $this->_oDbAdapter->lastInsertId();
         $oClientModel = new Model_Client();
         $iReceiverId = $oClientModel->setOption('client_name', $this->getOption('receiver_name'))->setOption('client_street', $this->getOption('receiver_street'))->setOption('client_city', $this->getOption('receiver_city'))->setOption('client_postal', $this->getOption('receiver_postal'))->setOption('client_email', $this->getOption('receiver_email', ''))->setOption('client_phone', $this->getOption('receiver_phone', ''))->create();
         // 			$query = "
         // 				INSERT INTO `client` (
         // 					`client_name`,
         // 					`client_street`,
         // 					`client_city`,
         // 					`client_postal`,
         // 					`client_email`,
         // 					`client_phone`
         // 				) VALUES (
         // 					" . $this->_oDbAdapter->quote($this->getOption('sender_name')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('sender_street')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('sender_city')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('sender_postal')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('sender_email')) . ",
         // 					" . $this->_oDbAdapter->quote($this->getOption('sender_phone')) . "
         // 				)
         // 			";
         // 			$this->_oDbAdapter->query($query);
         // 			$iSenderId = $this->_oDbAdapter->lastInsertId();
         $iSenderId = $oClientModel->setOption('client_name', $this->getOption('sender_name'))->setOption('client_street', $this->getOption('sender_street'))->setOption('client_city', $this->getOption('sender_city'))->setOption('client_postal', $this->getOption('sender_postal'))->setOption('client_email', $this->getOption('sender_email', ''))->setOption('client_phone', $this->getOption('sender_phone', ''))->create();
         do {
             $iTrackingCode = base_convert(time(), 10, 9);
             $query = "\n\t\t\t\t\tINSERT INTO `package` (\n\t\t\t\t\t\t`package_tracking_code`,\n\t\t\t\t\t\t`sender_id`,\n\t\t\t\t\t\t`receiver_id`,\n\t\t\t\t\t\t`package_weight`,\n\t\t\t\t\t\t`package_width`,\n\t\t\t\t\t\t`package_height`,\n\t\t\t\t\t\t`package_depth`,\n\t\t\t\t\t\t`package_payment_method`\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t" . $this->_oDbAdapter->quote($iTrackingCode) . ",\n\t\t\t\t\t\t" . $iSenderId . ",\n\t\t\t\t\t\t" . $iReceiverId . ",\n\t\t\t\t\t\t" . (int) $this->getOption('package_weight') . ",\n\t\t\t\t\t\t" . (int) $this->getOption('package_width') . ",\n\t\t\t\t\t\t" . (int) $this->getOption('package_height') . ",\n\t\t\t\t\t\t" . (int) $this->getOption('package_depth') . ",\n\t\t\t\t\t\t" . $this->_oDbAdapter->quote($this->getOption('package_payment_method')) . "\n\t\t\t\t\t)\n\t\t\t\t";
             try {
                 $this->_oDbAdapter->query($query);
                 break;
             } catch (PDOException $e) {
                 // grab exceptions but loop only if unique constraint fails
                 // Error: 1169 SQLSTATE: 23000 (ER_DUP_UNIQUE)
                 // Message: Can't write, because of unique constraint, to table '%s'
                 if (1169 == $e->getCode()) {
                     continue;
                 } else {
                     // rethrow the exception... we want to know what crapped here
                     throw $e;
                     break;
                     // just to be on a safe side (:
                 }
             }
         } while (true);
         // HEAVY HACK ALERT!!!!!!!!!!!
         $iPackageId = $this->_oDbAdapter->lastInsertId();
         $query = "\n\t\t\t\tINSERT INTO `package_log` (\n\t\t\t\t\t`package_id`,\n\t\t\t\t\t`user_id`,\n\t\t\t\t\t`package_log_type`,\n\t\t\t\t\t`package_log_time`,\n\t\t\t\t\t`package_log_info`\n\t\t\t\t) VALUES (\n\t\t\t\t\t" . $iPackageId . ",\n\t\t\t\t\tNULL,\n\t\t\t\t\t'created',\n\t\t\t\t\t" . time() . ",\n\t\t\t\t\tNULL\n\t\t\t\t)\n\t\t\t";
         $this->_oDbAdapter->query($query);
     } catch (PDOException $e) {
         $this->handlePDOException($e);
         $this->_oDbAdapter->rollBack();
         return false;
     }
     $this->_oDbAdapter->commit();
     return $iTrackingCode;
 }