/admin/managers/adm_content/models/ContentsModel.php

https://github.com/Xirt/XirtCMS · PHP · 106 lines · 46 code · 22 blank · 38 comment · 1 complexity · 42277d9f6b9c314ee4481fc33a85c876 MD5 · raw file

  1. <?php
  2. /**
  3. * Model for XirtCMS Contents
  4. *
  5. * @author A.G. Gideonse
  6. * @version 2.0
  7. * @copyright XirtCMS 2010 - 2014
  8. * @package XirtCMS
  9. */
  10. class ContentsModel extends XContentList {
  11. /**
  12. * The name of the table containing the information
  13. * @var String
  14. */
  15. protected $_table = "#__content";
  16. /**
  17. * The ordering column of the list (for database loading)
  18. * @var String
  19. */
  20. protected $_column = "title";
  21. /**
  22. * The list of columns used for every item
  23. * @var Array
  24. */
  25. protected $_columns = array("xid", "title");
  26. /**
  27. * Initializes Model with requested values
  28. */
  29. function __construct() {
  30. $this->setStart(XTools::getParam("start", 0, _INT));
  31. $this->setLimit(XTools::getParam("limit", 999, _INT));
  32. $this->setOrder(XTools::getParam("order", "DESC", _STRING));
  33. $this->setColumn(XTools::getParam("column", "id", _STRING));
  34. $this->setFilter(XTools::getParam("filter", "", _STRING));
  35. }
  36. /**
  37. * Loads list information from the database
  38. *
  39. * @param $iso The language to load (e.g. "en-GB")
  40. * @return boolean True on succes, false on failure
  41. */
  42. public function load($iso = null) {
  43. return ($this->_table ? !$this->_load($iso) : false);
  44. }
  45. /**
  46. * Loads all content items from the database and adds them to the list
  47. *
  48. * @param $iso The language to load (e.g. "en-GB")
  49. */
  50. protected function _load($iso = null) {
  51. global $xDb;
  52. $searchFilter = array();
  53. foreach ($this->_columns as $column) {
  54. $searchFilter[] = sprintf(" %s LIKE '%%%%%s%%%%'", $column, $this->_filter);
  55. }
  56. $languages = (new LanguageList)->toArray();
  57. $iso = array_key_exists($iso, $languages) ? $iso : XConfig::get("SESSION_LANGUAGE");
  58. $iso = intval($languages[$iso]->preference);
  59. // Query (selection)
  60. $query = "SELECT id, xid, category, title, language, " .
  61. " author_name, published, translation " .
  62. "FROM (%%s) AS subset " .
  63. "GROUP BY xid " .
  64. "HAVING %s " .
  65. "ORDER BY %s %s ";
  66. $query = sprintf($query, implode(" OR ", $searchFilter), $this->_column, $this->_order);
  67. // Subquery (translations)
  68. $trans = "SELECT t1.*, replace(t2.preference, :iso, 0) as translation " .
  69. "FROM %s AS t1 " .
  70. "INNER JOIN #__languages AS t2 " .
  71. "ON t1.language = t2.iso " .
  72. "ORDER BY replace(t2.preference, :iso, 0) ";
  73. $trans = sprintf($trans, $this->_table);
  74. // Retrieve data
  75. $stmt = $xDb->prepare(sprintf($query, $trans));
  76. $stmt->bindParam(":iso", $iso, PDO::PARAM_STR);
  77. $stmt->execute();
  78. // Populate instance
  79. while ($dbRow = $stmt->fetchObject()) {
  80. $this->_add(new XItemModel($dbRow), false);
  81. }
  82. }
  83. }
  84. ?>