PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/ContentNode.php

http://digitalus-site-manager.googlecode.com/
PHP | 118 lines | 80 code | 12 blank | 26 comment | 15 complexity | c1fbc4da953dcf1434111241419a3e1f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. class ContentNode extends DSF_Db_Table
  3. {
  4. protected $_name = "content_nodes";
  5. protected $_namespace = "page";
  6. /**
  7. * returns the selected content block
  8. *
  9. * @param int $id
  10. * @param string $node
  11. * @param string $version
  12. */
  13. public function fetchContent($id, $node, $version = null) {
  14. $where[] = $this->_db->quoteInto("parent_id = ?", $this->_namespace . '_' . $id);
  15. $where[] = $this->_db->quoteInto("node = ?", $node);
  16. if($version != null) {
  17. $where[] = $this->_db->quoteInto("version = ?", $version);
  18. }
  19. $row = $this->fetchRow($where);
  20. if($row && !empty($row->contet)) {
  21. return stripslashes($row->contet);
  22. }
  23. }
  24. /**
  25. * returns the content object for the selected page
  26. * if nodes is set then it will only return the specified nodes
  27. * otherwise it returns all
  28. *
  29. * @param int $pageId
  30. * @param array $nodes
  31. * @return object
  32. */
  33. public function fetchContentObject($pageId, $nodes = null, $namespace = null) {
  34. if(null == $namespace) {
  35. $namespace = $this->_namespace;
  36. }
  37. $data = new stdClass();
  38. $where[] = $this->_db->quoteInto("parent_id = ?", $namespace . '_' . $pageId);
  39. $rowset = $this->fetchAll($where);
  40. if($rowset->count() > 0) {
  41. foreach ($rowset as $row) {
  42. $node = $row->node;
  43. $data->$node = stripslashes($row->content);
  44. }
  45. }
  46. if(is_array($nodes)) {
  47. $return = new stdClass();
  48. foreach ($nodes as $node) {
  49. if(!empty($data->$node)) {
  50. $return->$node = $data->$node;
  51. }else{
  52. $return->$node = null;
  53. }
  54. }
  55. return $return;
  56. }else{
  57. return $data;
  58. }
  59. }
  60. public function fetchContentArray($pageId, $nodes = null, $namespace = null)
  61. {
  62. $dataArray = array();
  63. $data = $this->fetchContentObject($pageId, $nodes, $namespace);
  64. if($data) {
  65. foreach ($data as $k => $v) {
  66. $dataArray[$k] = $v;
  67. }
  68. return $dataArray;
  69. }else{
  70. return null;
  71. }
  72. }
  73. /**
  74. * this function sets a content node
  75. * if the node already exists then it updates it
  76. * if not then it inserts it
  77. *
  78. * @param int $pageId
  79. * @param string $node
  80. * @param string $content
  81. * @param string $version
  82. */
  83. public function set($pageId, $node, $content, $type = 'content', $version = null) {
  84. $node = strtolower($node);
  85. $where[] = $this->_db->quoteInto("parent_id = ?", $this->_namespace . '_' . $pageId);
  86. $where[] = $this->_db->quoteInto("node = ?", $node);
  87. if($version != null) {
  88. $where[] = $this->_db->quoteInto("version = ?", $version);
  89. }
  90. $row = $this->fetchRow($where);
  91. if($row) {
  92. $row->content = $content;
  93. $row->save();
  94. }else{
  95. $data = array(
  96. 'parent_id' => $this->_namespace . '_' . $pageId,
  97. 'node' => $node,
  98. 'content' => $content
  99. );
  100. if($version != null) {
  101. $data['version'] = $version;
  102. }
  103. $this->insert($data);
  104. }
  105. }
  106. }