BxOktaConStudioPage.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup OktaConnect Okta Connect
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxOktaConStudioPage extends BxTemplStudioModule
  12. {
  13. function __construct($sModule, $mixedPageName, $sPage = "")
  14. {
  15. parent::__construct($sModule, $mixedPageName, $sPage);
  16. $this->aMenuItems = array(
  17. 'settings' => array('name' => 'settings', 'icon' => 'cogs', 'title' => '_adm_lmi_cpt_settings'),
  18. 'help' => array('name' => 'help', 'icon' => 'question', 'title' => '_sys_connect_information'),
  19. );
  20. }
  21. function getHelp ()
  22. {
  23. $oModule = BxDolModule::getInstance('bx_oktacon');
  24. return _t('_bx_oktacon_information_block', BX_DOL_URL_ROOT . $oModule -> _oConfig -> getBaseUri() . 'handle');
  25. }
  26. function getImport ()
  27. {
  28. $aForm = array(
  29. 'form_attrs' => ['enctype' => 'multipart/form-data'],
  30. 'params' => ['db' => ['submit_name' => 'do_submit']],
  31. 'inputs' => array(
  32. 'file' => array(
  33. 'type' => 'file',
  34. 'name' => 'file',
  35. 'caption' => 'CSV File',
  36. 'required' => true,
  37. ),
  38. 'field_email' => array(
  39. 'type' => 'text',
  40. 'name' => 'field_email',
  41. 'caption' => 'Email field',
  42. 'required' => true,
  43. ),
  44. 'field_remote_id' => array(
  45. 'type' => 'text',
  46. 'name' => 'field_remote_id',
  47. 'caption' => 'Remote ID field',
  48. 'required' => true,
  49. ),
  50. 'do_submit' => array(
  51. 'type' => 'submit',
  52. 'name' => 'do_submit',
  53. 'value' => 'Submit',
  54. ),
  55. ),
  56. );
  57. $oForm =new BxTemplFormView($aForm);
  58. $oForm->initChecker(); // init form checker witout any data - adding new record
  59. if ($oForm->isSubmittedAndValid()) {
  60. $aFields = [];
  61. $iEmail = false;
  62. $iRemId = false;
  63. $sOutput = '<style>.err { color:red; } .ok { color:green; } </style>';
  64. if (!empty($_FILES["file"]["tmp_name"]) && ($handle = fopen($_FILES["file"]["tmp_name"], "r")) !== FALSE) {
  65. $row = 1;
  66. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  67. $num = count($data);
  68. if (1 === $row) {
  69. for ($c=0; $c < $num; $c++) {
  70. $aFields[$data[$c]] = $c;
  71. }
  72. $iEmail = isset($aFields[$_POST['field_email']]) ? $aFields[$_POST['field_email']] : false;
  73. $iRemId = isset($aFields[$_POST['field_remote_id']]) ? $aFields[$_POST['field_remote_id']] : false;
  74. if (!isset($aFields[$_POST['field_email']]) || !isset($aFields[$_POST['field_remote_id']])) {
  75. $sOutput = 'no matching fields were found';
  76. break;
  77. }
  78. }
  79. else {
  80. if (empty($data[$iEmail]) || empty($data[$iRemId])) {
  81. $sOutput .= "$row. EMPTY : <b class=\"err\">ERR - input data is empty</b><br />";
  82. } else {
  83. $oAccount = BxDolAccount::getInstance(trim($data[$iEmail]));
  84. $oProfile = $oAccount ? BxDolProfile::getInstanceByAccount($oAccount->id()) : false;
  85. if (!$oAccount || !$oProfile) {
  86. $sOutput .= "$row. {$data[$iEmail]} : <b class=\"err\">ERR - no matching account or profile was found</b><br />";
  87. }
  88. else {
  89. $res = BxDolDb::getInstance()->query("INSERT IGNORE INTO `bx_oktacon_accounts` (`local_profile`, `remote_profile`) VALUES (:lp, :rp)", ['lp' => $oProfile->id(), 'rp' => $data[$iRemId]]);
  90. if ($res)
  91. $sOutput .= "$row. {$data[$iEmail]} : <b class=\"ok\">OK</b> {$data[$iRemId]} => " . $oProfile->id() . " <br />";
  92. else
  93. $sOutput .= "$row. {$data[$iEmail]} : <b class=\"err\">ERR - DB insert query failed (maybe duplicate record)</b> {$data[$iRemId]} => " . $oProfile->id() . " <br />";
  94. }
  95. }
  96. $sOutput .= "<hr />";
  97. }
  98. $row++;
  99. }
  100. fclose($handle);
  101. return $sOutput;
  102. }
  103. }
  104. return $oForm->getCode();
  105. }
  106. }
  107. /** @} */