Real Content in PHP5 CMS: Part 3

0
81
7 min read

Administering text items—viewer

Generating the XHTML is handled in a separate class, thus implementing the principles of the MVC pattern. The viewer class constructor establishes strings for translation in a way that will allow them to be picked up by gettext, as well as invoking the constructor in the parent class basicAdminHTML, which will provide useful methods and also transfer information such as the page navigation object from the controller object passed as a parameter:

class listTextHTML extends basicAdminHTML
{
public function __construct ($controller)
{
parent::__construct($controller);
$lang_strings = array(T_('Simple Text'),T_('Title'),
T_('Byline'),T_('Version'),
T_('Publishing'),T_('Published'),
T_('Start date'),T_('End date'),
T_('Article text'),T_('Metadata'),
T_('Keys'),T_('Description'),
T_('Hits'),T_('ID'));
$this->translations = array_combine(
$lang_strings, $lang_strings);
}

The actual display of a list of text items is then quite simple, involving the creation of a heading first, followed by a loop through the text items, and then some final XHTML including hidden fields that allow for effective navigation. Note that the parent class will have set up $this->optionurl and $this->optionline to help in the construction of links within the component and a hidden variable to identify the component respectively.

public function view ($rows)
{
$mainhtml = $this->listview($rows);
echo <<<ALL_HTML
$mainhtml
<div>
<input type="hidden" name="task" value="" />
$this->optionline
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="hidemainmenu" value="0" />
</div>

ALL_HTML;

}

The view method does very little, relying on the listview method for most of the work, and only adding hidden fields needed to ensure that navigation and the toolbar will work correctly. Note that the parent class helps us by setting $this->optionline with a hidden input field for the critical option variable needed to ensure the correct component is invoked when the form is submitted. Actual XHTML form tags are created by the CMS framework so that every administrator page is a form. The reason for splitting the page creation in this way will become apparent later, when we look at menu creation. So, moving on to the listview method, we find quite a lot of simple code, which is mainly just a definition of the page in XHTML. The second and third parameters will be set differently from their default values when we come to menu creation.

public function listview ($rows, $showlinks=true, $subhead='')
{
$rowcount = count($rows);
$html = <<<ADMIN_HEADER

{$this->header($subhead)}
<table class="adminlist" width="100%">
<thead>
<tr>
<th width="3%" class="title">
<input type="checkbox" name="toggle" value=""
onclick="checkAll($rowcount);" />
</th>
<th>
{$this->T_('ID')}
</th>
<th width="50%" class="title">
{$this->T_('Title')}
</th>
<th>
{$this->T_('Byline')}
</th>
<th>
{$this->T_('Hits')}
</th>
<th align="left">
{$this->T_('Published')}
</th>
</tr>
</thead>
<tbody>

ADMIN_HEADER;

$i = $k = 0;
foreach ($rows as $i=>$row)
{
if ($showlinks) $title = <<<LINK_TITLE

<a href="{$this->optionurl}&amp;task=edit&amp;
id=$row->id">$row->title</a>

LINK_TITLE;

else $title = $row->title;
$html .= <<<END_OF_BODY_HTML

<tr class="row$k">
<td>
{$this->html('idBox', $i, $row->id)}
</td>
<td align="center">
$row->id
</td>
<td>
$title
</td>
<td>
$row->byline
</td>
<td align="center">
$row->hits
</td>
<td align="center">
{$this->html('publishedProcessing', $row, $i )}
</td>
</tr>
END_OF_BODY_HTML;

$i++;
$k = 1 - $k;
}
if (0 == $rowcount) $html .= <<<NO_ITEMS_HTML

<tr><td colspan="6" class="center">
{$this->T_('No items')}
</td></tr>

NO_ITEMS_HTML;

$html .= <<<END_OF_FINAL_HTML

</tbody>
</table>
{$this->pageNav->getListFooter()}

END_OF_FINAL_HTML;

return $html;
}

When it comes to adding a new item or editing an existing one, no looping is required, and the WYSIWYG editor is activated to provide a helpful interface for the administrator who is editing a text item. Note that the use of PHP heredoc allows the XHTML to be written out quite plainly, with the PHP insertions unobtrusive but effective. Actual text for translation is shown in its correct place (in the base language) by using the T_ method that is inherited from aliroBasicHTML via basicAdminHTML.

public function edit ($text)
{
$subhead = $text->id ? 'ID='.$text->id : T_('New');
$editor = aliroEditor::getInstance();
echo <<<EDIT_HTML

{$this->header($subhead)}
<div id="simpletext1">
<div>
<label for="title">{$this->T_('Title')}</label><br />
<input type="text" name="title" id="title" size="80"
value="$text->title" />
</div>
<div>
<label for="byline">{$this->T_('Byline')}</label><br />
<input type="text" name="byline" id="byline" size="80"
value="$text->byline" />
</div>
<div>
<label for="version">{$this->T_('Version')}</label><br />
<input type="text" name="version" id="version" size="80"
value="$text->version" />
</div>
<div>
<label for="article">{$this->T_('Article text')}</label><br />
{$editor->editorAreaText( 'article', $text->article,
'article', 500, 200, 80, 15 )}
</div>
</div>
<div id="simpletext2">
<fieldset>
<legend>{$this->T_('Publishing')}</legend>
<div>
<label for="published">{$this->T_('Published')}</label><br />
<input type="checkbox" name="published" id="published"
value="1" {$this->checkedIfTrue($text->published)} />
</div>
<div>
<label for="publishstart">{$this->T_('Start date')}</label><br />
<input type="text" name="publish_start" id="publishstart"
size="20" value="$text->publish_start" />
</div>
<div>
<label for="publishend">{$this->T_('End date')}</label><br />
<input type="text" name="publish_end" id="publishend"
size="20" value="$text->publish_end" />
</div>
</fieldset>
<fieldset>
<legend>{$this->T_('Metadata')}</legend>
<div>
<label for="metakey">{$this->T_('Keys')}</label><br />
<textarea name="metakey" id="metakey" rows="4"
cols="40">$text->metakey</textarea>
</div>
<div>
<label for="metadesc">{$this->T_('Description')}</label><br />
<textarea name="metadesc" id="metadesc" rows="4"
cols="40">$text->metadesc</textarea>
</div>
</fieldset>
<input type="hidden" name="task" value="" />
$this->optionline
</div>
<div id="simpletext3">
<input type="hidden" name="id" value="$text->id" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="hidemainmenu" value="0" />
</div>

EDIT_HTML;

}

Finally, there is a common method to deal with the creation of the heading. It uses the addCSS method provided by the parent class to link to a small amount of CSS that is held in a separate file. Although the list of text items defined in the XHTML above is perfectly legitimate as a table, since it really is a tabular structure, the heading would be better built out of other XHTML elements. The only reason for using a table here is that it is one of the features retained from earlier systems for the sake of backwards compatibility:

private function header ($subhead='')
{
$this->addCSS(_ALIRO_ADMIN_DIR.'/components
/com_text/admin.text.css');
if ($subhead) $subhead = "<small>[$subhead]</small>";
return <<<HEAD_HTML

<table class="adminheading">
<tr>
<th class="user">
{$this->T_('Simple Text')} $subhead
</th>
</tr>
</table>

HEAD_HTML;

}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here