OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxerptype.php
Go to the documentation of this file.
1 <?php
2 
7 class oxERPType
8 {
9  public static $ERROR_WRONG_SHOPID = "Wrong shop id, operation not allowed!";
10 
11  protected $_sTableName = null;
12  protected $_sFunctionSuffix = null;
13  protected $_aFieldList = null;
14  protected $_aKeyFieldList = null;
15  protected $_sShopObjectName = null;
16 
22  protected $_blRestrictedByShopId = false;
23 
29  protected $_aFieldListVersions = null;
30 
36  public function getFunctionSuffix()
37  {
39  }
40 
46  public function getShopObjectName()
47  {
49  }
50 
56  public function getBaseTableName()
57  {
58  return $this->_sTableName;
59  }
60 
66  public function __construct()
67  {
68  $this->_sFunctionSuffix = str_replace( "oxERPType_", "", get_class( $this));
69  }
70 
78  public function setFunctionSuffix($sNew)
79  {
80  $this->_sFunctionSuffix = $sNew;
81  }
82 
90  public function setFieldList($aFieldList)
91  {
92  $this->_aFieldList = $aFieldList;
93  }
94 
103  public function getTableName($iShopID=null, $iLanguage = 0)
104  {
105  if ($iShopID === null) {
106  $iShopID = oxRegistry::getConfig()->getShopId();
107  }
108 
109  return getViewName($this->_sTableName, -1, $iShopID);
110  }
111 
117  private function _getMultilangualFields()
118  {
119  $aRet = array();
120 
121  $aData = oxDb::getInstance()->getTableDescription( $this->_sTableName);
122 
123  foreach ($aData as $key => $oADODBField) {
124  $iLang = substr( $oADODBField->name, strlen( $oADODBField->name) - 1, 1);
125  if ( is_numeric( $iLang) && substr( $oADODBField->name, strlen( $oADODBField->name) - 2, 1) == '_') {
126  // multilangual field
127  $sMainFld = str_replace( '_'.$iLang, "", $oADODBField->name);
128  $aRet[$iLang][$sMainFld] = $oADODBField->name.' as '.$sMainFld;
129  }
130  }
131 
132  return $aRet;
133  }
134 
144  protected function _getSqlFieldName($sField, $iLanguage = 0, $iShopID = 1)
145  {
146  if ($iLanguage) {
147  $aMultiLang = $this->_getMultilangualFields();
148  // we need to load different fields
149  if ( isset( $aMultiLang[$iLanguage][$sField])) {
150  $sField = $aMultiLang[$iLanguage][$sField];
151  }
152  }
153 
154  switch ($sField) {
155  case 'OXSHOPID':
156  case 'OXSHOPINCL':
157  return "1 as $sField";
158  case 'OXSHOPEXCL':
159  return "0 as $sField";
160  }
161 
162  return $sField;
163  }
164 
174  public function getSQL( $sWhere, $iLanguage = 0, $iShopId = 1)
175  {
176  if ( !$this->_aFieldList) {
177  return;
178  }
179 
180  $sSQL = 'select ';
181  $blSep = false;
182 
183  foreach ( $this->_aFieldList as $sField) {
184  if ( $blSep) {
185  $sSQL .= ',';
186  }
187 
188  $sSQL .= $this->_getSqlFieldName($sField, $iLanguage, $iShopId);
189  $blSep = true;
190  }
191 
192 
193  $sSQL .= ' from '.$this->getTableName($iShopId, $iLanguage).' '.$sWhere;
194 
195  return $sSQL;
196  }
197 
206  public function getSortString($sFieldName = null, $sType = null)
207  {
208  $sRes = " order by ";
209  if ($sFieldName) {
210  $sRes .= $sFieldName;
211  } else {
212  $sRes .= "oxid";
213  }
214  if ($sType && ($sType == "ASC" || $sType == "DESC")) {
215  $sRes .= " ". $sType;
216  }
217  return $sRes;
218  }
219 
230  public function checkWriteAccess($oObj, $aData = null)
231  {
232  return;
233 
234  if ($oObj->isDerived()) {
235  throw new Exception( oxERPBase::$ERROR_USER_NO_RIGHTS);
236  }
237  }
238 
248  public function checkCreateAccess($aData)
249  {
250  }
251 
261  public function getObjectForDeletion( $sId)
262  {
264 
265  if (!isset($sId)) {
266  throw new Exception( "Missing ID!");
267  }
268 
269  $sName = $this->getShopObjectName();
270  if ($sName) {
271  $oObj = oxNew( $sName, "core");
272  } else {
273  $oObj = oxNew( 'oxbase', 'core');
274  $oObj->init($this->getBaseTableName());
275  }
276 
277  if (!$oObj->exists($sId)) {
278  throw new Exception( $this->getShopObjectName(). " " . $sId. " does not exists!");
279  }
280 
281  //We must load the object here, to check shopid and return it for further checks
282  if (!$oObj->Load($sId)) {
283  //its possible that access is restricted allready
284  throw new Exception( "No right to delete object {$sId} !");
285  }
286 
287  if (!$this->_isAllowedToEdit($oObj->getShopId())) {
288  throw new Exception( "No right to delete object {$sId} !");
289  }
290 
291  return $oObj;
292  }
293 
301  protected function _isAllowedToEdit($iShopId)
302  {
303  $oUsr = oxNew('oxUser');
304  $oUsr->loadAdminUser();
305 
306  if ($oUsr->oxuser__oxrights->value == "malladmin") {
307  return true;
308  } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
309  return true;
310  }
311 
312  return false;
313  }
314 
324  protected function _directSqlCheckForDeletion($sId)
325  {
326  $oDb = oxDb::getDb();
327  $sSql = "select oxshopid from ".$this->_sTableName." where oxid = " . $oDb->quote( $sId );
328  try {
329  $iShopId = $oDb->getOne($sSql);
330  } catch (Exception $e) {
331  // no shopid was found
332  return;
333  }
334  if (!$this->_isAllowedToEdit($iShopId)) {
335  throw new Exception( "No right to delete object {$sId} !");
336  }
337  }
338 
348  public function checkForDeletion($sId)
349  {
350 
351  if ( !isset($sId)) {
352  throw new Exception( "Missing ID!");
353  }
354  // malladmin can do it
355  $oUsr = oxNew('oxUser');
356  $oUsr->loadAdminUser();
357  if ($oUsr->oxuser__oxrights->value == "malladmin") {
358  return;
359  }
360  try {
361  $this->getObjectForDeletion($sId);
362  } catch (oxSystemComponentException $e) {
363  if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
364  $this->_directSqlCheckForDeletion($sId);
365  } else {
366  throw $e;
367  }
368  }
369  }
370 
378  public function delete($sID)
379  {
381  $oDb = oxDb::getDb();
382  $sSql = "delete from ".$this->_sTableName." where oxid = " . $oDb->quote( $sID );
383 
384  return $oDb->Execute($sSql);
385  }
386 
395  public function deleteObject($oObj, $sID)
396  {
397  return $oObj->delete($sID);
398  }
399 
407  public function addExportData( $aFields)
408  {
409  return $aFields;
410  }
411 
421  public function addImportData($aFields)
422  {
423  return $aFields;
424  }
425 
431  public function getRightFields()
432  {
433  $aRParams = array();
434  if (!$this->_aFieldList) {
435  $this->getFieldList();
436  }
437 
438  foreach ($this->_aFieldList as $sField) {
439  $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
440  }
441  return $aRParams;
442  }
443 
449  public function getFieldList()
450  {
451  $sObjectName = $this->getShopObjectName();
452 
453  if ( $sObjectName ) {
454  $oShopObject = oxNew( $sObjectName );
455  } else {
456  $oShopObject = oxNew( 'oxbase' );
457  $oShopObject->init( $this->getTableName() );
458  }
459 
460  if ($oShopObject instanceof oxI18n) {
461  $oShopObject->setLanguage( 0 );
462  $oShopObject->setEnableMultilang(false);
463  }
464 
465  $sViewName = $oShopObject->getViewName();
466  $sFields = str_ireplace( '`' . $sViewName . "`.", "", strtoupper($oShopObject->getSelectFields()) );
467  $sFields = str_ireplace( array(" ", "`"), array("", ""), $sFields );
468  $this->_aFieldList = explode( ",", $sFields );
469 
470  return $this->_aFieldList;
471  }
472 
478  public function getKeyFields()
479  {
480  return $this->_aKeyFieldList;
481  }
482 
490  public function getOxidFromKeyFields($aData)
491  {
493 
494  if (!is_array($this->getKeyFields())) {
495  return null;
496  }
497 
498  $oDb = oxDb::getDb();
499 
500  $aWhere = array();
501  $blAllKeys = true;
502  foreach ($this->getKeyFields() as $sKey) {
503  if (array_key_exists($sKey, $aData)) {
504  $aWhere[] = $sKey.'='.$oDb->qstr($aData[$sKey]);
505  } else {
506  $blAllKeys = false;
507  }
508  }
509 
510  if ($blAllKeys) {
511  $sSelect = 'SELECT OXID FROM '.$this->getTableName().' WHERE '.implode(' AND ', $aWhere);
512  return $oDb->getOne( $sSelect );
513  }
514 
515  return null;
516  }
517 
523  public function hasKeyFields()
524  {
525  if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
526  return true;
527  }
528  return false;
529  }
530 
540  protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
541  {
542  if (isset($aData['OXSHOPID'])) {
543  $aData['OXSHOPID'] = 'oxbaseshop';
544  }
545 
546 
547  if (!isset($aData['OXID'])) {
548  $aData['OXID'] = $this->getOxidFromKeyFields($aData);
549  }
550 
551  // null values support
552  foreach ($aData as $key => $val) {
553  if (!strlen((string) $val)) {
554  // oxbase whill quote it as string if db does not support null for this field
555  $aData[$key] = null;
556  }
557  }
558  return $aData;
559  }
560 
570  protected function _preSaveObject($oShopObject, $aData)
571  {
572  return true;
573  }
574 
583  public function saveObject($aData, $blAllowCustomShopId)
584  {
585  $sObjectName = $this->getShopObjectName();
586  if ($sObjectName) {
587  $oShopObject = oxNew( $sObjectName, 'core');
588  if ($oShopObject instanceof oxI18n) {
589  $oShopObject->setLanguage( 0 );
590  $oShopObject->setEnableMultilang(false);
591  }
592  } else {
593  $oShopObject = oxNew( 'oxbase', 'core');
594  $oShopObject->init($this->getBaseTableName());
595  }
596 
597  foreach ($aData as $key => $value) {
598  // change case to UPPER
599  $sUPKey = strtoupper($key);
600  if (!isset($aData[$sUPKey])) {
601  unset($aData[$key]);
602  $aData[$sUPKey] = $value;
603  }
604  }
605 
606 
607  $blLoaded = false;
608  if ($aData['OXID']) {
609  $blLoaded = $oShopObject->load( $aData['OXID']);
610  }
611 
612  $aData = $this->_preAssignObject( $oShopObject, $aData, $blAllowCustomShopId );
613 
614  if ($blLoaded) {
615  $this->checkWriteAccess($oShopObject, $aData);
616  } else {
617  $this->checkCreateAccess($aData);
618  }
619 
620  $oShopObject->assign( $aData );
621 
622  if ($blAllowCustomShopId) {
623  $oShopObject->setIsDerived(false);
624  }
625 
626  if ($this->_preSaveObject($oShopObject, $aData)) {
627  // store
628  if ( $oShopObject->save()) {
629  return $this->_postSaveObject($oShopObject, $aData);
630  }
631  }
632 
633  return false;
634  }
635 
644  protected function _postSaveObject($oShopObject, $aData)
645  {
646  // returning ID on success
647  return $oShopObject->getId();
648  }
649 }
650