OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxerpbase.php
Go to the documentation of this file.
1 <?php
2 
8 abstract class oxERPBase
9 {
10 
11  const ERROR_USER_WRONG = "ERROR: Could not login";
12  const ERROR_USER_NO_RIGHTS = "Not sufficient rights to perform operation!";
13  const ERROR_USER_EXISTS = "ERROR: User already exists";
14  const ERROR_NO_INIT = "Init not executed, Access denied!";
15  const ERROR_DELETE_NO_EMPTY_CATEGORY = "Only empty category can be deleated";
16  const ERROR_OBJECT_NOT_EXISTING = "Object does not exist";
17  const ERROR_ERP_VERSION_NOT_SUPPORTED_BY_SHOP = "ERROR: shop does not support requested ERP version.";
18  const ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP = "ERROR: ERP does not support current shop version.";
19 
20  public static $MODE_IMPORT = "Import";
21  public static $MODE_DELETE = "Delete";
22 
23  protected $_blInit = false;
24  protected $_iLanguage = null;
25  protected $_sUserID = null;
26  //session id
27  protected $_sSID = null;
28 
29  protected static $_sRequestedVersion = '';
30 
43  protected static $_aDbLayer2ShopDbVersions = array(
44  '2.9.0' => '8', // added new fields to oxcategories, oxorderarticle
45  );
46 
52  protected $_aImportedIds = array();
53 
59  protected $_iImportedRowCount = 0;
60 
61  public $_aStatistics = array();
62  public $_iIdx = 0;
63 
68  abstract public function getImportedRowCount();
69 
76  abstract public function setImportedIds($key);
77 
83  public function getStatistics()
84  {
85  return $this->_aStatistics;
86  }
87 
93  public function getSessionID()
94  {
95  return $this->_sSID;
96  }
97 
105  abstract protected function _beforeExport($sType);
106 
114  abstract protected function _afterExport($sType);
115 
121  abstract protected function _beforeImport();
122 
128  abstract protected function _afterImport();
129 
137  abstract public function getImportData($iIdx = null);
138 
146  abstract protected function _getImportType(&$aData);
147 
155  abstract protected function _getImportMode($aData);
156 
165  abstract protected function _modifyData($aData, $oType);
166 
175  public function __call($sMethod, $aArguments)
176  {
177  throw new Exception("ERROR: Handler for Object '$sMethod' not implemented!");
178  }
179 
180 
181  // -------------------------------------------------------------------------
182  //
183  // public interface
184  //
185  // -------------------------------------------------------------------------
186 
187 
199  public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
200  {
201  ini_set('session.use_cookies', 0);
202  $_COOKIE = array('admin_sid' => false);
204  $myConfig->setConfigParam('blForceSessionStart', 1);
205  $myConfig->setConfigParam('blSessionUseCookies', 0);
206  $myConfig->setConfigParam('blAdmin', 1);
207  $myConfig->setAdminMode(true);
208 
209  $mySession = oxRegistry::getSession();
210  @$mySession->start();
211 
212 
213  oxRegistry::getSession()->setVariable("lang", $iLanguage);
214  oxRegistry::getSession()->setVariable("language", $iLanguage);
215 
216  $oUser = oxNew('oxuser');
217  try {
218  if (!$oUser->login($sUserName, $sPassword)) {
219  $oUser = null;
220  }
221  } catch (oxUserException $e) {
222  $oUser = null;
223  }
224 
226 
227  if (!$oUser || (isset($oUser->iError) && $oUser->iError == -1000)) {
228  // authorization error
229  throw new Exception(self::ERROR_USER_WRONG);
230  } elseif (($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID())) {
231  $this->_sSID = $mySession->getId();
232  $this->_blInit = true;
233  $this->_iLanguage = $iLanguage;
234  $this->_sUserID = $oUser->getId();
235  //$mySession->freeze();
236  } else {
237 
238  //user does not have sufficient rights for shop
239  throw new Exception(self::ERROR_USER_NO_RIGHTS);
240  }
241 
242  $this->_resetIdx();
243 
244  return $this->_blInit;
245  }
246 
253  public function loadSessionData($sSessionID)
254  {
255  if (!$sSessionID) {
256  throw new Exception("ERROR: Session ID not valid!");
257  }
258  $_COOKIE = array('admin_sid' => $sSessionID);
259  // start session
261  $myConfig->setConfigParam('blAdmin', 1);
262  $myConfig->setAdminMode(true);
263  $mySession = oxRegistry::getSession();
264 
265  // change session if needed
266  if ($sSessionID != session_id()) {
267  if (session_id()) {
268  session_write_close();
269  }
270  session_id($sSessionID);
271  session_start();
272  }
273 
274  $sAuth = $mySession->getVariable('auth');
275 
276  if (!isset($sAuth) || !$sAuth) {
277  throw new Exception("ERROR: Session ID not valid!");
278  }
279 
280  $this->_iLanguage = $mySession->getVariable('lang');
281  $this->_sUserID = $sAuth;
282 
283 
284  $this->_blInit = true;
285  }
286 
297  public function exportType($sType, $sWhere = null, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
298  {
299  $this->_beforeExport($sType);
300  $this->_export($sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
301  $this->_afterExport($sType);
302  }
303 
307  public function import()
308  {
309  $this->_beforeImport();
310  while ($this->_importOne()) {
311  }
312  $this->_afterImport();
313  }
314 
322  protected function _getInstanceOfType($sType)
323  {
324  $sClassName = 'oxerptype_' . $sType;
325  $sFullPath = dirname(__FILE__) . '/objects/' . $sClassName . '.php';
326 
327  if (!file_exists($sFullPath)) {
328  throw new Exception("Type $sType not supported in ERP interface!");
329  }
330 
331  include_once $sFullPath;
332 
333  //return new $sClassName;
334  return oxNew($sClassName);
335  }
336 
348  protected function _export($sType, $sWhere, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
349  {
350  global $ADODB_FETCH_MODE;
351 
353  // prepare
354  $oType = $this->_getInstanceOfType($sType);
355  //$sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $this->_iShopID);
356  $sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $myConfig->getShopId());
357  $sSQL .= $oType->getSortString($sSortFieldName, $sSortType);
358  $sFnc = '_Export' . $oType->getFunctionSuffix();
359 
360  $save = $ADODB_FETCH_MODE;
361 
363  if (isset($iCount) || isset($iStart)) {
364  $rs = $oDb->selectLimit($sSQL, $iCount, $iStart);
365  } else {
366  $rs = $oDb->select($sSQL);
367  }
368 
369  if ($rs != false && $rs->recordCount() > 0) {
370  while (!$rs->EOF) {
371  $blExport = false;
372  $sMessage = '';
373 
374  $rs->fields = $oType->addExportData($rs->fields);
375 
376  // check rights
377  $this->_checkAccess($oType, false);
378 
379  // export now
380  try {
381  $blExport = $this->$sFnc($rs->fields);
382  } catch (Exception $e) {
383  $sMessage = $e->getMessage();
384 
385  }
386 
387  $this->_aStatistics[$this->_iIdx] = array('r' => $blExport, 'm' => $sMessage);
388  //#2428 MAFI
389  $this->_nextIdx();
390 
391  $rs->moveNext();
392  }
393  }
394  $ADODB_FETCH_MODE = $save;
395  }
396 
402  protected function _outputMappingArray($sTable)
403  {
404  $aData = GetTableDescription($sTable);
405 
406  $iIdx = 0;
407  foreach ($aData as $key => $oADODBField) {
408  if (!(is_numeric(substr($oADODBField->name, strlen($oADODBField->name) - 1, 1)) && substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_')) {
409  echo("'" . $oADODBField->name . "'\t\t => '" . $oADODBField->name . "',\n");
410  $iIdx++;
411  }
412  }
413  }
414 
423  protected function _getKeyID($oType, $aData)
424  {
425  $sOXID = $oType->getOxidFromKeyFields($aData);
426  if (isset($sOXID)) {
427  // note: also pass false here
428  return $sOXID;
429  }
430 
431  return oxUtilsObject::getInstance()->generateUID();
432  }
433 
437  protected function _resetIdx()
438  {
439  $this->_iIdx = 0;
440 
441  if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
442  while (isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r']) {
443  $this->_iIdx++;
444  }
445  }
446  }
447 
451  protected function _nextIdx()
452  {
453  $this->_iIdx++;
454 
455  if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
456  while (isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r']) {
457  $this->_iIdx++;
458  }
459  }
460  }
461 
469  protected function _checkAccess($oType, $blWrite, $sOxid = null)
470  {
472  static $aAccessCache;
473 
474  if (!$this->_blInit) {
475  throw new Exception(self::ERROR_NO_INIT);
476  }
477 
478  }
479 
490  protected function _importOne()
491  {
492  $blRet = false;
493 
494  // import one row/call/object...
495  $aData = $this->getImportData();
496 
497  if ($aData) {
498  $blRet = true;
499  $blImport = false;
500  $sMessage = '';
501 
502  $sType = $this->_getImportType($aData);
503  $sMode = $this->_getImportMode($aData);
504  $oType = $this->_getInstanceOfType($sType);
505  $aData = $this->_modifyData($aData, $oType);
506 
507  // import now
508  $sFnc = '_' . $sMode . $oType->getFunctionSuffix();
509 
510  if ($sMode == oxERPBase::$MODE_IMPORT) {
511  $aData = $oType->addImportData($aData);
512  }
513 
514  try {
515  $iId = $this->$sFnc($oType, $aData);
516  if (!$iId) {
517  $blImport = false;
518  } else {
519  $this->setImportedIds($iId);
520  $blImport = true;
521  }
522  $sMessage = '';
523  } catch (Exception $e) {
524  $sMessage = $e->getMessage();
525  }
526 
527  $this->_aStatistics[$this->_iIdx] = array('r' => $blImport, 'm' => $sMessage);
528 
529  }
530  //hotfix #2428 MAFI
531  $this->_nextIdx();
532 
533  return $blRet;
534  }
535 
536 
546  protected function _save(oxERPType &$oType, $aData, $blAllowCustomShopId = false)
547  {
549 
550  // check rights
551  $sOxid = null;
552  if (isset($aData['OXID'])) {
553  $sOxid = $aData['OXID'];
554  }
555  $this->_checkAccess($oType, true, $sOxid);
556 
557  return $oType->saveObject($aData, $blAllowCustomShopId);
558  }
559 
567  protected static function _checkShopVersion()
568  {
570  if (method_exists($myConfig, 'getSerial')) {
571  if ($myConfig->getSerial() instanceof oxSerial) {
572  return;
573  }
574  }
575  throw new Exception(self::ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP);
576  }
577 
585  protected static function _checkRequestedVersion()
586  {
587  return true;
588  }
589 
597  public static function getRequestedVersion()
598  {
599  if (!self::$_sRequestedVersion) {
601  }
602 
604  }
605 
611  public static function getUsedDbFieldsVersion()
612  {
613  return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
614  }
615 
623  public static function setVersion($sDbLayerVersion = '')
624  {
625  $sDbLayerVersion = '2.9.0';
626  self::$_sRequestedVersion = $sDbLayerVersion;
628  }
629 
637  public function createPluginObject($sId)
638  {
639  $sClassName = preg_replace('/[^a-z0-9_]/i', '', $sId);
640  if (preg_match('/(.*)Plugin$/i', $sClassName, $m)) {
641  // fix possible case changes
642  $sClassName = $m[1] . 'Plugin';
643  } else {
644  throw new Exception("Plugin handler class has to end with 'Plugin' word (GOT '$sClassName').");
645  }
646 
647  $sFileName = dirname(__FILE__) . '/plugins/' . strtolower($sClassName) . '.php';
648  if (!is_readable($sFileName)) {
649  $sFileName = basename($sFileName);
650  throw new Exception("Can not find the requested plugin file ('$sFileName').");
651  }
652  include_once dirname(__FILE__) . '/plugins/oxerppluginbase.php';
653  include_once $sFileName;
654  if (!class_exists($sClassName)) {
655  throw new Exception("Can not find the requested plugin class.");
656  }
657  $o = new $sClassName();
658  if ($o instanceof oxErpPluginBase) {
659  return $o;
660  }
661  throw new Exception("Plugin does not extend oxErpPluginBase class.");
662  }
663 }