Category.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ K9PfLDm6IYP5RLNLNc//Ik1uYkiidL77Wm6NaMualOA=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.5
  11. */
  12. /**
  13. * category selection validator
  14. * only leaf nodes are valid selections
  15. */
  16. namespace Ppb\Validate\Db;
  17. use Cube\Validate\Db\RecordExists,
  18. Ppb\Db\Table\Categories as CategoriesTable;
  19. class Category extends RecordExists
  20. {
  21. protected $_message = "'%s': the value '%value%' is invalid.";
  22. /**
  23. *
  24. * class constructor
  25. *
  26. * add the table and the field to compare against
  27. *
  28. * @param array $data
  29. * Supported keys:
  30. * 'table' -> table - fully qualified namespace;
  31. * 'field' -> table field
  32. * 'exclude' -> where clause or field/value pair to exclude from the query
  33. */
  34. public function __construct(array $data = null)
  35. {
  36. if (!array_key_exists('table', $data)) {
  37. $data['table'] = new CategoriesTable();
  38. }
  39. if (!array_key_exists('field', $data)) {
  40. $data['field'] = 'id';
  41. }
  42. parent::__construct($data);
  43. }
  44. /**
  45. *
  46. * get select object, construct if it wasn't set yet
  47. *
  48. * @return \Cube\Db\Select
  49. */
  50. public function getSelect()
  51. {
  52. if ($this->_select === null) {
  53. $adapter = $this->_table->getAdapter();
  54. $tableName = $this->_table->getPrefix() . $this->_table->getName();
  55. $select = $this->_table->select()
  56. ->columns("(SELECT count(*)
  57. FROM " . $adapter->quoteTableAs($tableName) . "
  58. WHERE " . $adapter->quoteIdentifier('parent_id') . " = '" . strval($this->_value) . "') AS nb_rows")
  59. ->where($adapter->quoteIdentifier($this->_field) . ' = ?', strval($this->_value))
  60. ->having('nb_rows = 0');
  61. if ($this->_exclude !== null) {
  62. if (is_array($this->_exclude)) {
  63. $select->where(
  64. $adapter->quoteIdentifier($this->_exclude['field'], true) .
  65. ' != ?', strval($this->_exclude['value'])
  66. );
  67. }
  68. else {
  69. $select->where($this->_exclude);
  70. }
  71. }
  72. $select->limit(1);
  73. $this->_select = $select;
  74. }
  75. return $this->_select;
  76. }
  77. }