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