OXID eShop CE  4.10.6
 All Classes Namespaces Files Functions Variables Pages
oxerptype.php
Go to the documentation of this file.
1 <?php
2 
9 class oxERPType
10 {
11 
12  public static $ERROR_WRONG_SHOPID = "Wrong shop id, operation not allowed!";
13 
14  protected $_sTableName = null;
15  protected $_sFunctionSuffix = null;
16  protected $_aFieldList = null;
17  protected $_aKeyFieldList = null;
18  protected $_sShopObjectName = null;
19 
25  protected $_blRestrictedByShopId = false;
26 
32  protected $_aFieldListVersions = null;
33 
39  public function getFunctionSuffix()
40  {
42  }
43 
49  public function getShopObjectName()
50  {
52  }
53 
59  public function getBaseTableName()
60  {
61  return $this->_sTableName;
62  }
63 
67  public function __construct()
68  {
69  $this->_sFunctionSuffix = str_replace("oxERPType_", "", get_class($this));
70  }
71 
77  public function setFunctionSuffix($sNew)
78  {
79  $this->_sFunctionSuffix = $sNew;
80  }
81 
87  public function setFieldList($aFieldList)
88  {
89  $this->_aFieldList = $aFieldList;
90  }
91 
100  public function getTableName($iShopID = null, $iLanguage = 0)
101  {
102  if ($iShopID === null) {
103  $iShopID = oxRegistry::getConfig()->getShopId();
104  }
105 
106  return getViewName($this->_sTableName, -1, $iShopID);
107  }
108 
114  private function _getMultilangualFields()
115  {
116  $aRet = array();
117 
118  $aData = oxDb::getInstance()->getTableDescription($this->_sTableName);
119 
120  foreach ($aData as $key => $oADODBField) {
121  $iLang = substr($oADODBField->name, strlen($oADODBField->name) - 1, 1);
122  if (is_numeric($iLang) && substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_') {
123  // multilangual field
124  $sMainFld = str_replace('_' . $iLang, "", $oADODBField->name);
125  $aRet[$iLang][$sMainFld] = $oADODBField->name . ' as ' . $sMainFld;
126  }
127  }
128 
129  return $aRet;
130  }
131 
141  protected function _getSqlFieldName($sField, $iLanguage = 0, $iShopID = 1)
142  {
143  if ($iLanguage) {
144  $aMultiLang = $this->_getMultilangualFields();
145  // we need to load different fields
146  if (isset($aMultiLang[$iLanguage][$sField])) {
147  $sField = $aMultiLang[$iLanguage][$sField];
148  }
149  }
150 
151  switch ($sField) {
152  case 'OXSHOPID':
153  case 'OXSHOPINCL':
154  return "1 as $sField";
155  case 'OXSHOPEXCL':
156  return "0 as $sField";
157  }
158 
159  return $sField;
160  }
161 
171  public function getSQL($sWhere, $iLanguage = 0, $iShopId = 1)
172  {
173  if (!$this->_aFieldList) {
174  return;
175  }
176 
177  $sSQL = 'select ';
178  $blSep = false;
179 
180  foreach ($this->_aFieldList as $sField) {
181  if ($blSep) {
182  $sSQL .= ',';
183  }
184 
185  $sSQL .= $this->_getSqlFieldName($sField, $iLanguage, $iShopId);
186  $blSep = true;
187  }
188 
189 
190  $sSQL .= ' from ' . $this->getTableName($iShopId, $iLanguage) . ' ' . $sWhere;
191 
192  return $sSQL;
193  }
194 
203  public function getSortString($sFieldName = null, $sType = null)
204  {
205  $sRes = " order by ";
206  if ($sFieldName) {
207  $sRes .= $sFieldName;
208  } else {
209  $sRes .= "oxid";
210  }
211  if ($sType && ($sType == "ASC" || $sType == "DESC")) {
212  $sRes .= " " . $sType;
213  }
214 
215  return $sRes;
216  }
217 
228  public function checkWriteAccess($oObj, $aData = null)
229  {
230  return;
231 
232  if ($oObj->isDerived()) {
233  throw new Exception(oxERPBase::$ERROR_USER_NO_RIGHTS);
234  }
235  }
236 
244  public function checkCreateAccess($aData)
245  {
246  }
247 
257  public function getObjectForDeletion($sId)
258  {
260 
261  if (!isset($sId)) {
262  throw new Exception("Missing ID!");
263  }
264 
265  $sName = $this->getShopObjectName();
266  if ($sName) {
267  $oObj = oxNew($sName, "core");
268  } else {
269  $oObj = oxNew('oxbase', 'core');
270  $oObj->init($this->getBaseTableName());
271  }
272 
273  if (!$oObj->exists($sId)) {
274  throw new Exception($this->getShopObjectName() . " " . $sId . " does not exists!");
275  }
276 
277  //We must load the object here, to check shopid and return it for further checks
278  if (!$oObj->Load($sId)) {
279  //its possible that access is restricted allready
280  throw new Exception("No right to delete object {$sId} !");
281  }
282 
283  if (!$this->_isAllowedToEdit($oObj->getShopId())) {
284  throw new Exception("No right to delete object {$sId} !");
285  }
286 
287  return $oObj;
288  }
289 
297  protected function _isAllowedToEdit($iShopId)
298  {
299  $oUsr = oxNew('oxUser');
300  $oUsr->loadAdminUser();
301 
302  if ($oUsr->oxuser__oxrights->value == "malladmin") {
303  return true;
304  } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
305  return true;
306  }
307 
308  return false;
309  }
310 
320  protected function _directSqlCheckForDeletion($sId)
321  {
322  $oDb = oxDb::getDb();
323  $sSql = "select oxshopid from " . $this->_sTableName . " where oxid = " . $oDb->quote($sId);
324  try {
325  $iShopId = $oDb->getOne($sSql);
326  } catch (Exception $e) {
327  // no shopid was found
328  return;
329  }
330  if (!$this->_isAllowedToEdit($iShopId)) {
331  throw new Exception("No right to delete object {$sId} !");
332  }
333  }
334 
344  public function checkForDeletion($sId)
345  {
346 
347  if (!isset($sId)) {
348  throw new Exception("Missing ID!");
349  }
350  // malladmin can do it
351  $oUsr = oxNew('oxUser');
352  $oUsr->loadAdminUser();
353  if ($oUsr->oxuser__oxrights->value == "malladmin") {
354  return;
355  }
356  try {
357  $this->getObjectForDeletion($sId);
358  } catch (oxSystemComponentException $e) {
359  if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
360  $this->_directSqlCheckForDeletion($sId);
361  } else {
362  throw $e;
363  }
364  }
365  }
366 
374  public function delete($sID)
375  {
377  $oDb = oxDb::getDb();
378  $sSql = "delete from " . $this->_sTableName . " where oxid = " . $oDb->quote($sID);
379 
380  return $oDb->Execute($sSql);
381  }
382 
391  public function deleteObject($oObj, $sID)
392  {
393  return $oObj->delete($sID);
394  }
395 
403  public function addExportData($aFields)
404  {
405  return $aFields;
406  }
407 
417  public function addImportData($aFields)
418  {
419  return $aFields;
420  }
421 
427  public function getRightFields()
428  {
429  $aRParams = array();
430  if (!$this->_aFieldList) {
431  $this->getFieldList();
432  }
433 
434  foreach ($this->_aFieldList as $sField) {
435  $aRParams[] = strtolower($this->_sTableName . '__' . $sField);
436  }
437 
438  return $aRParams;
439  }
440 
446  public function getFieldList()
447  {
448  $sObjectName = $this->getShopObjectName();
449 
450  if ($sObjectName) {
451  $oShopObject = oxNew($sObjectName);
452  } else {
453  $oShopObject = oxNew('oxbase');
454  $oShopObject->init($this->getTableName());
455  }
456 
457  if ($oShopObject instanceof oxI18n) {
458  $oShopObject->setLanguage(0);
459  $oShopObject->setEnableMultilang(false);
460  }
461 
462  $sViewName = $oShopObject->getViewName();
463  $sFields = str_ireplace('`' . $sViewName . "`.", "", strtoupper($oShopObject->getSelectFields()));
464  $sFields = str_ireplace(array(" ", "`"), array("", ""), $sFields);
465  $this->_aFieldList = explode(",", $sFields);
466 
467  return $this->_aFieldList;
468  }
469 
475  public function getKeyFields()
476  {
477  return $this->_aKeyFieldList;
478  }
479 
487  public function getOxidFromKeyFields($aData)
488  {
490 
491  if (!is_array($this->getKeyFields())) {
492  return null;
493  }
494 
495  $oDb = oxDb::getDb();
496 
497  $aWhere = array();
498  $blAllKeys = true;
499  foreach ($this->getKeyFields() as $sKey) {
500  if (array_key_exists($sKey, $aData)) {
501  $aWhere[] = $sKey . '=' . $oDb->qstr($aData[$sKey]);
502  } else {
503  $blAllKeys = false;
504  }
505  }
506 
507  if ($blAllKeys) {
508  $sSelect = 'SELECT OXID FROM ' . $this->getTableName() . ' WHERE ' . implode(' AND ', $aWhere);
509 
510  return $oDb->getOne($sSelect);
511  }
512 
513  return null;
514  }
515 
521  public function hasKeyFields()
522  {
523  if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
524  return true;
525  }
526 
527  return false;
528  }
529 
539  protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
540  {
541  /*
542  if (isset($aData['OXSHOPID'])) {
543  $aData['OXSHOPID'] = 'oxbaseshop';
544  }
545 
546  */
547 
548  if (isset($aData['OXSHOPID'])) {
549  $aData['OXSHOPID'] = oxRegistry::getConfig()->getShopId();
550  }
551 
552  if (!isset($aData['OXID'])) {
553  $aData['OXID'] = $this->getOxidFromKeyFields($aData);
554  }
555 
556  // null values support
557  foreach ($aData as $key => $val) {
558  if (!strlen((string) $val)) {
559  // oxbase whill quote it as string if db does not support null for this field
560  $aData[$key] = null;
561  }
562  }
563 
564  return $aData;
565  }
566 
576  protected function _preSaveObject($oShopObject, $aData)
577  {
578  return true;
579  }
580 
589  public function saveObject($aData, $blAllowCustomShopId)
590  {
591  $sObjectName = $this->getShopObjectName();
592  if ($sObjectName) {
593  $oShopObject = oxNew($sObjectName, 'core');
594  if ($oShopObject instanceof oxI18n) {
595  $oShopObject->setLanguage(0);
596  $oShopObject->setEnableMultilang(false);
597  }
598  } else {
599  $oShopObject = oxNew('oxbase', 'core');
600  $oShopObject->init($this->getBaseTableName());
601  }
602 
603  foreach ($aData as $key => $value) {
604  // change case to UPPER
605  $sUPKey = strtoupper($key);
606  if (!isset($aData[$sUPKey])) {
607  unset($aData[$key]);
608  $aData[$sUPKey] = $value;
609  }
610  }
611 
612 
613  $blLoaded = false;
614  if ($aData['OXID']) {
615  $blLoaded = $oShopObject->load($aData['OXID']);
616  }
617 
618  $aData = $this->_preAssignObject($oShopObject, $aData, $blAllowCustomShopId);
619 
620  if ($blLoaded) {
621  $this->checkWriteAccess($oShopObject, $aData);
622  } else {
623  $this->checkCreateAccess($aData);
624  }
625 
626  $oShopObject->assign($aData);
627 
628  if ($blAllowCustomShopId) {
629  $oShopObject->setIsDerived(false);
630  }
631 
632  if ($this->_preSaveObject($oShopObject, $aData)) {
633  // store
634  if ($oShopObject->save()) {
635  return $this->_postSaveObject($oShopObject, $aData);
636  }
637  }
638 
639  return false;
640  }
641 
650  protected function _postSaveObject($oShopObject, $aData)
651  {
652  // returning ID on success
653  return $oShopObject->getId();
654  }
655 }