SettingsRepository.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace MarketplaceRepository;
  3. class SettingsRepository
  4. implements \Core\App\Repository\Marketplace\SettingsInterface
  5. {
  6. const VERSION_PLATFORM_CODE = 'SHOPIFY';
  7. const VERSION_FILE = 'https://facturis-online.ro/plugins/latest_versions.txt';
  8. const VERSION_SEPARATOR = ", ";
  9. public $settings;
  10. public $shop;
  11. public $db;
  12. public function __construct($db, $shop = '')
  13. {
  14. $this->conn = $db->getConnection();
  15. $this->db = $db;
  16. $this->setShop($shop);
  17. $settings = $db->getSettings($this->shop);
  18. $settingsFactory = new \Core\App\Factory\SettingsFactory();
  19. $this->settings = $settingsFactory->createSettings(
  20. isset($settings['fsync_auth_apikey']) ? $settings['fsync_auth_apikey'] : '',
  21. isset($settings['fsync_auth_username']) ? $settings['fsync_auth_username'] : '',
  22. isset($settings['fsync_auth_password']) ? $settings['fsync_auth_password'] : '',
  23. isset($settings['fsync_auth_fiscalcode']) ? $settings['fsync_auth_fiscalcode'] : '',
  24. isset($settings['fsync_option_autosyncstock']) ? $settings['fsync_option_autosyncstock'] : '',
  25. isset($settings['fsync_option_autosyncorder']) ? $settings['fsync_option_autosyncorder'] : '',
  26. isset($settings['fsync_option_filterstock']) ? $settings['fsync_option_filterstock'] : '',
  27. isset($settings['fsync_option_syncordersas']) ? $settings['fsync_option_syncordersas'] : '',
  28. isset($settings['fsync_option_proformaserie']) ? $settings['fsync_option_proformaserie'] : '',
  29. isset($settings['fsync_option_daysago']) ? $settings['fsync_option_daysago'] : '',
  30. isset($settings['fsync_option_locations']) ? $settings['fsync_option_locations'] : null,
  31. isset($settings['fsync_option_withdiscount']) ? $settings['fsync_option_withdiscount'] : ''
  32. );
  33. }
  34. public function getAuthApiKey()
  35. {
  36. return $this->settings->getAuthApiKey();
  37. }
  38. public function getAuthUsername()
  39. {
  40. return $this->settings->getAuthUsername();
  41. }
  42. public function getAuthPassword()
  43. {
  44. return $this->settings->getAuthPassword();
  45. }
  46. public function getAuthFiscalCode()
  47. {
  48. return $this->settings->getAuthFiscalCode();
  49. }
  50. public function getOptionAutoStockSync()
  51. {
  52. return $this->settings->getOptionAutoStockSync();
  53. }
  54. public function getOptionAutoOrderSync()
  55. {
  56. return $this->settings->getOptionAutoOrderSync();
  57. }
  58. public function getOptionStockFilter()
  59. {
  60. return $this->settings->getOptionStockFilter();
  61. }
  62. public function getOptionOrderOrProforma()
  63. {
  64. return $this->settings->getOptionOrderOrProforma();
  65. }
  66. public function getOptionProformaSerie()
  67. {
  68. return $this->settings->getOptionProformaSerie();
  69. }
  70. public function getOptionOrderedDaysAgo()
  71. {
  72. return $this->settings->getOptionOrderedDaysAgo();
  73. }
  74. public function getOptionLocations()
  75. {
  76. return $this->settings->getOptionLocations();
  77. }
  78. public function getCustomFieldBank()
  79. {
  80. return $this->settings->getCustomFieldBank();
  81. }
  82. public function getCustomFieldAccount()
  83. {
  84. return $this->settings->getCustomFieldAccount();
  85. }
  86. public function getCustomFieldCompany()
  87. {
  88. return $this->settings->getCustomFieldCompany();
  89. }
  90. public function getCustomFieldFiscalCode()
  91. {
  92. return $this->settings->getCustomFieldFiscalCode();
  93. }
  94. public function getCustomFieldRegNumber()
  95. {
  96. return $this->settings->getCustomFieldRegNumber();
  97. }
  98. public function getSyncDataTypes()
  99. {
  100. return $this->settings->getSyncDataTypes();
  101. }
  102. public function getSyncOptions()
  103. {
  104. return array(
  105. 'fsync_option_autosyncstock' => $this->settings->getOptionAutoStockSync(),
  106. 'fsync_option_autosyncorder' => $this->settings->getOptionAutoOrderSync(),
  107. 'fsync_option_filterstock' => $this->settings->getOptionStockFilter(),
  108. 'fsync_option_syncordersas' => $this->settings->getOptionOrderOrProforma(),
  109. 'fsync_option_proformaserie' => $this->settings->getOptionProformaSerie(),
  110. 'fsync_option_daysago' => $this->settings->getOptionOrderedDaysAgo(),
  111. 'fsync_option_locations' => $this->settings->getOptionLocations(),
  112. 'fsync_option_withdiscount' => $this->settings->getOptionWithDiscount()
  113. );
  114. }
  115. public function getSyncAuth()
  116. {
  117. return array(
  118. 'fsync_auth_apikey' => $this->settings->getAuthApiKey(),
  119. 'fsync_auth_username' => $this->settings->getAuthUsername(),
  120. 'fsync_auth_password' => $this->settings->getAuthPassword(),
  121. 'fsync_auth_fiscalcode' => $this->settings->getAuthFiscalCode()
  122. );
  123. }
  124. public function getToken()
  125. {
  126. return $this->db->getToken($this->shop);
  127. }
  128. public function getShop()
  129. {
  130. if(!empty($this->shop))
  131. {
  132. return str_replace(SHOPIFY_DOMAIN, '', $this->shop);
  133. }
  134. else
  135. {
  136. throw new \Exception('A shop name is required for the script to run');
  137. }
  138. }
  139. public function getVersionLink()
  140. {
  141. return VERSION_LINK;
  142. }
  143. public function getVersion()
  144. {
  145. return VERSION;
  146. }
  147. public function setShop($shop = '')
  148. {
  149. $this->shop = '';
  150. if(!empty($shop))
  151. {
  152. $this->shop = $shop;
  153. }
  154. else
  155. {
  156. if(isset($_GET['shop']))
  157. {
  158. $this->shop = $_GET['shop'];
  159. }
  160. }
  161. }
  162. public function getSyncOrdersAs()
  163. {
  164. return array(
  165. \Core\App\Factory\SettingsFactory::SELECT_ORDER,
  166. \Core\App\Factory\SettingsFactory::SELECT_PROFORMA
  167. );
  168. }
  169. public function getAutoSyncOptions()
  170. {
  171. return array(
  172. \Core\App\Factory\SettingsFactory::AUTO_SYNC_ENABLED,
  173. \Core\App\Factory\SettingsFactory::AUTO_SYNC_DISABLED
  174. );
  175. }
  176. public function getOptionWithDiscount()
  177. {
  178. return $this->settings->getOptionWithDiscount();
  179. }
  180. public function getEnabledDisabledOptions()
  181. {
  182. return array(
  183. \Core\App\Factory\SettingsFactory::SYNC_ENABLED,
  184. \Core\App\Factory\SettingsFactory::SYNC_DISABLED
  185. );
  186. }
  187. public function checkLatestVersion($language)
  188. {
  189. $errorMessage = '<h4><span class="alert danger alert-box">' . $language->get('version_check_error') . '</span></h4>';
  190. $versionFile = @fopen(self::VERSION_FILE, "r") or die($errorMessage);
  191. while(!feof($versionFile)) {
  192. $versionRow = explode(self::VERSION_SEPARATOR, fgets($versionFile));
  193. if($versionRow[0] == self::VERSION_PLATFORM_CODE && isset($versionRow[1]))
  194. {
  195. if(trim($this->getVersion()) == trim($versionRow[1]))
  196. {
  197. $successMessage = '<small style="color: #74B807 !important">' . $language->get('version_check_success') . '</small>';
  198. }
  199. else
  200. {
  201. $successMessage = '<small style="color: red !important">' . $language->get('version_check_new') . ': <b>' . $versionRow[1] . '</b>';
  202. if(isset($versionRow[2]) && !empty($versionRow[2]))
  203. {
  204. $successMessage .= ' ' . $language->get('version_check_link_1') . '</small> <u><a style="color: red !important" target="_blank" href="' . $versionRow[2] . '">';
  205. $successMessage .= $language->get('version_check_link_2');
  206. $successMessage .= '</a></u>';
  207. }
  208. }
  209. echo $successMessage;
  210. exit;
  211. }
  212. }
  213. echo '';
  214. fclose($versionFile);
  215. }
  216. }