Skip to content

Commit

Permalink
First tests of formats manager
Browse files Browse the repository at this point in the history
  • Loading branch information
otrok7 committed Nov 12, 2024
1 parent b4ab064 commit 8446579
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 43 deletions.
19 changes: 15 additions & 4 deletions admin/class-bread-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ public function __construct($plugin_name, $version)
*
* @since 2.8.0
*/
public function enqueue_styles()
public function enqueue_styles($hook)
{
if ($hook != 'toplevel_page_class-bread-admin') {
return;
}
wp_enqueue_style("jquery-ui", plugin_dir_url(__FILE__) . "css/jquery-ui.min.css", false, "1.2", 'all');
wp_enqueue_style("spectrum", plugin_dir_url(__FILE__) . "css/spectrum.css", false, "1.2", 'all');
wp_enqueue_style("admin", plugin_dir_url(__FILE__) . "css/admin.css", false, "1.2", 'all');
Expand All @@ -75,8 +78,11 @@ public function enqueue_styles()
*
* @since 2.8.0
*/
public function enqueue_scripts()
public function enqueue_scripts($hook)
{
if ($hook != 'toplevel_page_class-bread-admin') {
return;
}
wp_enqueue_script('common');
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_script('jquery-ui-accordion');
Expand Down Expand Up @@ -402,8 +408,13 @@ function admin_menu_link()
activate_bread();
global $my_admin_page;
Bread_Activator::activate();
$my_admin_page = add_menu_page('Meeting List', 'Meeting List', 'manage_bread', basename(__FILE__), array(&$this, 'admin_options_page'),
'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyINCiB3aWR0aD0iNTAuMDAwMDAwcHQiIGhlaWdodD0iNTAuMDAwMDAwcHQiIHZpZXdCb3g9IjAgMCA1MC4wMDAwMDAgNTAuMDAwMDAwIg0KIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaWRZTWlkIG1lZXQiPg0KDQo8ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgwLjAwMDAwMCw1MC4wMDAwMD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CA4MyAxNCA4MCA3IC0zIDEzIC0yNiAxNSAtNTYgMyAtNDQgNiAtNTIgMjMNCi01MiAxNiAwIDE5IDcgMTkgNTUgbDAgNTUgNDAgMCBjNDkgMCA1MiAtMTcgMTUgLTczIGwtMjYgLTM3IDMxIDAgYzI3IDAgMzQgNg0KNTAgNDMgMjcgNTggMjUgMTI0IC0zIDE3OCAtMjMgNDMgLTgwIDkxIC0xMjQgMTA0IC0yMyA2IC0yMyA1IC0yMyAtODR6IG04OQ0KLTE2IGMxNyAtMjAgMzEgLTQ1IDMxIC01NSAwIC0xNyAtNyAtMjAgLTQwIC0yMCBsLTQwIDAgMCA1NSBjMCAzMCA0IDU1IDkgNTUNCjUgMCAyMyAtMTYgNDAgLTM1eiIvPg0KPHBhdGggZD0iTTY5IDQyMyBjLTEzIC0xNiAtMTIgLTE3IDQgLTQgOSA3IDE3IDE1IDE3IDE3IDAgOCAtOCAzIC0yMSAtMTN6Ii8+DQo8cGF0aCBkPSJNNDEwIDQzNiBjMCAtMiA4IC0xMCAxOCAtMTcgMTUgLTEzIDE2IC0xMiAzIDQgLTEzIDE2IC0yMSAyMSAtMjEgMTN6Ii8+DQo8cGF0aCBkPSJNNDE5IDczIGMtMTMgLTE2IC0xMiAtMTcgNCAtNCAxNiAxMyAyMSAyMSAxMyAyMSAtMiAwIC0xMCAtOCAtMTcNCi0xN3oiLz4NCjxwYXRoIGQ9Ik00NTUgNTAgYy00IC02IC0zIC0xNiAzIC0yMiA2IC02IDEyIC02IDE3IDIgNCA2IDMgMTYgLTMgMjIgLTYgNg0KLTEyIDYgLTE3IC0yeiIvPg0KPC9nPg0KPC9zdmc+'
);
}
function admin_options_page()
Expand Down
9 changes: 2 additions & 7 deletions admin/partials/_meetings_setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,7 @@
<option value="">Not Used</option>
<option value="@Virtual@" <?php echo Bread::getOption('additional_list_format_key') == '@Virtual@' ? 'selected' : '' ?>>Virtual Meetings</option>
<option value="@F2F@" <?php echo Bread::getOption('additional_list_format_key') == '@F2F@' ? 'selected' : '' ?>>Face-to-Face Meetings</option>
<?php
if ($this_connected) {
$used_formats = Bread_Bmlt::getFormatsForSelect(true);
}
<?php $used_formats = Bread_Bmlt::getFormatsForSelect(true);
$countmax = count($used_formats);
for ($count = 0; $count < $countmax; $count++) {
if ($used_formats[$count]['key_string'] == Bread::getOption('additional_list_format_key')) { ?>
Expand All @@ -381,9 +378,7 @@
<option value="<?php echo esc_html($used_formats[$count]['key_string']) ?>"><?php echo esc_html($used_formats[$count]['name_string']) ?></option>
<?php }
}
} else { ?>
<option selected="selected" value="<?php echo Bread::getOption('additional_list_format_key'); ?>"><?php echo 'additional_list'; ?></option>
<?php } ?>
} ?>
</select>
</p>
<p>
Expand Down
14 changes: 9 additions & 5 deletions public/class-bread-content-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private function standard_shortcode_replacement(string $page): string

$search_strings[] = '[meeting_count]';
$replacements[] = $this->meeting_count;
$data = $this->options[$page.'_content'];
$data = $this->options[$page . '_content'];
$data = $this->locale_month_replacement($data, 'lower');
$data = $this->locale_month_replacement($data, 'upper');
$data = str_replace($search_strings, $replacements, $data);
Expand Down Expand Up @@ -564,23 +564,27 @@ function write_additional_meetinglist()
}
$additional_list_query = false;
$additional_meetinglist_result = $this->result_meetings;
if (empty($this->options['additional_list_format_key']) || $this->options['additional_list_format_key'] == 'additional_list') {
/**
* If we are selecting the meetings in the second list based on some format, we don't need another BMLT query.
* Except if its an service meeting (format code ASM). Then we need a second query because of the protected fields.
*/
if (empty($this->options['additional_list_format_key']) || $this->options['additional_list_format_key'] == 'ASM') {
$additional_list_query = true;
$sort_order = $this->options['additional_list_sort_order'];
if ($sort_order == 'same') {
$sort_order = 'weekday_tinyint,start_time';
}
$additional_list_id = "";
if ($this->options['additional_list_format_key'] === 'additional_list') {
$additional_list_id = '&formats[]=' . $this->formatsManager->getFormatByKey($this->options['weekday_language'], 'additional_list');
if ($this->options['additional_list_format_key'] === 'ASM') {
$additional_list_id = '&formats[]=' . $this->formatsManager->getFormatByKey($this->options['weekday_language'], 'ASM');
}
$services = Bread_Bmlt::generateDefaultQuery();
if (!empty($this->options['additional_list_custom_query'])) {
$services = $this->options['additional_list_custom_query'];
}
$additional_list_query = "client_interface/json/?switcher=GetSearchResults$services$additional_list_id&sort_keys=$sort_order";
// additional_list can contain E-Mail and phone numbers that require logins.
if ($this->options['additional_list_format_key'] === 'additional_list') {
if ($this->options['additional_list_format_key'] === 'ASM') {
$additional_list_query .= "&advanced_published=0";
}
$additional_meetinglist_result = Bread_Bmlt::get_configured_root_server_request($additional_list_query);
Expand Down
90 changes: 63 additions & 27 deletions public/class-bread-format-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ class Bread_FormatsManager
* @var string
*/
private string $defaultLang;
/**
* The format indicating wheelchair accessibility.
*
* @var array|null
*/
private array|null $wheelchairFormat = array();
/**
* The info regarding the formats used is available already during construction because it is returned by the initial root server query.
*
* @param array $usedFormats The array of formats.
* @param array $usedFormats The array of formats actually used by meetings in the meeting list.
* @param string $lang The language of the formats.
*/
function __construct(array &$usedFormats, string $lang)
Expand All @@ -42,15 +47,6 @@ function __construct(array &$usedFormats, string $lang)
$this->hashedFormats[$lang] = $this->hashFormats($usedFormats);
$this->defaultLang = $lang;
}
/**
* Returns the array of formats that are actually used by meetings in the meeting list. Key strings and descriptions are from the default language.
*
* @var array
*/
public function getFormatsUsed(): array
{
return $this->usedFormats[$this->defaultLang];
}
/**
* Helper functtion to create a key=>value array of formats for convenient lookup
*
Expand Down Expand Up @@ -81,14 +77,14 @@ private function loadFormats(string $lang): void
$this->hashedFormats[$lang] = $this->hashFormats($this->allFormats[$lang]);
}
/**
* Undocumented function
* ULookup the format having a particular field having a particular value. Null if none found.
*
* @param string $lang
* @param string $lang the language of the formats being searched.
* @param string $field
* @param string $id
* @return array
* @return array|null
*/
public function getFormatFromField(string $lang, string $field, string $id): array|null
private function getFormatFromField(string $lang, string $field, string $id): array|null
{
if (!isset($this->allFormats[$lang])) {
if (isset($this->usedFormats[$lang])) {
Expand All @@ -101,7 +97,15 @@ public function getFormatFromField(string $lang, string $field, string $id): arr
}
return $this->searchField($this->allFormats[$lang], $id, $field);
}
private function searchField(array $formats, string $id, string $field)
/**
* Do the actual search for a loaded set of formats.
*
* @param array $formats
* @param string $id
* @param string $field
* @return array|null
*/
private function searchField(array $formats, string $id, string $field): array|null
{
foreach ($formats as $format) {
if ($format[$field] == $id) {
Expand All @@ -110,7 +114,14 @@ private function searchField(array $formats, string $id, string $field)
}
return null;
}
public function getFormatByKey(string $lang, string $key)
/**
* Lookup a format in the hashed list.
*
* @param string $lang
* @param string $key
* @return array|null
*/
public function getFormatByKey(string $lang, string $key): array|null
{
if (!isset($this->hashedFormats[$lang])) {
$this->loadFormats($lang);
Expand All @@ -127,27 +138,43 @@ public function getFormatByKey(string $lang, string $key)
}
return null;
}
public function getUsedFormats(string $lang)
/**
* Get the list of formats that were actually used, translated into the specified language.
*
* @param string $lang
* @return array
*/
public function getFormatsUsed(string $lang=''): array
{
if (isset($this->usedFormats[$lang])) {
return $this->usedFormats[$lang];
}
$this->loadFormats($lang);
$this->usedFormats[$lang] = array();
foreach ($this->usedFormats[$this->defaultLang] as $usedFormat) {
$this->usedFormats[$lang] = $this->getFormatFromField($lang, 'id', $usedFormat['id']);
$lang = ($lang == '') ? $this->defaultLang : $lang;
if (!isset($this->usedFormats[$lang])) {
$this->loadFormats($lang);
$this->usedFormats[$lang] = array();
foreach ($this->usedFormats[$this->defaultLang] as $usedFormat) {
$this->usedFormats[$lang][] = $this->getFormatFromField($lang, 'id', $usedFormat['id']);
}
}
return $this->usedFormats[$lang];
}
public function getHashedFormats(string $lang)
public function getHashedFormats(string $lang): array
{
if (!isset($this->hashedFormats[$lang])) {
$this->loadFormats($lang);
}
return $this->hashedFormats[$lang];
}
/**
* Generate the HTML table of formats.
*
* @param string $lang
* @param boolean $isAll All formats or only used.
* @param string $lineHeight
* @param string $fontSize
* @return void
*/
public function write_detailed_formats(string $lang, bool $isAll, string $lineHeight, string $fontSize)
{
$formats = $isAll ? $this->allFormats[$lang] : $this->getUsedFormats($lang);
$formats = $isAll ? $this->allFormats[$lang] : $this->getFormatsUsed($lang);
if (empty($formats)) {
return '';
}
Expand All @@ -159,9 +186,18 @@ public function write_detailed_formats(string $lang, bool $isAll, string $lineHe
$data .= "</table>";
return $data;
}
/**
* Generate the HTML table of formats.
*
* @param string $lang
* @param boolean $isAll All formats or only used.
* @param string $lineHeight
* @param string $fontSize
* @return void
*/
public function write_formats(string $lang, bool $isAll, string $lineHeight, string $fontSize)
{
$formats = $isAll ? $this->allFormats[$lang] : $this->getUsedFormats($lang);
$formats = $isAll ? $this->allFormats[$lang] : $this->getFormatsUsed($lang);
if (empty($formats)) {
return '';
}
Expand Down
30 changes: 30 additions & 0 deletions tests/BreadFormatsManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
use PHPUnit\Framework\TestCase;

use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertNotNull;

final class BreadFormatsManagerTest extends TestCase
{
private function getFormats($formats): array
{
$json = file_get_contents('tests/formats/'.$formats.".json");
return json_decode($json, true)['formats'];
}
private function getFormatMgr($usedFormat, $lang)
{
Bread_Bmlt::setFormatBase('german-formats');
return new Bread_FormatsManager($this->getFormats($usedFormat), $lang);
}
public function testGetFormatsUsed() {
$mgr = $this->getFormatMgr('berlin-formats-de','de');
$used = $mgr->getFormatsUsed();
assertEquals(50,count($used));
$o1 = $mgr->getFormatByKey('de','O1');
assertNotNull($o1);
$o1e = $mgr->getFormatByKey('en','O1');
assertNotNull($o1e);
$used = $mgr->getFormatsUsed('en');
assertEquals(50,count($used));
}
}

0 comments on commit 8446579

Please sign in to comment.