-
Notifications
You must be signed in to change notification settings - Fork 8
WIP: adds datatable to index datatype. #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,17 +47,72 @@ abstract class HyphaSystemPage extends HyphaPage { | |
abstract class for a page backed by datatype and xml file. | ||
*/ | ||
abstract class HyphaDatatypePage extends HyphaPage { | ||
const INDEX_TABLE_COLUMNS_TITLE = 'index_table_column_title'; | ||
const INDEX_TABLE_COLUMNS_AUTHOR = 'index_table_column_author'; | ||
const INDEX_TABLE_COLUMNS_DATE = 'index_table_column_date'; | ||
const INDEX_TABLE_COLUMNS_STATUS = 'index_table_column_status'; | ||
|
||
public $pageListNode, $language, $pagename, $privateFlag; | ||
|
||
function __construct(HyphaDomElement $node, RequestContext $O_O) { | ||
parent::__construct($O_O); | ||
$this->replacePageListNode($node); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getDatatypeName() { | ||
return str_replace('_', ' ', get_called_class()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getIndexTableColumns() { | ||
return [ | ||
__(self::INDEX_TABLE_COLUMNS_TITLE), | ||
__(self::INDEX_TABLE_COLUMNS_AUTHOR), | ||
__(self::INDEX_TABLE_COLUMNS_DATE), | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getIndexData() { | ||
$id = $this->pageListNode->getAttribute('id'); | ||
$date = $this->getSortDateTime(); | ||
$data = [ | ||
__(self::INDEX_TABLE_COLUMNS_TITLE) => [ | ||
'class' => 'type_'.get_class($this).' '.($this->privateFlag ? 'is-private' : 'is-public'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These things (datatype and private flag) seem to be unrelated to the title, so I wonder if these are appropriate here? Conceptually, they would be a column of their own, but you probably do not want to display them as such. Maybe these would be more appropriate as classes on the |
||
'sort' => preg_replace("/[^A-Za-z0-9]/", '', $this->getTitle()).'_'.$id, | ||
'value' => '<a href="'.$this->language.'/'.$this->pagename.'">'.$this->getTitle().'</a>', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
], | ||
__(self::INDEX_TABLE_COLUMNS_AUTHOR) => $this->getAuthor(), | ||
__(self::INDEX_TABLE_COLUMNS_DATE) => [ | ||
'sort' => $date ? $date->format('YmdHis') : '', | ||
'value' => $date ? $date->format('Y-m-d') : '', | ||
], | ||
]; | ||
return $this->filterIndexData($data); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
protected function filterIndexData(array $data) { | ||
$availableColumns = self::getIndexTableColumns(); | ||
foreach ($data as $column => $dataPerColumn) { | ||
if (!in_array($column, $availableColumns)) { | ||
unset($data[$column]); | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
protected function deletePage() { | ||
global $hyphaXml, $hyphaUser; | ||
$id = $this->pageListNode->getAttribute('id'); | ||
|
@@ -92,6 +147,32 @@ public function getTitle() { | |
return showPagename($this->pagename); | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getAuthor() { | ||
$v = $this->getLatestVersion(); | ||
return $v ? $v->getAttribute('author') : null; | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getLatestVersion() { | ||
if ($this->xml->hasVersions() && $this->xml->getElementById($this->language)) { | ||
$history = []; | ||
foreach($this->xml->getElementById($this->language)->getElementsByTagName('version') as $v) { | ||
$timestamp = $v->getAttribute('xml:id'); | ||
$history[$timestamp] = $v; | ||
} | ||
if (!empty($history)) { | ||
krsort($history); | ||
return reset($history); | ||
} | ||
laurensmartina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return null; | ||
} | ||
|
||
public function renderSingleLine(HyphaDomElement $container) { | ||
$h2 = $container->document()->createElement('h2'); | ||
$h2->setText($this->getTitle()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,42 +39,116 @@ public function pageIndexView(HyphaRequest $request) { | |
$languageName = $isoLangList[$language]; | ||
$languageName = substr($languageName, 0, strpos($languageName, ' (')); | ||
|
||
$this->html->linkScript('//cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js'); | ||
$this->html->linkStyle('//cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css'); | ||
|
||
laurensmartina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$html = <<<TABLE | ||
<table class="page_index"> | ||
<thead> | ||
<tr> | ||
[[headerItems]] | ||
</tr> | ||
</thead> | ||
<tbody> | ||
[[tbody]] | ||
</tbody> | ||
</table> | ||
TABLE; | ||
|
||
$tRowTpl = <<<ROW | ||
<tr> | ||
[[recordItems]] | ||
</tr> | ||
ROW; | ||
$tRowItemTpl = <<<ROWITEM | ||
<td class="index-item [[name]] [[class]]" data-sort="[[sort]]">[[value]]</td> | ||
ROWITEM; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I like using these HTML templates like this. They are easy to read, but building HTML from text also seems prone to typos, encoding errors, etc. For forms, we're using this approach because those are complex and very diverse, but here the structure is very repetitive and simple, which means it could be feasible to just build up the DOM in memory like we do elsewhere. This has the advantage that all encoding is done automatically, you can't break the DOM with a typo, you can easily make changes to earlier sections of the DOM, etc. |
||
|
||
$dataTypes = hypha_getDataTypes(); | ||
|
||
// get list of available pages and sort alphabetically | ||
$pageList = []; | ||
foreach(hypha_getPageList() as $page) { | ||
foreach(hypha_getPageList() as $index => $page) { | ||
$lang = hypha_pageGetLanguage($page, $language); | ||
if ($lang) if ($this->O_O->isUser() || ($page->getAttribute('private')!='on')) { | ||
$pageList[] = $lang->getAttribute('name').($page->getAttribute('private')=='on' ? '&#;' : ''); | ||
$pageListDatatype[$lang->getAttribute('name')] = $page->getAttribute('type'); | ||
$isPrivateOrUser = $this->O_O->isUser() || ($page->getAttribute('private') != 'on'); | ||
laurensmartina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$isInDataType = array_key_exists($page->getAttribute('type'), $dataTypes); | ||
laurensmartina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ($lang && $isPrivateOrUser && $isInDataType) { | ||
/** @var HyphaDatatypePage $hyphaPage */ | ||
$hyphaPage = createPageInstance($this->O_O, $page); | ||
$titleSort = preg_replace("/[^A-Za-z0-9]/", '', $hyphaPage->getTitle()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This duplicates the sort return in the column data, could the default sorting maybe reuse that (i.e. by using a user-defined sorting function after collecting all index data that looks at |
||
$titleSort .= '_'.$index; | ||
$pageList[$titleSort] = $hyphaPage; | ||
} | ||
} | ||
if ($pageList) array_multisort(array_map('strtolower', $pageList), $pageList); | ||
|
||
// add capitals | ||
$capital = 'A'; | ||
$first = true; | ||
$htmlList = []; | ||
if ($pageList) foreach($pageList as $pagename) { | ||
while($capital < strtoupper($pagename[0])) $capital++; | ||
if (strtoupper($pagename[0]) == $capital) { | ||
if (!$first) { | ||
$htmlList[] = '</div>'; | ||
ksort($pageList); | ||
|
||
$columns = []; | ||
foreach ($dataTypes as $class => $name) { | ||
$cols = call_user_func($class . '::getIndexTableColumns'); | ||
$columns += array_combine($cols, $cols); | ||
laurensmartina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$tbody = ''; | ||
// iterate over page list | ||
foreach($pageList as $titleSort => $hyphaPage) { | ||
$recordItems = ''; | ||
$items = $hyphaPage->getIndexData(); | ||
foreach ($columns as $name) { | ||
if (array_key_exists($name, $items)) { | ||
$item = $items[$name]; | ||
} else { | ||
$item = ''; | ||
} | ||
if (!is_array($item)) { | ||
$item = ['value' => $item]; | ||
} | ||
|
||
$vars = ['name' => $name]; | ||
foreach (['value', 'sort', 'class'] as $key) { | ||
if (array_key_exists($key, $item)) { | ||
$vars[$key] = $item[$key]; | ||
} | ||
} | ||
$htmlList[] = '<div class="letter-wrapper">'; | ||
$htmlList[] = '<div class="letter">'.$capital.'</div>'; | ||
$capital++; | ||
$first = false; | ||
$recordItems .= hypha_substitute($tRowItemTpl, $vars); | ||
} | ||
$privatePos = strpos($pagename, '&#;'); | ||
if ($privatePos) $pagename = substr($pagename, 0, $privatePos); | ||
$htmlList[] = '<div class="index-item type_'.$pageListDatatype[$pagename].' '.($privatePos ? 'is-private' : 'is-public').'"><a href="'.$language.'/'.$pagename.'">'.showPagename($pagename).'</a></div>'; | ||
|
||
$vars = [ | ||
'recordItems' => $recordItems, | ||
]; | ||
|
||
$tbody .= hypha_substitute($tRowTpl, $vars); | ||
} | ||
|
||
$headerItems = ''; | ||
foreach ($columns as $column) { | ||
$headerItems .= '<th>'.$column.'</th>'; | ||
laurensmartina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$html = '<div class="index">'; | ||
foreach($htmlList as $htmlLine) $html.= $htmlLine; | ||
$html.= '</div>'; | ||
$vars = [ | ||
'headerItems' => $headerItems, | ||
'tbody' => $tbody, | ||
]; | ||
|
||
$html = hypha_substitute($html, $vars); | ||
|
||
// initialize datatable | ||
$js = <<<EOD | ||
$(document).ready(function () { | ||
$('.page_index').DataTable({ | ||
order: [[ [[sortIndex]], "desc" ]], | ||
paging: false, | ||
}); | ||
}); | ||
EOD; | ||
|
||
$dateIndex = array_search(__(HyphaDatatypePage::INDEX_TABLE_COLUMNS_DATE), array_values($columns)); | ||
$vars = [ | ||
'sortIndex' => $dateIndex, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this sortIndex mean exactly? Is this the default sorting (if so, then why sort the HTML at all)? Or the only option for interactive sorting? This could probably use a comment. |
||
]; | ||
$js = hypha_substitute($js, $vars); | ||
$this->html->writeScript($js); | ||
|
||
$this->html->writeToElement('pagename', __('page-index').': '.$languageName); | ||
$this->html->writeToElement('main', $html); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if using the translated titles as array indices here is the best approach? I would tend to use some kind of (untranslated) name as the index, and then put the translated title in a field next to class/sort/value?