PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/createCustomer.php

https://gitlab.com/sutrix.hoa.tran/Research-Prestashop
PHP | 132 lines | 86 code | 13 blank | 33 comment | 25 complexity | 57165bab2a2e37ab1a5c732f82e29a0a MD5 | raw file
  1. <html><head><title>CRUD Tutorial - Create example</title></head><body>
  2. <?php
  3. /*
  4. * 2007-2013 PrestaShop
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to license@prestashop.com so we can send you a copy immediately.
  15. *
  16. * DISCLAIMER
  17. *
  18. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  19. * versions in the future. If you wish to customize PrestaShop for your
  20. * needs please refer to http://www.prestashop.com for more information.
  21. *
  22. * @author PrestaShop SA <contact@prestashop.com>
  23. * @copyright 2007-2013 PrestaShop SA
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. * PrestaShop Webservice Library
  27. * @package PrestaShopWebservice
  28. */
  29. // Here we define constants /!\ You need to replace this parameters
  30. define('DEBUG', true);
  31. define('PS_SHOP_PATH', 'http://prestashop-research.local/');
  32. define('PS_WS_AUTH_KEY', 'L77TLJE2RPDE6UYP4648R873RVWE961D');
  33. require_once('./PSWebServiceLibrary.php');
  34. // Here we use the WebService to get the schema of "customers" resource
  35. try
  36. {
  37. $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
  38. $opt = array('resource' => 'customers');
  39. if (isset($_GET['Create']))
  40. $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/customers?schema=blank'));
  41. else
  42. $xml = $webService->get($opt);
  43. $resources = $xml->children()->children();
  44. }
  45. catch (PrestaShopWebserviceException $e)
  46. {
  47. // Here we are dealing with errors
  48. $trace = $e->getTrace();
  49. if ($trace[0]['args'][0] == 404) echo 'Bad ID';
  50. else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
  51. else echo 'Other error<br />'.$e->getMessage();
  52. }
  53. if (count($_POST) > 0)
  54. {
  55. // Here we have XML before update, lets update XML
  56. foreach ($resources as $nodeKey => $node)
  57. {
  58. $resources->$nodeKey = $_POST[$nodeKey];
  59. }
  60. try
  61. {
  62. $opt = array('resource' => 'customers');
  63. if ($_GET['Create'] == 'Creating')
  64. {
  65. $opt['postXml'] = $xml->asXML();
  66. $xml = $webService->add($opt);
  67. echo "Successfully added.";
  68. }
  69. }
  70. catch (PrestaShopWebserviceException $ex)
  71. {
  72. // Here we are dealing with errors
  73. $trace = $ex->getTrace();
  74. if ($trace[0]['args'][0] == 404) echo 'Bad ID';
  75. else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
  76. else echo 'Other error<br />'.$ex->getMessage();
  77. }
  78. }
  79. // We set the Title
  80. echo '<h1>Customer\'s ';
  81. if (isset($_GET['Create'])) echo 'Creation';
  82. else echo 'List';
  83. echo '</h1>';
  84. // We set a link to go back to list if we are in creation
  85. if (isset($_GET['Create']))
  86. echo '<a href="?">Return to the list</a>';
  87. if (!isset($_GET['Create']))
  88. echo '<input type="button" onClick="document.location.href=\'?Create\'" value="Create">';
  89. else
  90. echo '<form method="POST" action="?Create=Creating">';
  91. echo '<table border="5">';
  92. if (isset($resources))
  93. {
  94. echo '<tr>';
  95. if (count($_GET) == 0)
  96. {
  97. echo '<th>Id</th></tr>';
  98. foreach ($resources as $resource)
  99. {
  100. echo '<tr><td>'.$resource->attributes().'</td></tr>';
  101. }
  102. }
  103. else
  104. {
  105. echo '</tr>';
  106. foreach ($resources as $key => $resource)
  107. {
  108. echo '<tr><th>'.$key.'</th><td>';
  109. if (isset($_GET['Create']))
  110. echo '<input type="text" name="'.$key.'" value=""/>';
  111. echo '</td></tr>';
  112. }
  113. }
  114. }
  115. echo '</table><br/>';
  116. if (isset($_GET['Create']))
  117. echo '<input type="submit" value="Create"></form>';
  118. ?>
  119. </body></html>