summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLester Caine <lester@lsces.co.uk>2026-06-06 09:40:55 +0100
committerLester Caine <lester@lsces.co.uk>2026-06-06 09:40:55 +0100
commite979cd1ddcfc3f86f55b6385ac65372ec84796a5 (patch)
treec48457a2c8d010c71fbb1c4b6210671bb4da71bd
parentaed33e90f6628b281cd1c8ae09f9bed38960a333 (diff)
downloadliberty-e979cd1ddcfc3f86f55b6385ac65372ec84796a5.tar.gz
liberty-e979cd1ddcfc3f86f55b6385ac65372ec84796a5.tar.bz2
liberty-e979cd1ddcfc3f86f55b6385ac65372ec84796a5.zip
xref group templates: drop Link column; tighten text item edit gating
- list_xref.tpl: remove dead Link <th>; add 30/30/40% column widths; fix colspan to conditional 6/3; support both LibertyXrefGroup and legacy $source paths - view_xref_text_item.tpl: remove &nbsp; Link cell; wrap date/updated/ edit cells under single {if $xrefAllowEdit|default:false} - LibertyXrefGroup: new class — loads xref rows for one x_group, separates expired rows into history Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
-rw-r--r--includes/classes/LibertyXrefGroup.php87
-rw-r--r--templates/list_xref.tpl66
-rw-r--r--templates/view_xref_text_item.tpl31
3 files changed, 153 insertions, 31 deletions
diff --git a/includes/classes/LibertyXrefGroup.php b/includes/classes/LibertyXrefGroup.php
new file mode 100644
index 0000000..1114c88
--- /dev/null
+++ b/includes/classes/LibertyXrefGroup.php
@@ -0,0 +1,87 @@
+<?php
+/**
+ * @package liberty
+ * @subpackage classes
+ */
+
+namespace Bitweaver\Liberty;
+
+/**
+ * One xref group (content_type_guid + x_group) with its raw xref data rows for a content item.
+ * Defined by liberty_xref_item rows sharing this x_group; data loaded from liberty_xref.
+ * Active items land in mXrefs; expired items (type_source='history') are returned by load()
+ * for LibertyXrefInfo to collect into the synthetic history group.
+ */
+class LibertyXrefGroup extends LibertyBase {
+ public string $mXGroup;
+ public string $mContentTypeGuid;
+ public string $mTitle;
+ public int $mSortOrder;
+ public ?string $mTemplate;
+ public int $mRoleId;
+ /** @var array[] active xref data rows for the current content item */
+ public array $mXrefs = [];
+
+ public function __construct( array $groupRow, string $contentTypeGuid ) {
+ parent::__construct();
+ $this->mXGroup = $groupRow['x_group'];
+ $this->mContentTypeGuid = $contentTypeGuid;
+ $this->mTitle = $groupRow['title'];
+ $this->mSortOrder = (int)( $groupRow['sort_order'] ?? 0 );
+ $this->mTemplate = !empty( $groupRow['template'] ) ? trim( $groupRow['template'] ) : null;
+ $this->mRoleId = (int)( $groupRow['role_id'] ?? 0 );
+ }
+
+ /**
+ * Load xref rows for this group. Active rows go into mXrefs.
+ * Expired rows (type_source='history') are returned for LibertyXrefInfo to collect.
+ * Requires mContentTypeGuid — fails loudly if not set.
+ *
+ * @return array expired rows to be added to the history group
+ */
+ public function loadXrefs( int $contentId ): array {
+ global $gBitUser;
+
+ if( empty( $this->mContentTypeGuid ) ) {
+ $this->mErrors[] = 'LibertyXrefGroup::load() requires mContentTypeGuid — cannot scope liberty_xref_item query without it.';
+ return [];
+ }
+
+ $roles = array_keys( $gBitUser->mRoles ?? [] ) ?: [-1];
+ $userId = $gBitUser->mUserId;
+ $bindVars = array_merge( [ $this->mDb->NOW(), $contentId ], $roles, [ $userId ] );
+
+ $sql = "SELECT x.`xref_id`, x.`item`, x.`xref`, x.`xkey`, x.`xkey_ext`,
+ x.`xorder`, x.`data`, x.`start_date`, x.`end_date`, x.`last_update_date`,
+ s.`template`, s.`cross_ref_href`,
+ CASE WHEN x.`xorder` = 0 THEN s.`cross_ref_title`
+ ELSE s.`cross_ref_title` || '-' || x.`xorder` END AS xref_title,
+ CASE WHEN x.`end_date` IS NOT NULL AND x.`end_date` < ? THEN 'history'
+ ELSE s.`x_group` END AS type_source,
+ pc.`add1` || ',' || pc.`add2` || ',' || pc.`add4` || ',' || pc.`town` AS address
+ FROM `" . BIT_DB_PREFIX . "liberty_xref` x
+ JOIN `" . BIT_DB_PREFIX . "liberty_xref_item` s
+ ON s.`item` = x.`item`
+ AND s.`content_type_guid` = '{$this->mContentTypeGuid}'
+ AND s.`x_group` = '{$this->mXGroup}'
+ LEFT JOIN `" . BIT_DB_PREFIX . "address_postcode` pc ON pc.`postcode` = x.`xkey`
+ LEFT OUTER JOIN `" . BIT_DB_PREFIX . "users_roles_map` purm
+ ON purm.`user_id` = $userId AND purm.`role_id` = s.`role_id`
+ WHERE x.`content_id` = ?
+ AND (s.`role_id` IN(" . implode( ',', array_fill( 0, count( $roles ), '?' ) ) . ") OR purm.`user_id` = ?)
+ ORDER BY x.`item`, x.`xorder`";
+
+ $historyRows = [];
+ $result = $this->mDb->query( $sql, $bindVars );
+ if( $result ) {
+ while( $row = $result->fetchRow() ) {
+ if( $row['type_source'] === 'history' ) {
+ $historyRows[] = $row;
+ } else {
+ $this->mXrefs[] = $row;
+ }
+ }
+ }
+ return $historyRows;
+ }
+}
diff --git a/templates/list_xref.tpl b/templates/list_xref.tpl
index 6ffa8c4..21b2608 100644
--- a/templates/list_xref.tpl
+++ b/templates/list_xref.tpl
@@ -1,21 +1,60 @@
+{assign var=xrefAllowEdit value=$allow_edit|default:true}
+{if isset($xrefGroup)}
+ {* New path: called with a LibertyXrefGroup object *}
+ {assign var=tabTitle value=$xrefGroup->mTitle}
+ {assign var=isHistory value=($xrefGroup->mXGroup eq 'history')}
+ {jstab title="`$tabTitle` ({$xrefGroup->mXrefs|@count})"}
+ {legend legend=$tabTitle}
+ <div class="form-group table-responsive">
+ <table>
+ <thead>
+ <tr>
+ <th style="width:30%">{tr}Type{/tr}</th>
+ <th style="width:30%">{tr}Key{/tr}</th>
+ <th style="width:40%">{tr}Value{/tr}</th>
+ {if $xrefAllowEdit}
+ {if $isHistory}<th>{tr}Ended{/tr}</th>{else}<th>{tr}Started{/tr}</th>{/if}
+ <th>{tr}Updated{/tr}</th>
+ <th>{tr}Edit{/tr}</th>
+ {/if}
+ </tr>
+ </thead>
+ <tbody>
+ {if $xrefGroup->mXrefs}
+ {foreach $xrefGroup->mXrefs as $xrefInfo}
+ <tr class="{cycle values="even,odd"}">
+ {include file=$gContent->getXrefRecordTemplate($xrefInfo.template)}
+ </tr>
+ {/foreach}
+ {else}
+ <tr class="norecords">
+ <td colspan="{if $xrefAllowEdit}6{else}3{/if}">{tr}No {$tabTitle} records found{/tr}</td>
+ </tr>
+ {/if}
+ </tbody>
+ </table>
+ </div>
+ {if $allow_add && $gContent->isValid() && $gContent->hasUpdatePermission() && !$isHistory}
+ <div>
+ {smartlink ititle="Add record" ipackage="liberty" ifile="add_xref.php" biticon="list-add" content_id=$gContent->mInfo.content_id group=$xrefGroup->mSortOrder}
+ </div>
+ {/if}
+ {/legend}
+ {/jstab}
+{else}
+ {* Legacy path: called with $source and $source_title (stock, other packages) *}
{assign var=xrefcnt value=$gContent->mInfo.$source|default:[]|@count}
- {assign var=xrefAllowEdit value=$allow_edit|default:true}
{jstab title="$source_title ($xrefcnt)"}
{legend legend=$source_title}
<div class="form-group table-responsive">
<table>
<thead>
<tr>
- <th>{tr}Type{/tr}</th>
- <th>{tr}Link{/tr}</th>
- <th>{tr}Key{/tr}</th>
- <th>{tr}Value{/tr}</th>
+ <th style="width:30%">{tr}Type{/tr}</th>
+ <th style="width:30%">{tr}Key{/tr}</th>
+ <th style="width:40%">{tr}Value{/tr}</th>
{if $xrefAllowEdit}
- {if $source ne 'history'}
- <th>{tr}Started{/tr}</th>
- {else}
- <th>{tr}Ended{/tr}</th>
- {/if}
+ {if $source ne 'history'}<th>{tr}Started{/tr}</th>{else}<th>{tr}Ended{/tr}</th>{/if}
<th>{tr}Updated{/tr}</th>
<th>{tr}Edit{/tr}</th>
{/if}
@@ -23,13 +62,13 @@
</thead>
<tbody>
{section name=xref loop=$gContent->mInfo.$source}
- {assign var=_rowTpl value=$gContent->mInfo.$source[xref].template}
+ {assign var=xrefInfo value=$gContent->mInfo.$source[xref]}
<tr class="{cycle values="even,odd"}">
- {include file=$gContent->getXrefRecordTemplate($_rowTpl)}
+ {include file=$gContent->getXrefRecordTemplate($xrefInfo.template)}
</tr>
{sectionelse}
<tr class="norecords">
- <td colspan="7">{tr}No {$source_title} records found{/tr}</td>
+ <td colspan="{if $xrefAllowEdit}6{else}3{/if}">{tr}No {$source_title} records found{/tr}</td>
</tr>
{/section}
</tbody>
@@ -42,3 +81,4 @@
{/if}
{/legend}
{/jstab}
+{/if}
diff --git a/templates/view_xref_text_item.tpl b/templates/view_xref_text_item.tpl
index dbd534a..0c9523a 100644
--- a/templates/view_xref_text_item.tpl
+++ b/templates/view_xref_text_item.tpl
@@ -1,34 +1,29 @@
{strip}
-<td>{$gContent->mInfo.$source[xref].source_title|escape}</td>
-<td>
- {if isset($gContent->mInfo.$source[xref].xref) && $gContent->mInfo.$source[xref].xref > 100}
- {$gContent->mInfo.$source[xref].xref|escape}
- {else}
- &nbsp;
- {/if}
-</td>
-<td>{$gContent->mInfo.$source[xref].xkey|escape} {$gContent->mInfo.$source[xref].xkey_ext|escape}</td>
-<td>{$gContent->mInfo.$source[xref].data|escape}</td>
+<td>{$xrefInfo.xref_title|escape}</td>
+<td>{$xrefInfo.xkey|escape} {$xrefInfo.xkey_ext|escape}</td>
+<td>{$xrefInfo.data|escape}</td>
+{if $xrefAllowEdit|default:false}
<td>
{if $source ne 'history'}
- {$gContent->mInfo.$source[xref].start_date|bit_short_date}
+ {$xrefInfo.start_date|bit_short_date}
{else}
- {$gContent->mInfo.$source[xref].end_date|bit_short_date}
+ {$xrefInfo.end_date|bit_short_date}
{/if}
</td>
-<td>{$gContent->mInfo.$source[xref].last_update_date|bit_short_date}</td>
+<td>{$xrefInfo.last_update_date|bit_short_date}</td>
<td>
<span class="actionicon">
- {if $xrefAllowEdit|default:true && $gContent->hasUpdatePermission() && $source ne 'history'}
- {smartlink ititle="Edit" ipackage="liberty" ifile="edit_xref.php" biticon="edit" content_id=$gContent->mInfo.content_id xref_id=$gContent->mInfo.$source[xref].xref_id}
+ {if $gContent->hasUpdatePermission() && $source ne 'history'}
+ {smartlink ititle="Edit" ipackage="liberty" ifile="edit_xref.php" biticon="edit" content_id=$gContent->mInfo.content_id xref_id=$xrefInfo.xref_id}
{/if}
- {if $xrefAllowEdit|default:true && $gContent->hasExpungePermission()}
+ {if $gContent->hasExpungePermission()}
{if $source eq 'history'}
- {smartlink ititle="Restore" ipackage="liberty" ifile="edit_xref.php" biticon="edit" content_id=$gContent->mInfo.content_id xref_id=$gContent->mInfo.$source[xref].xref_id expunge=-1}
+ {smartlink ititle="Restore" ipackage="liberty" ifile="edit_xref.php" biticon="edit" content_id=$gContent->mInfo.content_id xref_id=$xrefInfo.xref_id expunge=-1}
{else}
- {smartlink ititle="Delete" ipackage="liberty" ifile="edit_xref.php" biticon="user-trash" content_id=$gContent->mInfo.content_id xref_id=$gContent->mInfo.$source[xref].xref_id expunge=1}
+ {smartlink ititle="Delete" ipackage="liberty" ifile="edit_xref.php" biticon="user-trash" content_id=$gContent->mInfo.content_id xref_id=$xrefInfo.xref_id expunge=1}
{/if}
{/if}
</span>
</td>
+{/if}
{/strip}