inetis.listswitch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.3 KiB

4 years ago
  1. <?php namespace Inetis\ListSwitch;
  2. use Backend\Classes\ListColumn;
  3. use Lang;
  4. use Model;
  5. class ListSwitchField
  6. {
  7. /**
  8. * Default field configuration
  9. * all these params can be overrided by column config
  10. * @var array
  11. */
  12. private static $defaultFieldConfig = [
  13. 'icon' => true,
  14. 'titleTrue' => 'inetis.listswitch::lang.inetis.listswitch.title_true',
  15. 'titleFalse' => 'inetis.listswitch::lang.inetis.listswitch.title_false',
  16. 'textTrue' => 'inetis.listswitch::lang.inetis.listswitch.text_true',
  17. 'textFalse' => 'inetis.listswitch::lang.inetis.listswitch.text_false',
  18. 'request' => 'onSwitchInetisListField'
  19. ];
  20. private static $listConfig = [];
  21. /**
  22. * @param $field
  23. * @param array $config
  24. *
  25. * @internal param $name
  26. */
  27. public static function storeFieldConfig($field, array $config)
  28. {
  29. self::$listConfig[$field] = array_merge(self::$defaultFieldConfig, $config, ['name' => $field]);
  30. }
  31. /**
  32. * @param $value
  33. * @param ListColumn $column
  34. * @param Model $record
  35. *
  36. * @return string HTML
  37. */
  38. public static function render($value, ListColumn $column, Model $record)
  39. {
  40. $field = new self($value, $column, $record);
  41. $config = $field->getConfig();
  42. return '
  43. <a href="javascript:;"
  44. data-request="' . $config['request'] . '"
  45. data-request-data="' . $field->getRequestData() . '"
  46. data-stripe-load-indicator
  47. title="' . $field->getButtonTitle() . '">
  48. ' . $field->getButtonValue() . '
  49. </a>
  50. ';
  51. }
  52. /**
  53. * ListSwitchField constructor.
  54. *
  55. * @param $value
  56. * @param ListColumn $column
  57. * @param Model $record
  58. */
  59. public function __construct($value, ListColumn $column, Model $record)
  60. {
  61. $this->name = $column->columnName;
  62. $this->value = $value;
  63. $this->column = $column;
  64. $this->record = $record;
  65. }
  66. /**
  67. * @param $config
  68. *
  69. * @return mixed
  70. */
  71. private function getConfig($config = null)
  72. {
  73. if (is_null($config)) {
  74. return self::$listConfig[$this->name];
  75. }
  76. return self::$listConfig[$this->name][$config];
  77. }
  78. /**
  79. * Return data-request-data params for the switch button
  80. *
  81. * @return string
  82. */
  83. public function getRequestData()
  84. {
  85. $modelClass = str_replace('\\', '\\\\', get_class($this->record));
  86. $data = [
  87. "id: {$this->record->{$this->record->getKeyName()}}",
  88. "field: '$this->name'",
  89. "model: '$modelClass'"
  90. ];
  91. if (post('page')) {
  92. $data[] = "page: " . post('page');
  93. }
  94. return implode(', ', $data);
  95. }
  96. /**
  97. * Return button text or icon
  98. *
  99. * @return string
  100. */
  101. public function getButtonValue()
  102. {
  103. if (!$this->getConfig('icon')) {
  104. return Lang::get($this->getConfig($this->value ? 'textTrue' : 'textFalse'));
  105. }
  106. if ($this->value) {
  107. return '<i class="oc-icon-check"></i>';
  108. }
  109. return '<i class="oc-icon-times"></i>';
  110. }
  111. /**
  112. * Return button hover title
  113. *
  114. * @return string
  115. */
  116. public function getButtonTitle()
  117. {
  118. return Lang::get($this->getConfig($this->value ? 'titleTrue' : 'titleFalse'));
  119. }
  120. }