-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forms: add tag manager class; insert tag content into the generated t…
…ickets
- Loading branch information
1 parent
81e5472
commit 78b30d9
Showing
11 changed files
with
289 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/** | ||
* --------------------------------------------------------------------- | ||
* | ||
* GLPI - Gestionnaire Libre de Parc Informatique | ||
* | ||
* http://glpi-project.org | ||
* | ||
* @copyright 2015-2024 Teclib' and contributors. | ||
* @copyright 2003-2014 by the INDEPNET Development Team. | ||
* @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
* | ||
* --------------------------------------------------------------------- | ||
* | ||
* LICENSE | ||
* | ||
* This file is part of GLPI. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* --------------------------------------------------------------------- | ||
*/ | ||
|
||
namespace Glpi\Form\Tag; | ||
|
||
final class FormTagsManager | ||
{ | ||
public function getTags(): array | ||
{ | ||
return [ | ||
new Tag(label: "Exemple tag 1", value: "exemple-tag-1"), | ||
new Tag(label: "Exemple tag 2", value: "exemple-tag-2"), | ||
new Tag(label: "Exemple tag 3", value: "exemple-tag-3"), | ||
]; | ||
} | ||
|
||
public function insertTagsContent(string $content): string | ||
{ | ||
return preg_replace_callback( | ||
'/<span.*?data-form-tag="true".*?>.*?<\/span>/', | ||
function ($match) { | ||
$tag = $match[0]; | ||
|
||
// Extract value. | ||
preg_match('/data-form-tag-value="([^"]+)"/', $tag, $value_match); | ||
if (empty($value_match)) { | ||
return ""; | ||
} | ||
|
||
// For now, we return the raw value. | ||
// In futures improvements, the value will be an id and we will | ||
// need to call some kind of object here to transform the id into | ||
// the right string value (for exemple, transforming a question | ||
// id into its name). | ||
return $value_match[1]; | ||
}, | ||
$content | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/** | ||
* --------------------------------------------------------------------- | ||
* | ||
* GLPI - Gestionnaire Libre de Parc Informatique | ||
* | ||
* http://glpi-project.org | ||
* | ||
* @copyright 2015-2024 Teclib' and contributors. | ||
* @copyright 2003-2014 by the INDEPNET Development Team. | ||
* @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
* | ||
* --------------------------------------------------------------------- | ||
* | ||
* LICENSE | ||
* | ||
* This file is part of GLPI. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* --------------------------------------------------------------------- | ||
*/ | ||
|
||
namespace Glpi\Form\Tag; | ||
|
||
/** | ||
* Simple data structure that will be json_encoded and sent to the | ||
* GLPI.RichText.FormTags component. | ||
*/ | ||
final readonly class Tag | ||
{ | ||
public string $label; | ||
public string $html; | ||
|
||
public function __construct( | ||
string $label, | ||
string $value, | ||
) { | ||
$this->label = $label; | ||
|
||
// Build HTML representation of the tag. | ||
$properties = [ | ||
"contenteditable" => "false", | ||
"data-form-tag" => "true", | ||
"data-form-tag-value" => $value, | ||
]; | ||
$properties = implode(" ", array_map( | ||
fn($key, $value) => sprintf('%s="%s"', htmlspecialchars($key), htmlspecialchars($value)), | ||
array_keys($properties), | ||
array_values($properties), | ||
)); | ||
$this->html = sprintf('<span %s>%s</span>', $properties, htmlspecialchars($label)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/** | ||
* --------------------------------------------------------------------- | ||
* | ||
* GLPI - Gestionnaire Libre de Parc Informatique | ||
* | ||
* http://glpi-project.org | ||
* | ||
* @copyright 2015-2024 Teclib' and contributors. | ||
* @copyright 2003-2014 by the INDEPNET Development Team. | ||
* @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
* | ||
* --------------------------------------------------------------------- | ||
* | ||
* LICENSE | ||
* | ||
* This file is part of GLPI. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* --------------------------------------------------------------------- | ||
*/ | ||
|
||
namespace tests\units\Glpi\Form\Tag; | ||
|
||
use GLPITestCase; | ||
use Glpi\Form\Tag\Tag; | ||
|
||
final class FormTagsManager extends GLPITestCase | ||
{ | ||
public function testGetTags() | ||
{ | ||
$tag_manager = new \Glpi\Form\Tag\FormTagsManager(); | ||
$tags = $tag_manager->getTags(); | ||
$this->array($tags)->isNotEmpty(); | ||
|
||
foreach ($tags as $tag) { | ||
$this->object($tag)->isInstanceOf(Tag::class); | ||
} | ||
} | ||
|
||
public function testInsertTagsContent() | ||
{ | ||
$tag_manager = new \Glpi\Form\Tag\FormTagsManager(); | ||
$tag_1 = new Tag(label: "Exemple tag 1", value: "exemple-tag-1"); | ||
$tag_2 = new Tag(label: "Exemple tag 2", value: "exemple-tag-2"); | ||
$tag_3 = new Tag(label: "Exemple tag 3", value: "exemple-tag-3"); | ||
|
||
$content_with_tag = | ||
"My content: $tag_1->html, $tag_2->html and $tag_3->html" | ||
; | ||
$computed_content = $tag_manager->insertTagsContent($content_with_tag); | ||
$this->string($computed_content)->isEqualTo( | ||
'My content: exemple-tag-1, exemple-tag-2 and exemple-tag-3' | ||
); | ||
} | ||
} |
Oops, something went wrong.