Skip to content

Instantly share code, notes, and snippets.

@vgrish
Created January 9, 2016 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrish/3d0f384aadf96312cf01 to your computer and use it in GitHub Desktop.
Save vgrish/3d0f384aadf96312cf01 to your computer and use it in GitHub Desktop.
<?php
$Tickets = $modx->getService('tickets', 'Tickets', $modx->getOption('tickets.core_path', null, $modx->getOption('core_path') . 'components/tickets/') . 'model/tickets/', $scriptProperties);
$Tickets->initialize($modx->context->key, $scriptProperties);
/** @var pdoFetch $pdoFetch */
$fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
if ($pdoClass = $modx->loadClass($fqn, '', false, true)) {
$pdoFetch = new $pdoClass($modx, $scriptProperties);
} elseif ($pdoClass = $modx->loadClass($fqn, MODX_CORE_PATH . 'components/pdotools/model/', false, true)) {
$pdoFetch = new $pdoClass($modx, $scriptProperties);
} else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not load pdoFetch from "MODX_CORE_PATH/components/pdotools/model/".');
return false;
}
$pdoFetch->addTime('pdoTools loaded');
if (isset($parents) && $parents === '') {
$scriptProperties['parents'] = $modx->resource->id;
}
$class = 'Ticket';
$where = array('class_key' => $class);
if (empty($outputSeparator)) {
$outputSeparator = "\n";
}
$leftJoin = array(
'Section' => array('class' => 'TicketsSection', 'on' => '`Section`.`id` = `Ticket`.`parent`'),
'User' => array('class' => 'modUser', 'on' => '`User`.`id` = `Ticket`.`createdby`'),
'Profile' => array('class' => 'modUserProfile', 'on' => '`Profile`.`internalKey` = `User`.`id`'),
);
$select = array(
'Section' => $modx->getSelectColumns('TicketsSection', 'Section', 'section.', array('content'), true),
'User' => $modx->getSelectColumns('modUser', 'User', '', array('username')),
'Profile' => $modx->getSelectColumns('modUserProfile', 'Profile', '', array('id'), true),
'Ticket' => $modx->getSelectColumns($class, $class, '', array('content'), true),
'Symbol' => 'SUBSTRING(Ticket.pagetitle,1,1) as letter'
);
$pdoFetch->addTime('Conditions prepared');
// Add custom parameters
foreach (array('where', 'select', 'leftJoin') as $v) {
if (!empty($scriptProperties[$v])) {
$tmp = $modx->fromJSON($scriptProperties[$v]);
if (is_array($tmp)) {
$$v = array_merge($$v, $tmp);
}
}
unset($scriptProperties[$v]);
}
$default = array(
'class' => $class,
'where' => $modx->toJSON($where),
'leftJoin' => $modx->toJSON($leftJoin),
'select' => $modx->toJSON($select),
'sortby' => 'letter',
//'sortby' => 'createdon',
'sortdir' => 'ASC',
'groupby' => $class . '.id',
'return' => !empty($returnIds)
? 'ids'
: 'data',
'nestedChunkPrefix' => 'tickets_',
);
// Merge all properties and run!
$pdoFetch->setConfig(array_merge($default, $scriptProperties));
$pdoFetch->addTime('Query parameters are prepared.');
$rows = $pdoFetch->run();
if (!empty($returnIds)) {
return $rows;
}
// Processing rows
$tickets = $letters = $letterContent = $output = '';
if (!empty($rows) && is_array($rows)) {
foreach ($rows as $k => $row) {
// Handle properties
$properties = is_string($row['properties']) ? $modx->fromJSON($row['properties']) : $row['properties'];
if (!empty($properties['tickets'])) {
$properties = $properties['tickets'];
}
if (empty($properties['process_tags'])) {
foreach ($row as $field => $value) {
$row[$field] = str_replace(
array('[', ']', '`', '{', '}'),
array('&#91;', '&#93;', '&#96;', '&#123;', '&#125;'),
$value
);
}
}
if (!is_array($properties)) {
$properties = array();
}
$row['idx'] = $pdoFetch->idx++;
if (!isset($letters[$row['letter']])) {
$letters[$row['letter']] = $pdoFetch->getChunk($tplLetter, $row, $pdo->config['fastMode']);
$letterContent[$row['letter']] = $pdoFetch->getChunk($tplLetterContent, $row, $pdo->config['fastMode']);
}
$tickets[$row['letter']][] = $pdoFetch->getChunk($tplTickets, $row, $pdo->config['fastMode']);
}
foreach ($letterContent as $letter => $content) {
$output .= str_replace('[[+tickets]]', implode($outputSeparator, $tickets[$letter]), $content);
}
$output = $pdoFetch->getChunk($tplLetterContentOuter, array('output' => $output), $pdo->config['fastMode']);
$output = $pdoFetch->getChunk($tplLetterOuter, array('output' => implode($outputSeparator, $letters)), $pdo->config['fastMode']) . $outputSeparator . $output;
}
$pdoFetch->addTime('Returning processed chunks');
if (!empty($tplWrapper) && (!empty($wrapIfEmpty) || !empty($output))) {
$array = array('output' => $output);
$output = $pdoFetch->getChunk($tplWrapper, $array, $pdoFetch->config['fastMode']);
}
if (!empty($toPlaceholder)) {
$modx->setPlaceholder($toPlaceholder, $output);
} else {
return $output;
}
@vgrish
Copy link
Author

vgrish commented Jan 9, 2016

[[alphabeticallyTickets?
&parents=`144`
&tplLetter=`@INLINE
<li><a href="#letter-[[+letter]]" data-toggle="tab">[[+letter]]</a></li>
`
&tplLetterOuter=`@INLINE
<ul class="nav nav-tabs" id="alphabeticallyTickets">[[+output]]</ul>
`
&tplLetterContent=`@INLINE
<div class="tab-pane" id="letter-[[+letter]]">{{+tickets}}</div>
`
&tplLetterContentOuter=`@INLINE
<div class="tab-content">[[+output]]</div>
`

&tplTickets=`@INLINE
<a href="[[+uri]]">[[+pagetitle]]</a><br>
`
]]

@vgrish
Copy link
Author

vgrish commented Jan 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment