‪TYPO3CMS  11.5
UpdateBitmaskOnFieldChange.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
25 class ‪UpdateBitmaskOnFieldChange implements OnFieldChangeInterface
26 {
27  protected int ‪$position;
28  protected int ‪$total;
29  protected bool ‪$invert;
30  protected string ‪$elementName;
31 
32  public function ‪__construct(int ‪$position, int ‪$total, bool ‪$invert, string ‪$elementName)
33  {
34  $this->position = ‪$position;
35  $this->total = ‪$total;
36  $this->invert = ‪$invert;
37  $this->elementName = ‪$elementName;
38  }
39 
40  public function ‪__toString(): string
41  {
42  return $this->‪generateInlineJavaScript();
43  }
44 
45  public function ‪toArray(): array
46  {
47  return [
48  'name' => 'typo3-backend-form-update-bitmask',
49  'data' => [
50  'position' => ‪$this->position,
51  'total' => ‪$this->total,
52  'invert' => ‪$this->invert,
53  'elementName' => ‪$this->elementName,
54  ],
55  ];
56  }
57 
58  protected function ‪generateInlineJavaScript(): string
59  {
60  $mask = 2 ** ‪$this->position;
61  $unmask = (2 ** ‪$this->total) - $mask - 1;
62  $elementRef = 'document.editform[' . GeneralUtility::quoteJSvalue($this->elementName) . ']';
63  return sprintf(
64  '%s.value = %sthis.checked ? (%s.value|%d) : (%s.value&%d);'
65  . " %s.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));",
66  $elementRef,
67  $this->invert ? '!' : '',
68  $elementRef,
69  $mask,
70  $elementRef,
71  $unmask,
72  $elementRef
73  );
74  }
75 }
‪TYPO3\CMS\Backend\Form\Behavior
Definition: OnFieldChangeInterface.php:18
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\$position
‪int $position
Definition: UpdateBitmaskOnFieldChange.php:27
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\__toString
‪__toString()
Definition: UpdateBitmaskOnFieldChange.php:40
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\toArray
‪toArray()
Definition: UpdateBitmaskOnFieldChange.php:45
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange
Definition: UpdateBitmaskOnFieldChange.php:26
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\$invert
‪bool $invert
Definition: UpdateBitmaskOnFieldChange.php:29
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\$elementName
‪string $elementName
Definition: UpdateBitmaskOnFieldChange.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\$total
‪int $total
Definition: UpdateBitmaskOnFieldChange.php:28
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\__construct
‪__construct(int $position, int $total, bool $invert, string $elementName)
Definition: UpdateBitmaskOnFieldChange.php:32
‪TYPO3\CMS\Backend\Form\Behavior\UpdateBitmaskOnFieldChange\generateInlineJavaScript
‪generateInlineJavaScript()
Definition: UpdateBitmaskOnFieldChange.php:58