Lookup.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. class Lookup extends CActiveRecord
  3. {
  4. /**
  5. * The followings are the available columns in table 'tbl_lookup':
  6. * @var integer $id
  7. * @var string $object_type
  8. * @var integer $code
  9. * @var string $name_en
  10. * @var string $name_fr
  11. * @var integer $sequence
  12. * @var integer $status
  13. */
  14. private static $_items=array();
  15. /**
  16. * Returns the static model of the specified AR class.
  17. * @return CActiveRecord the static model class
  18. */
  19. public static function model($className=__CLASS__)
  20. {
  21. return parent::model($className);
  22. }
  23. /**
  24. * @return string the associated database table name
  25. */
  26. public function tableName()
  27. {
  28. return '{{lookup}}';
  29. }
  30. /**
  31. * Returns the items for the specified type.
  32. * @param string item type (e.g. 'PostStatus').
  33. * @return array item names indexed by item code. The items are order by their position values.
  34. * An empty array is returned if the item type does not exist.
  35. */
  36. public static function items($type)
  37. {
  38. if(!isset(self::$_items[$type]))
  39. self::loadItems($type);
  40. return self::$_items[$type];
  41. }
  42. /**
  43. * Returns the item name for the specified type and code.
  44. * @param string the item type (e.g. 'PostStatus').
  45. * @param integer the item code (corresponding to the 'code' column value)
  46. * @return string the item name for the specified the code. False is returned if the item type or code does not exist.
  47. */
  48. public static function item($type,$code)
  49. {
  50. if(!isset(self::$_items[$type]))
  51. self::loadItems($type);
  52. return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false;
  53. }
  54. /**
  55. * Loads the lookup items for the specified type from the database.
  56. * @param string the item type
  57. */
  58. private static function loadItems($type)
  59. {
  60. self::$_items[$type]=array();
  61. $models=self::model()->findAll(array(
  62. 'condition'=>'type=:type',
  63. 'params'=>array(':type'=>$type),
  64. 'order'=>'position',
  65. ));
  66. foreach($models as $model)
  67. self::$_items[$type][$model->code]=$model->name;
  68. }
  69. }