ProductConfigurationDataProvider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Admin\ProductConfiguration\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
  4. use ThurData\Servers\KerioEmail\App\Enums\Size;
  5. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  6. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  9. use ThurData\Servers\KerioEmail\App\Traits\LangHandler;
  10. use ThurData\Servers\KerioEmail\App\Traits\ServerParams;
  11. use ThurData\Servers\KerioEmail\App\Traits\KerioApiHandler;
  12. use ThurData\Servers\KerioEmail\Core\App\Controllers\Interfaces\AdminArea;
  13. use ThurData\Servers\KerioEmail\Core\Traits\Lang;
  14. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  15. use \ThurData\Servers\KerioEmail\App\Models\ProductConfiguration;
  16. use ThurData\Servers\KerioEmail\App\Libs\Migrations\Drivers\Version1To2;
  17. use \Illuminate\Database\Capsule\Manager as DB;
  18. /**
  19. *
  20. * Created by PhpStorm.
  21. * User: ThurData
  22. * Date: 29.08.19
  23. * Time: 10:27
  24. * Class ProductConfigurationDataProvider
  25. */
  26. class ProductConfigurationDataProvider extends BaseDataProvider implements AdminArea
  27. {
  28. const FORM_DATA = [
  29. 'domain_size_limit',
  30. 'account_size_limit',
  31. 'account_limit',
  32. 'alias_limit',
  33. 'domain_alias_limit',
  34. 'dist_list_limit',
  35. 'outllok_connector',
  36. 'login_link',
  37. 'ca_emailAccountPage',
  38. 'ca_ressourcePage',
  39. 'ca_distributionListPage',
  40. 'ca_goToWebmailPage',
  41. 'ca_emailAliasesPage',
  42. 'ca_domainAliasesPage',
  43. 'ca_logInToMailboxButton',
  44. ];
  45. const FILED_NOT_UPDATED = [
  46. 'login_link'
  47. ];
  48. use LangHandler;
  49. /**
  50. *
  51. */
  52. public function read()
  53. {
  54. /**
  55. * load params from previous version
  56. */
  57. $this->checkConfigOrLoadFromPrevious($this->getRequestValue('id'));
  58. $this->loadDefaultData();
  59. $this->overrideDefaultDataByProductConfig();
  60. }
  61. /**
  62. *
  63. */
  64. public function update()
  65. {
  66. $this->catchFormData();
  67. $productId = $this->request->get('id');
  68. foreach ($this->formData as $key => $value)
  69. {
  70. ProductConfiguration::updateOrCreate(['product_id' => $productId, 'setting' => $key], ['value' => $value]);
  71. }
  72. }
  73. /**
  74. *
  75. */
  76. protected function loadDefaultData()
  77. {
  78. /* default data */
  79. $this->data['domain_size_limit'] = Size::DEFAULT_DOMAIN_LIMIT;
  80. $this->data['account_size_limit'] = Size::DEFAULT_ACCOUNT_SIZE;
  81. $this->data['account_limit'] = Size::DEFAULT_ACCOUNT_LIMIT;
  82. $this->data['alias_limit'] = Size::DEFAULT_ALIAS_LIMIT;
  83. $this->data['domain_alias_limit'] = Size::DEFAULT_DOMAIN_ALIAS_LIMIT;
  84. $this->data['dist_list_limit'] = Size::DEFAULT_DIST_ALIAS_LIMIT;
  85. $this->data['outllok_connector'] = Kerio::ATTR_DISABLED;
  86. $this->data['login_link'] = Kerio::DEFAULT_LOGIN_LINK;
  87. }
  88. /**
  89. * @description load data from database
  90. */
  91. protected function overrideDefaultDataByProductConfig()
  92. {
  93. $settings = ProductConfiguration::where('product_id', $this->request->get('id'))->get();
  94. foreach ($settings as $setting)
  95. {
  96. $this->data[$setting->setting] = $setting->value;
  97. }
  98. }
  99. /**
  100. * @description catch params from request
  101. */
  102. protected function catchFormData()
  103. {
  104. /**
  105. * catch from request
  106. */
  107. $params = array_merge(Kerio::BASE_ACCOUNT_CONFIG,self::FORM_DATA);
  108. foreach ($params as $name)
  109. {
  110. if ($value = $this->request->get($name))
  111. {
  112. $this->formData[$name] = $value;
  113. }elseif(!in_array($name,self::FILED_NOT_UPDATED)){
  114. $this->formData[$name] = ProductParams::SWITCHER_DISABLED;
  115. }else{
  116. $this->formData[$name] = '';
  117. }
  118. }
  119. }
  120. private function checkConfigOrLoadFromPrevious($id)
  121. {
  122. if(ProductConfiguration::where('product_id', $id)->first())
  123. {
  124. return;
  125. }
  126. $migration = new Version1To2\Settings();
  127. $exists = DB::schema()->hasTable($migration->getFromTable());
  128. if(!$exists)
  129. {
  130. return;
  131. }
  132. $previous = DB::table($migration->getFromTable())->where('product_id', $id)->get();
  133. /**
  134. *
  135. */
  136. $prodManager = new ProductConfiguration();
  137. /**
  138. *
  139. * storage all params per product id
  140. */
  141. foreach($previous as $setting)
  142. {
  143. $settings[$setting->product_id][$setting->setting] = $setting->value;
  144. $products[] = $setting->product_id;
  145. }
  146. /**
  147. *
  148. * update setting data & save
  149. */
  150. foreach($settings as $prodId => $settingsArray)
  151. {
  152. $attrs = $migration->updateValues($settingsArray, $prodId);
  153. foreach($attrs as $key => $value)
  154. {
  155. $prodManager->updateOrCreate(['product_id' => $prodId, 'setting' => $key],['value' => $value]);
  156. }
  157. }
  158. /**
  159. *
  160. * new options
  161. */
  162. foreach($migration->getNewFields() as $key => $value)
  163. {
  164. $prodManager->updateOrCreate(['product_id' => $id, 'setting' => $key],['value' => $value]);
  165. }
  166. }
  167. }