ProductConfigurationDataProvider.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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: Tomasz Bielecki ( tomasz.bi@thurdata.com )
  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. 'acc_limit',
  30. 'acc_base',
  31. 'alias_limit',
  32. 'cos_name',
  33. 'login_link',
  34. 'filterAccountsByCOS',
  35. 'acc_size',
  36. 'domain_alias_limit',
  37. 'dist_list_limit',
  38. 'domainMaxSize',
  39. 'domainBaseSize',
  40. 'cos',
  41. 'ca_emailAccountPage',
  42. 'ca_ressourcePage',
  43. 'ca_distributionListPage',
  44. 'ca_goToWebmailPage',
  45. 'ca_emailAliasesPage',
  46. 'ca_domainAliasesPage',
  47. 'ca_logInToMailboxButton',
  48. ];
  49. const FILED_NOT_UPDATED = [
  50. 'login_link'
  51. ];
  52. use LangHandler;
  53. /**
  54. *
  55. */
  56. public function read()
  57. {
  58. /**
  59. * load params from previous version
  60. */
  61. $this->checkConfigOrLoadFromPrevious($this->getRequestValue('id'));
  62. $this->loadDefaultData();
  63. $this->overrideDefaultDataByProductConfig();
  64. }
  65. /**
  66. *
  67. */
  68. public function update()
  69. {
  70. $this->catchFormData();
  71. $cos = $this->formData['cos'];
  72. unset($this->formData['cos']);
  73. $productId = $this->request->get('id');
  74. foreach ($this->formData as $key => $value)
  75. {
  76. ProductConfiguration::updateOrCreate(['product_id' => $productId, 'setting' => $key], ['value' => $value]);
  77. }
  78. /**
  79. * save serialized cos
  80. */
  81. ProductConfiguration::updateOrCreate(['product_id' => $productId, 'setting' => 'cos'], ['value' => json_encode($cos)]);
  82. }
  83. /**
  84. *
  85. */
  86. protected function loadDefaultData()
  87. {
  88. /* default data */
  89. $this->data['acc_limit'] = Size::DEFAULT_ACC_LIMIT;
  90. $this->data['acc_base'] = Size::DEFAULT_ACC_BASE;
  91. $this->data['acc_size'] = Size::DEFAULT_ACC_SIZE;
  92. $this->data['alias_limit'] = Size::DEFAULT_ALIAS_LIMIT;
  93. $this->data['domain_alias_limit'] = Size::DEFAULT_DOMAIN_ALIAS_LIMIT;
  94. $this->data['cos_name'] = ClassOfServices::CUSTOM_ZIMBRA;
  95. $this->data['dist_list_limit'] = Size::DEFAULT_DIST_ALIAS_LIMIT;
  96. $this->data['login_link'] = Kerio::DEFAULT_LOGIN_LINK;
  97. $this->data['domainMaxSize'] = Size::UNLIMITED;
  98. $this->data['domainBaseSize'] = Size::UNLIMITED;
  99. /* available class of services*/
  100. $this->availableValues['cos_name'] = [
  101. ClassOfServices::CUSTOM_ZIMBRA => $this->getLang()->absoluteT('addonAA', 'configuration', 'product', 'kerio', 'cos', 'Use Custom Settings'),
  102. ClassOfServices::ZIMBRA_CONFIG_OPTIONS => $this->getLang()->absoluteT('addonAA', 'configuration', 'product', 'kerio', 'cos', 'Allow clients to choose Class Of Service'),
  103. ClassOfServices::CLASS_OF_SERVICE_QUOTA => $this->getLang()->absoluteT('addonAA', 'configuration', 'product', 'kerio', 'cos', 'Allow clients to choose Class Of Service Quota Per Account'),
  104. ];
  105. $manager = new KerioManager();
  106. $repository = $manager->getApiByProduct($this->getRequestValue('id'))->soap->repository();
  107. $cosList = $repository->cos->all();
  108. /**
  109. * class of services from API
  110. */
  111. foreach ($cosList as $cos)
  112. {
  113. /**
  114. * @var $cos \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService
  115. */
  116. $this->availableValues['cos_name'][$cos->getId()] = $this->getLang()->absoluteT($cos->getName());
  117. }
  118. }
  119. /**
  120. * @description load data from database
  121. */
  122. protected function overrideDefaultDataByProductConfig()
  123. {
  124. $settings = ProductConfiguration::where('product_id', $this->request->get('id'))->get();
  125. foreach ($settings as $setting)
  126. {
  127. if('cos' === $setting->setting)
  128. {
  129. $this->data[$setting->setting] = json_decode($setting->value, true);
  130. continue;
  131. }
  132. $this->data[$setting->setting] = $setting->value;
  133. }
  134. }
  135. /**
  136. * @description catch params from request
  137. */
  138. protected function catchFormData()
  139. {
  140. /**
  141. * catch from request
  142. */
  143. $params = array_merge(Kerio::BASE_ACCOUNT_CONFIG,self::FORM_DATA);
  144. foreach ($params as $name)
  145. {
  146. if ($value = $this->request->get($name))
  147. {
  148. $this->formData[$name] = $value;
  149. }elseif(!in_array($name,self::FILED_NOT_UPDATED)){
  150. $this->formData[$name] = ProductParams::SWITCHER_DISABLED;
  151. }else{
  152. $this->formData[$name] = '';
  153. }
  154. }
  155. }
  156. private function checkConfigOrLoadFromPrevious($id)
  157. {
  158. if(ProductConfiguration::where('product_id', $id)->first())
  159. {
  160. return;
  161. }
  162. $migration = new Version1To2\Settings();
  163. $exists = DB::schema()->hasTable($migration->getFromTable());
  164. if(!$exists)
  165. {
  166. return;
  167. }
  168. $previous = DB::table($migration->getFromTable())->where('product_id', $id)->get();
  169. /**
  170. *
  171. */
  172. $prodManager = new ProductConfiguration();
  173. /**
  174. *
  175. * storage all params per product id
  176. */
  177. foreach($previous as $setting)
  178. {
  179. $settings[$setting->product_id][$setting->setting] = $setting->value;
  180. $products[] = $setting->product_id;
  181. }
  182. /**
  183. *
  184. * update setting data & save
  185. */
  186. foreach($settings as $prodId => $settingsArray)
  187. {
  188. $attrs = $migration->updateValues($settingsArray, $prodId);
  189. foreach($attrs as $key => $value)
  190. {
  191. $prodManager->updateOrCreate(['product_id' => $prodId, 'setting' => $key],['value' => $value]);
  192. }
  193. }
  194. /**
  195. *
  196. * new options
  197. */
  198. foreach($migration->getNewFields() as $key => $value)
  199. {
  200. $prodManager->updateOrCreate(['product_id' => $id, 'setting' => $key],['value' => $value]);
  201. }
  202. }
  203. }