SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/ColModifierLookupTable.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. /**
  17. * Returns data from a database table.
  18. *
  19. * This is basically than ColModifierLookupArray(Pdb::lookup),
  20. * but it doesn't call the Pdb::lookup until right before it's needed,
  21. * so if the itemlist doesn't get used, we aren't wasting a db call.
  22. **/
  23. class ColModifierLookupTable extends ColModifier
  24. {
  25. private $data;
  26. private $table;
  27.  
  28. public function __construct($table)
  29. {
  30. $this->table = $table;
  31. }
  32.  
  33. /**
  34.   * Modify a column value
  35.   * This value will be html/csv/etc encoded afterwards.
  36.   *
  37.   * @param string $val The incoming value
  38.   * @param string $field_name The name of the field being modified
  39.   * @return string The modified value
  40.   **/
  41. public function modify($val, $field_name)
  42. {
  43. if ($val == '') return '';
  44.  
  45. if (! $this->data) {
  46. $this->data = Pdb::lookup($this->table);
  47. }
  48.  
  49. return @$this->data[$val];
  50. }
  51.  
  52. }
  53.