/skyblue/includes/mvc/XmlDAO.php

https://github.com/iconifyit/SkyBlue-CMS · PHP · 160 lines · 120 code · 22 blank · 18 comment · 13 complexity · 029e71d7be1d3ad2880325d1e3762af0 MD5 · raw file

  1. <?php defined('SKYBLUE') or die('Bad file request');
  2. /**
  3. * @version 2.0 2009-06-22 23:50:00 $
  4. * @package SkyBlueCanvas
  5. * @copyright Copyright (C) 2005 - 2010 Scott Edwin Lewis. All rights reserved.
  6. * @license GNU/GPL, see COPYING.txt
  7. * SkyBlueCanvas is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYING.txt for copyright notices and details.
  12. */
  13. /**
  14. * @author Scott Lewis
  15. * @date June 22, 2009
  16. */
  17. class XmlDAO extends DAO {
  18. var $type;
  19. var $dataSubPath;
  20. var $source;
  21. function __construct($options) {
  22. $this->setType(Filter::getAlphaNumeric($options, 'type'));
  23. $this->setBeanClass(Filter::get($options, 'bean_class'));
  24. $this->initDataSource(
  25. Filter::get($options, 'data_sub_path'),
  26. "{$this->type}.xml"
  27. );
  28. }
  29. function getType() {
  30. return $this->type;
  31. }
  32. function setType($type) {
  33. $this->type = $type;
  34. }
  35. function setSource($source) {
  36. $this->source = SB_XML_DIR . $source;
  37. }
  38. function getSource() {
  39. return $this->source;
  40. }
  41. function index($refresh=false) {
  42. global $Core;
  43. return parent::bindTransferObjects(
  44. $Core->xmlHandler->ParserMain($this->getSource()),
  45. strtolower($this->getBeanClass())
  46. );
  47. }
  48. function insert($TransferObject) {
  49. $TransferObject->setType($this->type);
  50. $TransferObject->setObjtype($this->type);
  51. if (!is_callable(array($TransferObject, 'getValueObject'))) {
  52. trigger_error(__(
  53. 'DAO.TRANSFER_OBJECT_REQUIRED',
  54. 'DAO::insert($TransferObject) expects the first argument to be a TransferObject.'
  55. ));
  56. }
  57. $Bean = $TransferObject->getValueObject();
  58. $data = $this->index(true);
  59. $count = count($data);
  60. $inserted = false;
  61. for ($i=0; $i<$count; $i++) {
  62. $Item = $data[$i];
  63. if ($Item->getId() == $Bean->getId()) {
  64. $data[$i] = $Bean;
  65. $inserted = true;
  66. }
  67. }
  68. if (! $inserted) {
  69. array_push($data, $Bean);
  70. }
  71. return $this->save($data);
  72. }
  73. function copy($id) { /* May be defined by child class */ }
  74. /**
  75. * For the XML implementation, update is just an alias for insert.
  76. */
  77. function update($Bean) {
  78. return $this->insert($Bean);
  79. }
  80. function getItem($id) {
  81. return Utils::selectObject($this->index(), $id);
  82. }
  83. function getByKey($key, $value) {
  84. return Utils::findObjByKey($this->index(), $key, $value);
  85. }
  86. function getByName($name) {
  87. return $this->getByKey('name', $name);
  88. }
  89. function save($data=null) {
  90. return FileSystem::write_file(
  91. $this->source,
  92. ArrayUtils::objectsToXml($data, strtolower($this->getType()))
  93. );
  94. }
  95. function delete($id) {
  96. $data = $this->index(true);
  97. $filtered = array();
  98. $deleted = false;
  99. foreach ($data as $Item) {
  100. if ($Item->getId() != $id) {
  101. array_push($filtered, $Item);
  102. }
  103. else {
  104. $deleted = true;
  105. }
  106. }
  107. if ($deleted) {
  108. return $this->save($filtered);
  109. }
  110. return false;
  111. }
  112. function deleteAll() {
  113. return $this->save(array());
  114. }
  115. function exists($name) { /* To be defined by derivative class */ }
  116. function getDataSubPath() {
  117. return SB_XML_DIR . $this->dataSubPath;
  118. }
  119. function setDataSubPath($dataSubPath) {
  120. $this->dataSubPath = $dataSubPath;
  121. }
  122. function initDataSource($subpath, $fileName) {
  123. $this->setDataSubPath($subpath);
  124. $this->setSource("{$subpath}{$fileName}");
  125. if (!file_exists($this->getSource())) {
  126. $ClassName = ucwords($this->getType());
  127. if (trim($this->getDataSubPath()) != "") {
  128. if (!is_dir($this->getDataSubPath())) {
  129. FileSystem::make_dir($this->getDataSubPath());
  130. }
  131. }
  132. FileSystem::write_file(
  133. $this->getSource(),
  134. ArrayUtils::objectsToXml(array(), strtolower($ClassName))
  135. );
  136. }
  137. }
  138. }