oxerptype.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxERPType
00008 {
00009 
00010     public static $ERROR_WRONG_SHOPID = "Wrong shop id, operation not allowed!";
00011 
00012     protected $_sTableName = null;
00013     protected $_sFunctionSuffix = null;
00014     protected $_aFieldList = null;
00015     protected $_aKeyFieldList = null;
00016     protected $_sShopObjectName = null;
00017 
00023     protected $_blRestrictedByShopId = false;
00024 
00030     protected $_aFieldListVersions = null;
00031 
00037     public function getFunctionSuffix()
00038     {
00039         return $this->_sFunctionSuffix;
00040     }
00041 
00047     public function getShopObjectName()
00048     {
00049         return $this->_sShopObjectName;
00050     }
00051 
00057     public function getBaseTableName()
00058     {
00059         return $this->_sTableName;
00060     }
00061 
00065     public function __construct()
00066     {
00067         $this->_sFunctionSuffix = str_replace("oxERPType_", "", get_class($this));
00068     }
00069 
00075     public function setFunctionSuffix($sNew)
00076     {
00077         $this->_sFunctionSuffix = $sNew;
00078     }
00079 
00085     public function setFieldList($aFieldList)
00086     {
00087         $this->_aFieldList = $aFieldList;
00088     }
00089 
00098     public function getTableName($iShopID = null, $iLanguage = 0)
00099     {
00100         if ($iShopID === null) {
00101             $iShopID = oxRegistry::getConfig()->getShopId();
00102         }
00103 
00104         return getViewName($this->_sTableName, -1, $iShopID);
00105     }
00106 
00112     private function _getMultilangualFields()
00113     {
00114         $aRet = array();
00115 
00116         $aData = oxDb::getInstance()->getTableDescription($this->_sTableName);
00117 
00118         foreach ($aData as $key => $oADODBField) {
00119             $iLang = substr($oADODBField->name, strlen($oADODBField->name) - 1, 1);
00120             if (is_numeric($iLang) && substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_') {
00121                 // multilangual field
00122                 $sMainFld = str_replace('_' . $iLang, "", $oADODBField->name);
00123                 $aRet[$iLang][$sMainFld] = $oADODBField->name . ' as ' . $sMainFld;
00124             }
00125         }
00126 
00127         return $aRet;
00128     }
00129 
00139     protected function _getSqlFieldName($sField, $iLanguage = 0, $iShopID = 1)
00140     {
00141         if ($iLanguage) {
00142             $aMultiLang = $this->_getMultilangualFields();
00143             // we need to load different fields
00144             if (isset($aMultiLang[$iLanguage][$sField])) {
00145                 $sField = $aMultiLang[$iLanguage][$sField];
00146             }
00147         }
00148 
00149         switch ($sField) {
00150             case 'OXSHOPID':
00151             case 'OXSHOPINCL':
00152                 return "1 as $sField";
00153             case 'OXSHOPEXCL':
00154                 return "0 as $sField";
00155         }
00156 
00157         return $sField;
00158     }
00159 
00169     public function getSQL($sWhere, $iLanguage = 0, $iShopId = 1)
00170     {
00171         if (!$this->_aFieldList) {
00172             return;
00173         }
00174 
00175         $sSQL = 'select ';
00176         $blSep = false;
00177 
00178         foreach ($this->_aFieldList as $sField) {
00179             if ($blSep) {
00180                 $sSQL .= ',';
00181             }
00182 
00183             $sSQL .= $this->_getSqlFieldName($sField, $iLanguage, $iShopId);
00184             $blSep = true;
00185         }
00186 
00187 
00188         $sSQL .= ' from ' . $this->getTableName($iShopId, $iLanguage) . ' ' . $sWhere;
00189 
00190         return $sSQL;
00191     }
00192 
00201     public function getSortString($sFieldName = null, $sType = null)
00202     {
00203         $sRes = " order by ";
00204         if ($sFieldName) {
00205             $sRes .= $sFieldName;
00206         } else {
00207             $sRes .= "oxid";
00208         }
00209         if ($sType && ($sType == "ASC" || $sType == "DESC")) {
00210             $sRes .= " " . $sType;
00211         }
00212 
00213         return $sRes;
00214     }
00215 
00226     public function checkWriteAccess($oObj, $aData = null)
00227     {
00228         return;
00229 
00230         if ($oObj->isDerived()) {
00231             throw new Exception(oxERPBase::$ERROR_USER_NO_RIGHTS);
00232         }
00233     }
00234 
00242     public function checkCreateAccess($aData)
00243     {
00244     }
00245 
00255     public function getObjectForDeletion($sId)
00256     {
00257         $myConfig = oxRegistry::getConfig();
00258 
00259         if (!isset($sId)) {
00260             throw new Exception("Missing ID!");
00261         }
00262 
00263         $sName = $this->getShopObjectName();
00264         if ($sName) {
00265             $oObj = oxNew($sName, "core");
00266         } else {
00267             $oObj = oxNew('oxbase', 'core');
00268             $oObj->init($this->getBaseTableName());
00269         }
00270 
00271         if (!$oObj->exists($sId)) {
00272             throw new Exception($this->getShopObjectName() . " " . $sId . " does not exists!");
00273         }
00274 
00275         //We must load the object here, to check shopid and return it for further checks
00276         if (!$oObj->Load($sId)) {
00277             //its possible that access is restricted allready
00278             throw new Exception("No right to delete object {$sId} !");
00279         }
00280 
00281         if (!$this->_isAllowedToEdit($oObj->getShopId())) {
00282             throw new Exception("No right to delete object {$sId} !");
00283         }
00284 
00285         return $oObj;
00286     }
00287 
00295     protected function _isAllowedToEdit($iShopId)
00296     {
00297         $oUsr = oxNew('oxUser');
00298         $oUsr->loadAdminUser();
00299 
00300         if ($oUsr->oxuser__oxrights->value == "malladmin") {
00301             return true;
00302         } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
00303             return true;
00304         }
00305 
00306         return false;
00307     }
00308 
00318     protected function _directSqlCheckForDeletion($sId)
00319     {
00320         $oDb = oxDb::getDb();
00321         $sSql = "select oxshopid from " . $this->_sTableName . " where oxid = " . $oDb->quote($sId);
00322         try {
00323             $iShopId = $oDb->getOne($sSql);
00324         } catch (Exception $e) {
00325             // no shopid was found
00326             return;
00327         }
00328         if (!$this->_isAllowedToEdit($iShopId)) {
00329             throw new Exception("No right to delete object {$sId} !");
00330         }
00331     }
00332 
00342     public function checkForDeletion($sId)
00343     {
00344 
00345         if (!isset($sId)) {
00346             throw new Exception("Missing ID!");
00347         }
00348         // malladmin can do it
00349         $oUsr = oxNew('oxUser');
00350         $oUsr->loadAdminUser();
00351         if ($oUsr->oxuser__oxrights->value == "malladmin") {
00352             return;
00353         }
00354         try {
00355             $this->getObjectForDeletion($sId);
00356         } catch (oxSystemComponentException $e) {
00357             if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00358                 $this->_directSqlCheckForDeletion($sId);
00359             } else {
00360                 throw $e;
00361             }
00362         }
00363     }
00364 
00372     public function delete($sID)
00373     {
00374         $myConfig = oxRegistry::getConfig();
00375         $oDb = oxDb::getDb();
00376         $sSql = "delete from " . $this->_sTableName . " where oxid = " . $oDb->quote($sID);
00377 
00378         return $oDb->Execute($sSql);
00379     }
00380 
00389     public function deleteObject($oObj, $sID)
00390     {
00391         return $oObj->delete($sID);
00392     }
00393 
00401     public function addExportData($aFields)
00402     {
00403         return $aFields;
00404     }
00405 
00415     public function addImportData($aFields)
00416     {
00417         return $aFields;
00418     }
00419 
00425     public function getRightFields()
00426     {
00427         $aRParams = array();
00428         if (!$this->_aFieldList) {
00429             $this->getFieldList();
00430         }
00431 
00432         foreach ($this->_aFieldList as $sField) {
00433             $aRParams[] = strtolower($this->_sTableName . '__' . $sField);
00434         }
00435 
00436         return $aRParams;
00437     }
00438 
00444     public function getFieldList()
00445     {
00446         $sObjectName = $this->getShopObjectName();
00447 
00448         if ($sObjectName) {
00449             $oShopObject = oxNew($sObjectName);
00450         } else {
00451             $oShopObject = oxNew('oxbase');
00452             $oShopObject->init($this->getTableName());
00453         }
00454 
00455         if ($oShopObject instanceof oxI18n) {
00456             $oShopObject->setLanguage(0);
00457             $oShopObject->setEnableMultilang(false);
00458         }
00459 
00460         $sViewName = $oShopObject->getViewName();
00461         $sFields = str_ireplace('`' . $sViewName . "`.", "", strtoupper($oShopObject->getSelectFields()));
00462         $sFields = str_ireplace(array(" ", "`"), array("", ""), $sFields);
00463         $this->_aFieldList = explode(",", $sFields);
00464 
00465         return $this->_aFieldList;
00466     }
00467 
00473     public function getKeyFields()
00474     {
00475         return $this->_aKeyFieldList;
00476     }
00477 
00485     public function getOxidFromKeyFields($aData)
00486     {
00487         $myConfig = oxRegistry::getConfig();
00488 
00489         if (!is_array($this->getKeyFields())) {
00490             return null;
00491         }
00492 
00493         $oDb = oxDb::getDb();
00494 
00495         $aWhere = array();
00496         $blAllKeys = true;
00497         foreach ($this->getKeyFields() as $sKey) {
00498             if (array_key_exists($sKey, $aData)) {
00499                 $aWhere[] = $sKey . '=' . $oDb->qstr($aData[$sKey]);
00500             } else {
00501                 $blAllKeys = false;
00502             }
00503         }
00504 
00505         if ($blAllKeys) {
00506             $sSelect = 'SELECT OXID FROM ' . $this->getTableName() . ' WHERE ' . implode(' AND ', $aWhere);
00507 
00508             return $oDb->getOne($sSelect);
00509         }
00510 
00511         return null;
00512     }
00513 
00519     public function hasKeyFields()
00520     {
00521         if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
00522             return true;
00523         }
00524 
00525         return false;
00526     }
00527 
00537     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00538     {
00539         /*
00540         if (isset($aData['OXSHOPID'])) {
00541             $aData['OXSHOPID'] = 'oxbaseshop';
00542         }
00543 
00544         */
00545 
00546         if (isset($aData['OXSHOPID'])) {
00547             $aData['OXSHOPID'] = oxRegistry::getConfig()->getShopId();
00548         }
00549 
00550         if (!isset($aData['OXID'])) {
00551             $aData['OXID'] = $this->getOxidFromKeyFields($aData);
00552         }
00553 
00554         // null values support
00555         foreach ($aData as $key => $val) {
00556             if (!strlen((string) $val)) {
00557                 // oxbase whill quote it as string if db does not support null for this field
00558                 $aData[$key] = null;
00559             }
00560         }
00561 
00562         return $aData;
00563     }
00564 
00574     protected function _preSaveObject($oShopObject, $aData)
00575     {
00576         return true;
00577     }
00578 
00587     public function saveObject($aData, $blAllowCustomShopId)
00588     {
00589         $sObjectName = $this->getShopObjectName();
00590         if ($sObjectName) {
00591             $oShopObject = oxNew($sObjectName, 'core');
00592             if ($oShopObject instanceof oxI18n) {
00593                 $oShopObject->setLanguage(0);
00594                 $oShopObject->setEnableMultilang(false);
00595             }
00596         } else {
00597             $oShopObject = oxNew('oxbase', 'core');
00598             $oShopObject->init($this->getBaseTableName());
00599         }
00600 
00601         foreach ($aData as $key => $value) {
00602             // change case to UPPER
00603             $sUPKey = strtoupper($key);
00604             if (!isset($aData[$sUPKey])) {
00605                 unset($aData[$key]);
00606                 $aData[$sUPKey] = $value;
00607             }
00608         }
00609 
00610 
00611         $blLoaded = false;
00612         if ($aData['OXID']) {
00613             $blLoaded = $oShopObject->load($aData['OXID']);
00614         }
00615 
00616         $aData = $this->_preAssignObject($oShopObject, $aData, $blAllowCustomShopId);
00617 
00618         if ($blLoaded) {
00619             $this->checkWriteAccess($oShopObject, $aData);
00620         } else {
00621             $this->checkCreateAccess($aData);
00622         }
00623 
00624         $oShopObject->assign($aData);
00625 
00626         if ($blAllowCustomShopId) {
00627             $oShopObject->setIsDerived(false);
00628         }
00629 
00630         if ($this->_preSaveObject($oShopObject, $aData)) {
00631             // store
00632             if ($oShopObject->save()) {
00633                 return $this->_postSaveObject($oShopObject, $aData);
00634             }
00635         }
00636 
00637         return false;
00638     }
00639 
00648     protected function _postSaveObject($oShopObject, $aData)
00649     {
00650         // returning ID on success
00651         return $oShopObject->getId();
00652     }
00653 }