From b1b8dbd5cbbfd462db3280428d43cd4a16fc0ad4 Mon Sep 17 00:00:00 2001 From: Steve Knornschild Date: Wed, 21 Nov 2012 14:10:45 +0100 Subject: [PATCH 1/4] - Compatibility with Oxid 4.7.0 - Fixed some typos - Added closing div tags --- admin/admin2rest.php | 31 ------------- admin2/index.html | 2 +- admin2/views/productquickedit.php | 2 +- admin2/views/widgets/Image.php | 2 +- admin2/views/widgets/Select.php | 2 +- api/test.php | 2 +- .../views}/admin/de/admin2rest_lang.php | 0 .../views}/admin/en/admin2rest_lang.php | 0 modules/admin2/admin2rest.php | 44 +++++++++++++++++++ .../tpl => modules}/admin2/admin2rest.tpl | 43 +++++++++++++++--- modules/admin2/install.sql | 2 +- modules/admin2/metadata.php | 7 +-- {out/admin/src => modules}/admin2/utils.js | 0 13 files changed, 90 insertions(+), 47 deletions(-) delete mode 100644 admin/admin2rest.php rename {out => application/views}/admin/de/admin2rest_lang.php (100%) rename {out => application/views}/admin/en/admin2rest_lang.php (100%) create mode 100644 modules/admin2/admin2rest.php rename {out/admin/tpl => modules}/admin2/admin2rest.tpl (66%) rename {out/admin/src => modules}/admin2/utils.js (100%) diff --git a/admin/admin2rest.php b/admin/admin2rest.php deleted file mode 100644 index 480c677..0000000 --- a/admin/admin2rest.php +++ /dev/null @@ -1,31 +0,0 @@ -query( - 'SELECT `apiKey`, `apiSecret` FROM `oxuser` WHERE `OXID` = ?', - array(oxConfig::getParameter('oxid')) - ); - $user = $result->FetchRow(); - $this->_aViewData['edit'] = $user; - return parent::render(); - } - - public function save() - { - $admin2 = oxConfig::getParameter('admin2'); - $db = oxDb::getDb(true); - $db->query( - 'UPDATE `oxuser` SET `apiKey` = ?, `apiSecret` = ? WHERE `OXID` = ?', - array( - $admin2['apiKey'], - $admin2['apiSecret'], - oxConfig::getParameter('oxid'), - ) - ); - } -} diff --git a/admin2/index.html b/admin2/index.html index 9e86f2d..89b23fa 100644 --- a/admin2/index.html +++ b/admin2/index.html @@ -285,7 +285,7 @@

To Do:

- + diff --git a/admin2/views/productquickedit.php b/admin2/views/productquickedit.php index 80e9204..da8d87d 100644 --- a/admin2/views/productquickedit.php +++ b/admin2/views/productquickedit.php @@ -285,7 +285,7 @@
- + diff --git a/admin2/views/widgets/Image.php b/admin2/views/widgets/Image.php index ec830aa..202bfe8 100644 --- a/admin2/views/widgets/Image.php +++ b/admin2/views/widgets/Image.php @@ -13,7 +13,7 @@ public function output() { ob_start(); ?> - " /> + " /> - Options as $option => $value) { diff --git a/api/test.php b/api/test.php index d943b19..cf22e02 100644 --- a/api/test.php +++ b/api/test.php @@ -102,7 +102,7 @@ Response: - + diff --git a/out/admin/de/admin2rest_lang.php b/application/views/admin/de/admin2rest_lang.php similarity index 100% rename from out/admin/de/admin2rest_lang.php rename to application/views/admin/de/admin2rest_lang.php diff --git a/out/admin/en/admin2rest_lang.php b/application/views/admin/en/admin2rest_lang.php similarity index 100% rename from out/admin/en/admin2rest_lang.php rename to application/views/admin/en/admin2rest_lang.php diff --git a/modules/admin2/admin2rest.php b/modules/admin2/admin2rest.php new file mode 100644 index 0000000..0cacf39 --- /dev/null +++ b/modules/admin2/admin2rest.php @@ -0,0 +1,44 @@ +query( + 'SELECT `apiKey`, `apiSecret` FROM `oxuser` WHERE `OXID` = ?', + array(oxConfig::getParameter('oxid')) + ); + $user = $result->FetchRow();*/ + parent::render(); + $soxId = $this->getEditObjectId(); + $user = oxNew( "oxuser" ); + $user->load( $soxId ); + $this->_aViewData['edit'] = $user; + + return $this->_sThisTemplate; + } + + public function save() + { + parent::save(); + + $soxId = $this->getEditObjectId(); + + if ( !$this->_allowAdminEdit( $soxId ) ) + return false; + + $aParams = oxConfig::getParameter( "editval" ); + + $oUser = oxNew( "oxuser" ); + + $oUser->load( $soxId ); + + $oUser->assign($aParams); + $oUser->save(); + + // set oxid if inserted + $this->setEditObjectId($oUser->getId()); + } +} diff --git a/out/admin/tpl/admin2/admin2rest.tpl b/modules/admin2/admin2rest.tpl similarity index 66% rename from out/admin/tpl/admin2/admin2rest.tpl rename to modules/admin2/admin2rest.tpl index d80d221..3942aba 100644 --- a/out/admin/tpl/admin2/admin2rest.tpl +++ b/modules/admin2/admin2rest.tpl @@ -9,11 +9,39 @@
[{ $oViewConf->getHiddenSid() }] - +
- + +

[{ oxmultilang ident="ADMIN2_API_HEAD_TITLE" }]

[{ oxmultilang ident="ADMIN2_API_DESCRIPTION" }]

@@ -22,9 +50,10 @@ [{ $oViewConf->getHiddenSid() }] - - - + + + + @@ -33,7 +62,7 @@ @@ -41,7 +70,7 @@ diff --git a/modules/admin2/install.sql b/modules/admin2/install.sql index 2e1c0ad..6676c0a 100644 --- a/modules/admin2/install.sql +++ b/modules/admin2/install.sql @@ -1 +1 @@ -ALTER TABLE `oxuser` ADD COLUMN `apiKey` VARCHAR(10) NOT NULL, ADD COLUMN `apiSecret` VARCHAR(50) NOT NULL; +ALTER TABLE `oxuser` ADD COLUMN `apikey` VARCHAR(10) NOT NULL, ADD COLUMN `apisecret` VARCHAR(50) NOT NULL; diff --git a/modules/admin2/metadata.php b/modules/admin2/metadata.php index c0b2d90..1a6c51e 100644 --- a/modules/admin2/metadata.php +++ b/modules/admin2/metadata.php @@ -17,13 +17,14 @@ 'url' => 'https://github.com/OXIDprojects/admin_2_0', 'email' => 'tobias@merkl.eu', 'extend' => array( + 'oxadmindetails' => 'admin2/admin2rest', ), 'files' => array( - 'adm2rest_api' => 'admin20/admin/adm2rest_api.php', - 'adm2oauth' => 'admin20/core/adm2oauth.php', + 'adm2rest_api' => 'admin2/admin/adm2rest_api.php', + 'adm2oauth' => 'admin2/core/adm2oauth.php', ), 'templates' => array( - 'adm2rest_api.tpl' => 'admin20/out/admin/tpl/adm2rest_api.tpl', + 'admin2rest.tpl' => 'admin2/admin2rest.tpl', ), 'blocks' => array( ), diff --git a/out/admin/src/admin2/utils.js b/modules/admin2/utils.js similarity index 100% rename from out/admin/src/admin2/utils.js rename to modules/admin2/utils.js From 024fda40e2f034fb4de7e25e93e051dfac13adf3 Mon Sep 17 00:00:00 2001 From: Bernhard Scheffold Date: Thu, 29 Nov 2012 17:02:19 +0100 Subject: [PATCH 2/4] fix bootstrap for 4.7/5.0 --- api/rest/index.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/api/rest/index.php b/api/rest/index.php index 0dac790..5e9c1c2 100644 --- a/api/rest/index.php +++ b/api/rest/index.php @@ -43,22 +43,26 @@ function getShopBasePath() /** * Load OXID core classes. */ -require 'modules/functions.php'; -require 'core/oxfunctions.php'; +if (file_exists(getShopBasePath() . "bootstrap.php")) { + require_once getShopBasePath() . "bootstrap.php"; +} else { + require 'modules/functions.php'; + require 'core/oxfunctions.php'; +} // Load and initialize our main autoloader. require 'Admin2/Loader/Autoloader.php'; $loader = Admin2_Loader_Autoloader::getInstance(); $loader->registerNamespace('Admin2'); -$hash = null; +$hashClass = null; if (function_exists('hash')) { $hashClass = new Admin2_Signature_Hash_Sha256(); } else if (function_exists('mhash')) { $hashClass = new Admin2_Signature_Mhash_Sha256(); } -if ($hash === null) { +if ($hashClass === null) { header('HTTP/1.0 500 Internal Server Error'); exit(255); } From 38114f7a4467ab13d3a8e9d878861397002647b0 Mon Sep 17 00:00:00 2001 From: Bernhard Scheffold Date: Fri, 14 Dec 2012 16:55:16 +0100 Subject: [PATCH 3/4] add Payments model and controller --- api/rest/controllers/PaymentsController.php | 124 +++++++++++++++ api/rest/models/Payment.php | 165 ++++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 api/rest/controllers/PaymentsController.php create mode 100644 api/rest/models/Payment.php diff --git a/api/rest/controllers/PaymentsController.php b/api/rest/controllers/PaymentsController.php new file mode 100644 index 0000000..ef16dc9 --- /dev/null +++ b/api/rest/controllers/PaymentsController.php @@ -0,0 +1,124 @@ +_request->getEntity(); + $paymentData = null; + if ($entity != null) { + $paymentData = $paymentModel->getPayment($entity); + } + + if ($paymentData === null) { + $this->_response->setResponseCode(404); + $this->_response->setData(array('sMessage' => 'NOT FOUND')); + return; + } + $this->_response->setData(array('payment' => $paymentData)); + } + + /** + * Get list of payments + * + * @return void + */ + public function getList() + { + $paymentModel = new Application_Model_Payment(); + $limit = $this->_request->getParam('limit', 50); + $offset = $this->_request->getParam('offset', 0); + $paymentData = $paymentModel->getPaymentList($limit, $offset); + $this->_response->setData(array('paymentList' => $paymentData)); + } + + /** + * Save list of payments + * + * @return void + */ + public function post() + { + $aPayments = (array) $this->_request->getParam('apayments'); + $aResults = array(); + foreach ($aPayments as $aPaymentData) { + $paymentModel = new Application_Model_Payment(); + if (!$aPaymentData['oxpayments__oxid']) { + $aPaymentData['oxpayments__oxid'] = -1; // create a new entry in case no entity is provided + } + $paymentModel->savePayment($aPaymentData); + $aPaymentData['oxpayments__oxid'] = $paymentModel->getId(); + $aResults[] = $aPaymentData; + } + $this->_response->setData(array('aPayments' => $aResults)); + } + + /** + * Create or update a payment + * + * @return void + */ + public function put() + { + $paymentModel = new Application_Model_Payment(); + $aPaymentData = (array) $this->_request->getParams(); + $oxid = $this->_request->getEntity(); + if ($oxid) { + $aPaymentData['oxid'] = $oxid; + $ok = $paymentModel->savePayment($aPaymentData); + if ($ok) { + $this->_response->setData(array('sMessage' => 'OK')); + } else { + $this->_response->setResponseCode(400); + $this->_response->setData(array('sMessage' => 'NOT OK')); + } + } else { + $this->_response->setResponseCode(400); + $this->_response->setData(array('sMessage' => 'No entity given!')); + } + } + + /** + * Delete a payment + * + * @return void + */ + public function delete() + { + $paymentModel = new Application_Model_Payment(); + $entity = $this->_request->getEntity(); + $paymentData = null; + if ($entity != null) { + $paymentData = $paymentModel->deletePayment($entity); + } + if ($paymentData === false) { + $this->_response->setResponseCode(404); + $this->_response->setData(array('sMessage' => 'NOT FOUND')); + return; + } + $this->_response->setData(array('sMessage' => 'OK')); + } +} diff --git a/api/rest/models/Payment.php b/api/rest/models/Payment.php new file mode 100644 index 0000000..11d7ed6 --- /dev/null +++ b/api/rest/models/Payment.php @@ -0,0 +1,165 @@ +_oxid; + } + + /** + * Retrieve payment data. + * + * @param string $oxid OXID of the payment. + * + * @return array|null + */ + public function getPayment($oxid) + { + /** + * @var oxPayment $payment + */ + $payment = oxNew('oxpayment'); + $payment->disableLazyLoading(); + $found = $payment->loadInLang($this->_currentLanguageId, $oxid); + if ($found) { + return $this->oxidToArray($payment); + } + } + + /** + * Retrieve payment data. + * + * @param int $limit Maximum length of list. + * @param int $offset Offset of first list item. + * + * @return array|null + */ + public function getPaymentList($limit = 50, $offset = 0) + { + /** + * @var oxArticleList $paymentList + * @var oxArticle $payment + */ + $paymentList = oxNew('oxpaymentlist'); + $paymentList->setSqlLimit($offset, $limit); + $listObject = $paymentList->getBaseObject(); + $listObject->disableLazyLoading(); + $fieldList = $listObject->getSelectFields(); + $query = 'SELECT ' . $fieldList . ' FROM ' . $listObject->getViewName(); + $paymentList->selectString($query); + + $paymentData = array(); + foreach ($paymentList as $payment) { + $paymentData[$payment->getId()] = $this->oxidToArray($payment); + } + return $paymentData; + } + + /** + * Save payment data. + * + * @param array $aPaymentData the payment data + * + */ + public function savePayment($aParams) + { + $soxId = $aParams['oxpayments__oxid']; + // checkbox handling + if (!isset($aParams['oxpayments__oxactive'])) + $aParams['oxpayments__oxactive'] = 0; + if (!isset($aParams['oxpayments__oxchecked'])) + $aParams['oxpayments__oxchecked'] = 0; + + $oPayment = oxNew("oxpayment"); + + if ($soxId != "-1") + $oPayment->loadInLang($this->_currentLanguageId, $soxId); + else + $aParams['oxpayments__oxid'] = null; + + $oPayment->setLanguage(0); + $oPayment->assign($aParams); + + // setting add sum calculation rules + $aRules = (array) oxConfig::getParameter("oxpayments__oxaddsumrules"); + $oPayment->oxpayments__oxaddsumrules = new oxField(array_sum($aRules)); + + if (!is_array( $this->_aFieldArray)) + $this->_aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oPayment->oxpayments__oxvaldesc->value); + // build value + $sValdesc = ""; + foreach ($this->_aFieldArray as $oField) + $sValdesc .= $oField->name . "__@@"; + + $oPayment->oxpayments__oxvaldesc = new oxField($sValdesc, oxField::T_RAW); + $oPayment->setLanguage($this->_currentLanguageId); + $ok = $oPayment->save(); + + if ($ok) { + // set oxid if inserted + $this->_oxid = $oPayment->getId(); + } + + return $ok; + } + + /** + * @param string $oxid OXID of the payment. + * + * @return array|null + */ + public function deletePayment($oxid) + { + $payment = oxNew('oxpayment'); + $success = $payment->delete($oxid); + return $success ? $this->oxidToArray($payment) : $success; + } + + /** + * Model-specific initialization code. + * + * @return null + */ + public function init() + { + // Add your model-specific initialization code here, instead of overloading the constructor. + } + + private function oxidToArray(oxBase $oxidObject) + { + $objectData = array(); + $fields = $oxidObject->getFieldNames(); + + foreach ($fields as $field) { + $objectData[$field] = $oxidObject->getFieldData($field); + } + + return $objectData; + } +} From 13fddf6d6fd19aa72ef77f0b39a14851f2af60fc Mon Sep 17 00:00:00 2001 From: Bernhard Scheffold Date: Fri, 14 Dec 2012 17:01:07 +0100 Subject: [PATCH 4/4] improved response handling --- api/library/Admin2/Dispatcher.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/library/Admin2/Dispatcher.php b/api/library/Admin2/Dispatcher.php index 80cfc67..50e2f64 100644 --- a/api/library/Admin2/Dispatcher.php +++ b/api/library/Admin2/Dispatcher.php @@ -196,10 +196,10 @@ public function run( $processedData = $this->_outputProcessor->process($this->_response); $responseCode = $this->_response->getResponseCode(); if (!empty($responseCode)) { - header($responseCode); + header(' ', true, $responseCode); } } else { - header('HTTP/1.0 204 No Content', true); + header(' ', true, 204); } foreach ($this->_response->getResponseHeader() as $headerKey => $headerValue) {
- +
- +