Newer
Older
if ($text instanceof LazyDisplayWrapper) {
$text = $text->display($styles);
}
// Truncate
$text = $this->applyTruncate($text, $row);
foreach ($this->getAnnotations() as $D) {
$text = $D->render($row, $text);
}
foreach ($this->getConditions() as $C) {
$text = $C->render($row, $text, $styles);
$style = Format::array_implode(':', ';', $styles);
return array($text, $style);
}
function renderBasicValue($row) {
$root = ($q = $this->getQueue()) ? $q->getRoot() : 'Ticket';
$fields = CustomQueue::getSearchableFields($root);
$primary = CustomQueue::getOrmPath($this->primary);
$secondary = CustomQueue::getOrmPath($this->secondary);
// Return a lazily ::display()ed value so that the value to be
// rendered by the field could be changed or display()ed when
// converted to a string.
if (($F = $fields[$primary])
&& (list(,$field) = $F)
&& ($T = $field->from_query($row, $primary))
) {
return new LazyDisplayWrapper($field, $T);
}
if (($F = $fields[$secondary])
&& (list(,$field) = $F)
&& ($T = $field->from_query($row, $secondary))
return new LazyDisplayWrapper($field, $T);
return new LazyDisplayWrapper($field, '');
function applyTruncate($text, $row) {
$offset = 0;
foreach ($this->getAnnotations() as $a)
$offset += $a->getWidth($row);
$width = $this->width - $offset;
$class = array();
case 'lclip':
$linfo = Internationalization::getCurrentLanguageInfo();
// Use `rtl` class to cut the beginning of LTR text. But, wrap
// the text with an appropriate direction so the ending
// punctuation is not rearranged.
$dir = $linfo['direction'] ?: 'ltr';
$text = sprintf('<span class="%s">%s</span>', $dir, $text);
$class[] = $dir == 'rtl' ? 'ltr' : 'rtl';
$class[] = 'bleed';
case 'ellipsis':
$class[] = 'truncate';
return sprintf('<span class="%s" style="max-width:%dpx">%s</span>',
implode(' ', $class), $width, $text);
function addToQuery($query, $field, $path) {
if (preg_match('/__answers!\d+__/', $path)) {
// Ensure that only one record is returned from the join through
// the entry and answers joins
return $query->annotate(array(
$path => SqlAggregate::MAX($path)
));
}
return $field->addToQuery($query, $path);
}
function mangleQuery($query, $root=null) {
$fields = CustomQueue::getSearchableFields($root ?: $this->getQueue()->getRoot());
if ($primary = $fields[$this->primary]) {
list(,$field) = $primary;
$query = $this->addToQuery($query, $field,
CustomQueue::getOrmPath($this->primary, $query));
}
if ($secondary = $fields[$this->secondary]) {
list(,$field) = $secondary;
$query = $this->addToQuery($query, $field,
CustomQueue::getOrmPath($this->secondary, $query));
if ($filter = $this->getFilter())
$query = $filter->mangleQuery($query, $this);
foreach ($this->getAnnotations() as $D) {
$query = $D->annotate($query);
}
// Conditions
foreach ($this->getConditions() as $C) {
$query = $C->annotate($query);
}
return $query;
}
function applySort($query, $reverse=false) {
$root = ($q = $this->getQueue()) ? $q->getRoot() : 'Ticket';
$fields = CustomQueue::getSearchableFields($root);
if ($primary = $fields[$this->primary]) {
list(,$field) = $primary;
$keys[] = array(CustomQueue::getOrmPath($this->primary, $query),
$field);
if ($secondary = $fields[$this->secondary]) {
list(,$field) = $secondary;
$keys[] = array(CustomQueue::getOrmPath($this->secondary,
$query), $field);
}
if (count($keys) > 1) {
$fields = array();
foreach ($keys as $key) {
list($path, $field) = $key;
foreach ($field->getSortKeys($path) as $field)
$fields[] = new SqlField($field);
// Force nulls to the buttom.
$fields[] = 'zzz';
$alias = sprintf('C%d', $this->getId());
$expr = call_user_func_array(array('SqlFunction', 'COALESCE'),
$fields);
$query->annotate(array($alias => $expr));
$reverse = $reverse ? '-' : '';
$query = $query->order_by("{$reverse}{$alias}");
} else {
list($path, $field) = $keys[0];
$query = $field->applyOrderBy($query, $reverse, $path);
function getDataConfigForm($source=false) {
return new QueueColDataConfigForm($source ?: $this->getDbFields(),
array('id' => $this->id));
}
if (!isset($this->_annotations)) {
$this->_annotations = array();
if ($this->annotations
&& ($anns = JsonDataParser::decode($this->annotations))
) {
foreach ($anns as $D)
if ($T = QueueColumnAnnotation::fromJson($D))
$this->_annotations[] = $T;
}
}
function getConditions($include_queue=true) {
if (!isset($this->_conditions)) {
$this->_conditions = array();
if ($this->conditions
&& ($conds = JsonDataParser::decode($this->conditions))
) {
foreach ($conds as $C)
if ($T = QueueColumnCondition::fromJson($C))
$this->_conditions[] = $T;
}
// Support row-spanning conditions
if ($include_queue && ($q = $this->getQueue())
&& ($q_conds = $q->getConditions())
) {
$this->_conditions = array_merge($this->_conditions, $q_conds);
}
return $this->_conditions;
}
$c = new static($vars);
$c->save();
return $c;
}
static function placeholder($vars) {
return static::__hydrate($vars);
}
function update($vars, $root='Ticket') {
$form = $this->getDataConfigForm($vars);
foreach ($form->getClean() as $k=>$v)
$this->set($k, $v);
// Do the annotations
$this->_annotations = $annotations = array();
if (isset($vars['annotations'])) {
foreach (@$vars['annotations'] as $i=>$class) {
if ($vars['deco_column'][$i] != $this->id)
continue;
if (!class_exists($class) || !is_subclass_of($class, 'QueueColumnAnnotation'))
continue;
$json = array('c' => $class, 'p' => $vars['deco_pos'][$i]);
$annotations[] = $json;
$this->_annotations[] = QueueColumnAnnotation::fromJson($json);
}
$this->_conditions = $conditions = array();
if (isset($vars['conditions'])) {
list($this->_conditions, $conditions)
= self::getConditionsFromPost($vars, $this->id, $root);
$this->annotations = JsonDataEncoder::encode($annotations);
$this->conditions = JsonDataEncoder::encode($conditions);
}
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
static function getConditionsFromPost(array $vars, $myid, $root='Ticket') {
$condition_objects = $conditions = array();
if (!isset($vars['conditions']))
return array($condition_objects, $conditions);
foreach (@$vars['conditions'] as $i=>$id) {
if ($vars['condition_column'][$i] != $myid)
// Not a condition for this column
continue;
// Determine the criteria
$name = $vars['condition_field'][$i];
$fields = CustomQueue::getSearchableFields($root);
if (!isset($fields[$name]))
// No such field exists for this queue root type
continue;
$parts = CustomQueue::getSearchField($fields[$name], $name);
$search_form = new SimpleForm($parts, $vars, array('id' => $id));
$search_form->getField("{$name}+search")->value = true;
$crit = $search_form->getClean();
// Check the box to enable searching on the field
$crit["{$name}+search"] = true;
// Isolate only the critical parts of the criteria
$crit = QueueColumnCondition::isolateCriteria($crit);
// Determine the properties
$props = array();
foreach ($vars['properties'] as $i=>$cid) {
if ($cid != $id)
// Not a property for this condition
continue;
// Determine the property configuration
$prop = $vars['property_name'][$i];
if (!($F = QueueColumnConditionProperty::getField($prop))) {
// Not a valid property
continue;
}
$prop_form = new SimpleForm(array($F), $vars, array('id' => $cid));
$props[$prop] = $prop_form->getField($prop)->getClean();
}
$json = array('crit' => $crit, 'prop' => $props);
$condition_objects[] = QueueColumnCondition::fromJson($json);
$conditions[] = $json;
}
return array($condition_objects, $conditions);
}
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
class QueueConfig
extends VerySimpleModel {
static $meta = array(
'table' => QUEUE_CONFIG_TABLE,
'pk' => array('queue_id', 'staff_id'),
'joins' => array(
'queue' => array(
'constraint' => array(
'queue_id' => 'CustomQueue.id'),
),
'staff' => array(
'constraint' => array(
'staff_id' => 'Staff.staff_id',
)
),
'columns' => array(
'reverse' => 'QueueColumnGlue.config',
'constrain' => array('staff_id' =>'QueueColumnGlue.staff_id'),
'broker' => 'QueueColumnListBroker',
),
),
);
function getSettings() {
return JsonDataParser::decode($this->setting);
}
function update($vars, &$errors) {
// settings of interest
$setting = array(
'sort_id' => (int) $vars['sort_id'],
'filter' => $vars['filter'],
'inherit-columns' => isset($vars['inherit-columns']),
'criteria' => $vars['criteria'] ?: array(),
);
if (!$setting['inherit-columns'] && $vars['columns']) {
if (!$this->columns->update($vars['columns'], $errors, array(
'queue_id' => $this->queue_id,
'staff_id' => $this->staff_id)))
$setting['inherit-columns'] = true;
$this->columns->reset();
}
$this->setting = JsonDataEncoder::encode($setting);
return $this->save();
}
function save($refetch=false) {
if ($this->dirty)
$this->updated = SqlFunction::NOW();
return parent::save($refetch || $this->dirty);
}
static function create($vars=false) {
$inst = new static($vars);
return $inst;
}
}
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
class QueueExport
extends VerySimpleModel {
static $meta = array(
'table' => QUEUE_EXPORT_TABLE,
'pk' => array('id'),
'joins' => array(
'queue' => array(
'constraint' => array('queue_id' => 'CustomQueue.id'),
),
),
'select_related' => array('queue'),
'ordering' => array('sort'),
);
function getPath() {
return $this->path;
}
function getField() {
return $this->getPath();
}
function getHeading() {
return $this->heading;
}
static function create($vars=false) {
$inst = new static($vars);
return $inst;
}
}
class QueueColumnGlue
extends VerySimpleModel {
static $meta = array(
'table' => QUEUE_COLUMN_TABLE,
'pk' => array('queue_id', 'staff_id', 'column_id'),
'joins' => array(
'column' => array(
'constraint' => array('column_id' => 'QueueColumn.id'),
),
'queue' => array(
'constraint' => array(
'queue_id' => 'CustomQueue.id',
'staff_id' => 'CustomQueue.staff_id'),
),
'config' => array(
'constraint' => array(
'queue_id' => 'QueueConfig.queue_id',
'staff_id' => 'QueueConfig.staff_id'),
'select_related' => array('column'),
'ordering' => array('sort'),
);
}
class QueueColumnGlueMIM
extends ModelInstanceManager {
function getOrBuild($modelClass, $fields, $cache=true) {
$m = parent::getOrBuild($modelClass, $fields, $cache);
if ($m && $modelClass === 'QueueColumnGlue') {
// Instead, yield the QueueColumn instance with the local fields
// in the association table as annotations
$m = AnnotatedModel::wrap($m->column, $m, 'QueueColumn');
}
return $m;
}
}
class QueueColumnListBroker
extends InstrumentedList {
function __construct($fkey, $queryset=false) {
parent::__construct($fkey, $queryset, 'QueueColumnGlueMIM');
$this->queryset->select_related('column');
}
function add($column, $glue=null, $php7_is_annoying=true) {
$glue = $glue ?: new QueueColumnGlue();
$anno = AnnotatedModel::wrap($column, $glue);
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
function update($columns, &$errors, $options=array()) {
$new = $columns;
$order = array_keys($new);
foreach ($this as $col) {
$key = $col->column_id;
if (!isset($columns[$key])) {
$this->remove($col);
continue;
}
$info = $columns[$key];
$col->set('sort', array_search($key, $order));
$col->set('heading', $info['heading']);
$col->set('width', $info['width']);
$col->setSortable($info['sortable']);
unset($new[$key]);
}
// Add new columns
foreach ($new as $info) {
$glue = new QueueColumnGlue(array(
'staff_id' => $options['staff_id'] ?: 0 ,
'queue_id' => $options['queue_id'] ?: 0,
'column_id' => $info['column_id'],
'sort' => array_search($info['column_id'], $order),
'heading' => $info['heading'],
'width' => $info['width'] ?: 100,
'bits' => $info['sortable'] ? QueueColumn::FLAG_SORTABLE : 0,
));
$this->add(QueueColumn::lookup($info['column_id']), $glue);
}
// Re-sort the in-memory columns array
$this->sort(function($c) { return $c->sort; });
return $this->saveAll();
}
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
class QueueSort
extends VerySimpleModel {
static $meta = array(
'table' => QUEUE_SORT_TABLE,
'pk' => array('id'),
'ordering' => array('name'),
'joins' => array(
'queue' => array(
'constraint' => array('queue_id' => 'CustomQueue.id'),
),
),
);
var $_columns;
function getRoot($hint=false) {
switch ($hint ?: $this->root) {
case 'T':
default:
return 'Ticket';
}
}
function getName() {
return $this->name;
}
function getId() {
return $this->id;
}
function applySort(QuerySet $query, $reverse=false, $root=false) {
$fields = CustomQueue::getSearchableFields($this->getRoot($root));
foreach ($this->getColumnPaths() as $path=>$descending) {
$descending = $reverse ? !$descending : $descending;
if (isset($fields[$path])) {
list(,$field) = $fields[$path];
$query = $field->applyOrderBy($query, $descending,
CustomQueue::getOrmPath($path, $query));
}
}
return $query;
}
function getColumnPaths() {
if (!isset($this->_columns)) {
$columns = array();
foreach (JsonDataParser::decode($this->columns) as $path) {
if ($descending = $path[0] == '-')
$path = substr($path, 1);
$columns[$path] = $descending;
}
$this->_columns = $columns;
}
return $this->_columns;
}
function getColumns() {
$columns = array();
$paths = $this->getColumnPaths();
$everything = CustomQueue::getSearchableFields($this->getRoot());
foreach ($paths as $p=>$descending) {
if (isset($everything[$p])) {
$columns[$p] = array($everything[$p], $descending);
}
}
return $columns;
}
function getDataConfigForm($source=false) {
return new QueueSortDataConfigForm($source ?: $this->getDbFields(),
array('id' => $this->id));
}
static function forQueue(CustomQueue $queue) {
return static::objects()->filter([
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
]);
}
function save($refetch=false) {
if ($this->dirty)
$this->updated = SqlFunction::NOW();
return parent::save($refetch || $this->dirty);
}
function update($vars, &$errors=array()) {
if (!isset($vars['name']))
$errors['name'] = __('A title is required');
$this->name = $vars['name'];
if (isset($vars['root']))
$this->root = $vars['root'];
elseif (!isset($this->root))
$this->root = 'T';
$fields = CustomQueue::getSearchableFields($this->getRoot($vars['root']));
$columns = array();
if (@is_array($vars['columns'])) {
foreach ($vars['columns']as $path=>$info) {
$descending = (int) @$info['descending'];
// TODO: Check if column is valid, stash in $columns
if (!isset($fields[$path]))
continue;
$columns[] = ($descending ? '-' : '') . $path;
}
$this->columns = JsonDataEncoder::encode($columns);
}
if (count($errors))
return false;
return $this->save();
}
static function __create($vars) {
$c = new static($vars);
$c->save();
return $c;
}
}
class QueueSortGlue
extends VerySimpleModel {
static $meta = array(
'table' => QUEUE_SORTING_TABLE,
'pk' => array('sort_id', 'queue_id'),
'joins' => array(
'ordering' => array(
'constraint' => array('sort_id' => 'QueueSort.id'),
),
'queue' => array(
'constraint' => array('queue_id' => 'CustomQueue.id'),
),
),
'select_related' => array('ordering', 'queue'),
'ordering' => array('sort'),
);
}
class QueueSortGlueMIM
extends ModelInstanceManager {
function getOrBuild($modelClass, $fields, $cache=true) {
$m = parent::getOrBuild($modelClass, $fields, $cache);
if ($m && $modelClass === 'QueueSortGlue') {
// Instead, yield the QueueColumn instance with the local fields
// in the association table as annotations
$m = AnnotatedModel::wrap($m->ordering, $m, 'QueueSort');
}
return $m;
}
}
class QueueSortListBroker
extends InstrumentedList {
function __construct($fkey, $queryset=false) {
parent::__construct($fkey, $queryset, 'QueueSortGlueMIM');
$this->queryset->select_related('ordering');
}
function add($ordering, $glue=null, $php7_is_annoying=true) {
$glue = $glue ?: new QueueSortGlue();
$glue->ordering = $ordering;
$anno = AnnotatedModel::wrap($ordering, $glue);
parent::add($anno, false);
return $anno;
}
}
abstract class QueueColumnFilter {
static $registry;
static $id = null;
static $desc = null;
static function register($filter, $group) {
if (!isset($filter::$id))
throw new Exception('QueueColumnFilter must define $id');
if (isset(static::$registry[$filter::$id]))
throw new Exception($filter::$id
. ': QueueColumnFilter already registered under that id');
if (!is_subclass_of($filter, get_called_class()))
throw new Exception('Filter must extend QueueColumnFilter');
static::$registry[$filter::$id] = array($group, $filter);
}
static function getFilters() {
$list = static::$registry;
$base = array();
foreach ($list as $id=>$stuff) {
list($group, $class) = $stuff;
$base[$group][$id] = __($class::$desc);
}
return $base;
}
static function getInstance($id) {
if (isset(static::$registry[$id])) {
list(, $class) = @static::$registry[$id];
if ($class && class_exists($class))
return new $class();
}
function mangleQuery($query, $column) { return $query; }
abstract function filter($value, $row);
}
class TicketLinkFilter
extends QueueColumnFilter {
static $id = 'link:ticket';
static $desc = /* @trans */ "Ticket Link";
function filter($text, $row) {
if ($link = $this->getLink($row))
return sprintf('<a style="display:inline" href="%s">%s</a>', $link, $text);
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
}
function mangleQuery($query, $column) {
static $fields = array(
'link:ticket' => 'ticket_id',
'link:ticketP' => 'ticket_id',
'link:user' => 'user_id',
'link:org' => 'user__org_id',
);
if (isset($fields[static::$id])) {
$query = $query->values($fields[static::$id]);
}
return $query;
}
function getLink($row) {
return Ticket::getLink($row['ticket_id']);
}
}
class UserLinkFilter
extends TicketLinkFilter {
static $id = 'link:user';
static $desc = /* @trans */ "User Link";
function getLink($row) {
return User::getLink($row['user_id']);
}
}
class OrgLinkFilter
extends TicketLinkFilter {
static $id = 'link:org';
static $desc = /* @trans */ "Organization Link";
function getLink($row) {
return Organization::getLink($row['user__org_id']);
QueueColumnFilter::register('TicketLinkFilter', __('Link'));
QueueColumnFilter::register('UserLinkFilter', __('Link'));
QueueColumnFilter::register('OrgLinkFilter', __('Link'));
class TicketLinkWithPreviewFilter
extends TicketLinkFilter {
static $id = 'link:ticketP';
static $desc = /* @trans */ "Ticket Link with Preview";
function filter($text, $row) {
$link = $this->getLink($row);
return sprintf('<a style="display: inline" class="preview" data-preview="#tickets/%d/preview" href="%s">%s</a>',
$row['ticket_id'], $link, $text);
}
}
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
QueueColumnFilter::register('TicketLinkWithPreviewFilter', __('Link'));
class DateTimeFilter
extends QueueColumnFilter {
static $id = 'date:full';
static $desc = /* @trans */ "Date and Time";
function filter($text, $row) {
return $text->changeTo(Format::datetime($text->value));
}
}
class HumanizedDateFilter
extends QueueColumnFilter {
static $id = 'date:human';
static $desc = /* @trans */ "Relative Date and Time";
function filter($text, $row) {
return sprintf(
'<time class="relative" datetime="%s" title="%s">%s</time>',
date(DateTime::W3C, Misc::db2gmtime($text->value)),
Format::daydatetime($text->value),
Format::relativeTime(Misc::db2gmtime($text->value))
);
}
}
QueueColumnFilter::register('DateTimeFilter', __('Date Format'));
QueueColumnFilter::register('HumanizedDateFilter', __('Date Format'));
class QueueColDataConfigForm
extends AbstractForm {
function buildFields() {
return array(
'primary' => new DataSourceField(array(
'label' => __('Primary Data Source'),
'configuration' => array(
'root' => 'Ticket',
),
'layout' => new GridFluidCell(6),
)),
'secondary' => new DataSourceField(array(
'label' => __('Secondary Data Source'),
'configuration' => array(
'root' => 'Ticket',
),
'layout' => new GridFluidCell(6),
)),
'name' => new TextboxField(array(
'label' => __('Name'),
'required' => true,
'layout' => new GridFluidCell(4),
'filter' => new ChoiceField(array(
'label' => __('Filter'),
'choices' => QueueColumnFilter::getFilters(),
)),
'truncate' => new ChoiceField(array(
'label' => __('Text Overflow'),
'choices' => array(
'wrap' => __("Wrap Lines"),
'ellipsis' => __("Add Ellipsis"),
'clip' => __("Clip Text"),
'lclip' => __("Clip Beginning Text"),
),
'default' => 'wrap',
class QueueSortDataConfigForm
extends AbstractForm {
function getInstructions() {
return __('Add, and remove the fields in this list using the options below. Sorting can be performed on any field, whether displayed in the queue or not.');
}
function buildFields() {
return array(
'name' => new TextboxField(array(
'required' => true,
'layout' => new GridFluidCell(12),
'translatable' => isset($this->options['id'])
? _H('queuesort.name.'.$this->options['id']) : false,
'configuration' => array(
'placeholder' => __('Sort Criteria Title'),
),
)),
);
}
}