* @copyright 2016-2019 GFI Informatique, 2016-2019 TNT * @license https://opensource.org/licenses/MIT MIT License */ require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_ClassLoader.php'; /** * Class TNTOfficielLabel */ class TNTOfficielLabel extends ObjectModel { // id_tntofficiel_label public $id; public $id_order; public $label_name; /** @var type base64 (16 Mb Max) */ public $label_pdf_content; /** @var type label_date_created */ public $label_type; public $date_add; public static $definition = array( 'table' => 'tntofficiel_label', 'primary' => 'id_tntofficiel_label', 'fields' => array( 'id_order' => array( 'type' => ObjectModel::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true ), 'label_name' => array( 'type' => ObjectModel::TYPE_STRING, 'size' => 255 ), 'label_pdf_content' => array( 'type' => ObjectModel::TYPE_NOTHING, ), 'label_type' => array( 'type' => ObjectModel::TYPE_STRING, 'size' => 32 ), 'date_add' => array( 'type' => ObjectModel::TYPE_DATE ), ), ); // cache and prevent race condition. private static $arrLoadedEntities = array(); /** * Creates the tables needed by the model. * * @return bool */ public static function createTables() { TNTOfficiel_Logstack::log(); $strLogMessage = sprintf('%s::%s', __CLASS__, __FUNCTION__); $strTablePrefix = _DB_PREFIX_; $strTableEngine = _MYSQL_ENGINE_; $strTableName = $strTablePrefix.TNTOfficielLabel::$definition['table']; // Create table. $strSQLCreateLabel = <<execute($strSQLCreateLabel)) { TNTOfficiel_Logger::logInstall($strLogMessage.' : '.$objDB->getMsgError(), false); return false; } TNTOfficiel_Logger::logInstall($strLogMessage); return true; } /** * Constructor. */ public function __construct($intArgId = null) { TNTOfficiel_Logstack::log(); parent::__construct($intArgId); } /** * Load existing object model or optionally create a new one for it's ID. * * @param $intArgOrderID * @param bool $boolArgCreate * * @return TNTOfficielLabel|null */ public static function loadOrderID($intArgOrderID, $boolArgCreate = true) { TNTOfficiel_Logstack::log(); $intOrderID = (int)$intArgOrderID; // No new order ID. if (!($intOrderID > 0)) { return null; } $strEntityID = '_'.$intOrderID.'-'.(int)null.'-'.(int)null; // If already loaded. if (array_key_exists($strEntityID, TNTOfficielLabel::$arrLoadedEntities)) { $objTNTLabelModel = TNTOfficielLabel::$arrLoadedEntities[$strEntityID]; // Check. if ((int)$objTNTLabelModel->id_order === $intOrderID && Validate::isLoadedObject($objTNTLabelModel)) { return $objTNTLabelModel; } } // Search row for order ID. $objDbQuery = new DbQuery(); $objDbQuery->select('*'); $objDbQuery->from(TNTOfficielLabel::$definition['table']); $objDbQuery->where('id_order = '.$intOrderID); $objDB = Db::getInstance(); $arrResult = $objDB->executeS($objDbQuery); // If row found and match order ID. if (count($arrResult) === 1 && $intOrderID === (int)$arrResult[0]['id_order']) { // Load existing TNT order entry. $objTNTLabelModel = new TNTOfficielLabel((int)$arrResult[0]['id_tntofficiel_label']); } elseif ($boolArgCreate === true) { // Create a new TNT order entry. $objTNTLabelModelCreate = new TNTOfficielLabel(null); $objTNTLabelModelCreate->id_order = $intOrderID; $objTNTLabelModelCreate->save(); // Reload to get default DB values after creation. $objTNTLabelModel = TNTOfficielLabel::loadOrderID($intOrderID, false); } else { $objException = new Exception('TNTOfficielLabel not found for Order ID #'.$intOrderID); TNTOfficiel_Logger::logException($objException); return null; } // Check. if ((int)$objTNTLabelModel->id_order !== $intOrderID || !Validate::isLoadedObject($objTNTLabelModel)) { return null; } $objTNTLabelModel->id = (int)$objTNTLabelModel->id; $objTNTLabelModel->id_order = (int)$objTNTLabelModel->id_order; TNTOfficielLabel::$arrLoadedEntities[$strEntityID] = $objTNTLabelModel; return $objTNTLabelModel; } /** * @return mixed */ public function getLabelPDFContent() { TNTOfficiel_Logstack::log(); return TNTOfficiel_Tools::decodeBase64($this->label_pdf_content); } /** * Save BT in BDD. * * @param $strArgName * @param $strArgPDFContent * @param $strArgPickupLabelType * * @return mixed */ public function addLabel($strArgName, $strArgPDFContent, $strArgPickupLabelType) { TNTOfficiel_Logstack::log(); $objDateTimeNow = new DateTime('now'); $this->label_name = $strArgName; $this->label_pdf_content = TNTOfficiel_Tools::encodeBase64($strArgPDFContent); $this->label_type = $strArgPickupLabelType; $this->date_add = $objDateTimeNow->format('Y-m-d H:i:s'); return $this->save(); } }