PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/metadata/ParentListInput.php

https://gitlab.com/nitm/yii2-widgets
PHP | 97 lines | 69 code | 12 blank | 16 comment | 3 complexity | bc060cf1a33605ec8a9a1337ead115cb MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace nitm\widgets\metadata;
  8. use nitm\helpers\Html;
  9. use yii\widgets\ListView;
  10. use yii\base\Widget;
  11. use nitm\helpers\Icon;
  12. /**
  13. * Alert widget renders a parent from session flash. All flash parents are displayed
  14. * in the sequence they were assigned using setFlash. You can set parent as following:
  15. *
  16. * - \Yii::$app->getSession()->setFlash('error', 'This is the parent');
  17. * - \Yii::$app->getSession()->setFlash('success', 'This is the parent');
  18. * - \Yii::$app->getSession()->setFlash('info', 'This is the parent');
  19. *
  20. * @author Kartik Visweswaran <kartikv2@gmail.com>
  21. * @author Alexander Makarov <sam@rmcerative.ru>
  22. */
  23. class ParentListInput extends \nitm\widgets\ajax\Dropdown
  24. {
  25. public $model;
  26. public $name = 'parent_id_autocomplete';
  27. public $data = [];
  28. public $options = [
  29. 'role' => 'parentListInput',
  30. 'multiple' => true,
  31. 'class' => 'form-control',
  32. ];
  33. public function init()
  34. {
  35. if(!($this->model instanceof \nitm\models\Data))
  36. throw new \yii\base\ErrorException(__CLASS__.'->'.__FUNCTION__."() needs a \\nitm\models\Data based model for the parents list!");
  37. $this->options['id'] = isset($this->options['id']) ? $this->options['id'] : $this->model->isWhat().'parent-list-input'.uniqid();
  38. $parents = [];
  39. foreach((array)$this->model->parents as $model)
  40. $parents[$model->getId()] = $model->title();
  41. $parentsCSV = implode(', ', array_map(function ($parent) { return $parent;}, $parents));
  42. $this->allowClear = false;
  43. $this->data = $parents;
  44. $this->model->parent_ids = array_keys($parents);
  45. $this->options = array_merge($this->options, [
  46. 'title' => $parentsCSV,
  47. 'data-model-id' => $this->model->getId(),
  48. 'data-real-input' => "#".$this->model->isWhat()."-parent_ids",
  49. 'placeholder' => "Search for parents "
  50. ]);
  51. $this->pluginEvents = [
  52. 'select2:select' => 'function (event) {
  53. "use strict";
  54. let $input = $(this),
  55. modelId = $input.data("model-id"),
  56. parentId = event.params.data.id;
  57. if(modelId && parentId)
  58. $.post("/'.$this->model->isWhat().'/add-parent/"+modelId+"/"+parentId, function (result) {
  59. if(result !== false) {
  60. $(\'[role="parentList"]\').append(result);
  61. $nitm.m("utils").notify("Successfully added parent", "success", event.target);
  62. } else {
  63. $nitm.m("utils").notify("Unable to add parent", "error", event.target);
  64. }
  65. });
  66. }',
  67. 'select2:unselect' => 'function (event) {
  68. "use strict";
  69. let $input = $(this),
  70. modelId = $input.data("model-id"),
  71. parentId = event.params.data.id;
  72. if(modelId && parentId)
  73. $.post("/'.$this->model->isWhat().'/remove-parent/"+modelId+"/"+parentId, function (result) {
  74. if(result !== false) {
  75. $nitm.m("utils").notify("Successfully removed parent", "success", event.target);
  76. } else {
  77. $nitm.m("utils").notify("Unable to remove parent", "error", event.target);
  78. }
  79. });
  80. }',
  81. ];
  82. parent::init();
  83. $this->value = array_keys($parents);
  84. }
  85. }