/sites/all/modules/custom/fast_gallery/modules/fg.api.php

https://bitbucket.org/vectorbase/vectorbase · PHP · 130 lines · 70 code · 18 blank · 42 comment · 16 complexity · 51e970a1c0f4456ad5b58b6c75440242 MD5 · raw file

  1. <?php
  2. // $Id:
  3. class FGApi {
  4. static private $instance = null;
  5. /**
  6. * We are implementing a singleton pattern
  7. */
  8. private function __construct() {
  9. }
  10. /**
  11. *
  12. * @return FGApi
  13. */
  14. public function getInstance() {
  15. if (is_null(self :: $instance)) {
  16. self :: $instance = new self;
  17. }
  18. return self :: $instance;
  19. }
  20. /**
  21. * filtering the params and checking if they are complete
  22. * @param array $params
  23. * @return array
  24. * - status -> boolean if need params are available
  25. * - params
  26. */
  27. public function transformParams($params) {
  28. $checkedParams = array();
  29. foreach ($params as $key => $item) {
  30. $checkedParams[$key] = check_plain($item);
  31. }
  32. if (!isset($checkedParams['type'])) {
  33. $checkedParams['type'] = 'html';
  34. }
  35. if (!isset($checkedParams['path'])) {
  36. return array('status' => FALSE, 'params' => $checkedParams);
  37. }
  38. if (!isset($checkedParams['gallery'])) {
  39. return array('status' => FALSE, 'params' => $checkedParams);
  40. }
  41. return array('status' => TRUE, 'params' => $checkedParams);
  42. }
  43. /**
  44. * the path alias needs to be transformed so it can be
  45. * matched onto the internal path to find the pictures
  46. * @param array $params
  47. * @return string
  48. */
  49. public function createFgGalleryPath($params) {
  50. $settings = variable_get('fg_galleries');
  51. //we first get the correct path, which depends on the path alias
  52. //then we put it together with the files path
  53. //remove slashed that are too much
  54. $ar_base_path = explode('/', $params['gallery']);
  55. if ($ar_base_path[count($ar_base_path)-1] == '') {
  56. unset($ar_base_path[count($ar_base_path)-1]);
  57. }
  58. if ($ar_base_path[0] == '') {
  59. unset($ar_base_path[0]);
  60. }
  61. $gallery = implode('/', $ar_base_path) . '/';
  62. $current_path_alias = $params['path'];
  63. $start = strlen($settings[$gallery]['fg_alias']);
  64. $path_new = substr($current_path_alias, ++$start);
  65. $path_new = $settings[$gallery]['fg_path'] . $path_new;
  66. return $path_new;
  67. }
  68. /**
  69. * The main function for converting to an XML document.
  70. * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
  71. *
  72. * @param array $data
  73. * @param string $rootNodeName - what you want the root node to be - defaultsto data.
  74. * @param SimpleXMLElement $xml - should only be used recursively
  75. * @return string XML
  76. */
  77. public function toXml($data, $rootNodeName = 'data', $xml=null) {
  78. if(empty($data)) {
  79. return false;
  80. }
  81. // turn off compatibility mode as simple xml throws a wobbly if you don't.
  82. if (ini_get('zend.ze1_compatibility_mode') == 1) {
  83. ini_set ('zend.ze1_compatibility_mode', 0);
  84. }
  85. if ($xml == null) {
  86. $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
  87. }
  88. // loop through the data passed in.
  89. foreach($data as $key => $value) {
  90. // no numeric keys in our xml please!
  91. if (is_numeric($key)) {
  92. // make string key...
  93. $key = "unknownNode_". (string) $key;
  94. }
  95. // replace anything not alpha numeric
  96. $key = preg_replace('/[^a-z]/i', '', $key);
  97. // if there is another array found recrusively call this function
  98. if (is_array($value)) {
  99. $node = $xml->addChild($key);
  100. // recrusive call.
  101. $this->toXml($value, $rootNodeName, $node);
  102. }
  103. else {
  104. // add single node.
  105. $value = htmlentities($value);
  106. $xml->addChild($key,$value);
  107. }
  108. }
  109. // pass back as string. or simple xml object if you want!
  110. return $xml->asXML();
  111. }
  112. }