Created
January 9, 2016 11:43
-
-
Save vgrish/3d0f384aadf96312cf01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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('[', ']', '`', '{', '}'), | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment