PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/bx/plugins/admin/properties.php

https://github.com/chregu/fluxcms
PHP | 295 lines | 243 code | 41 blank | 11 comment | 41 complexity | 416b5f952d72ad05d941fa5de440d5a8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. class bx_plugins_admin_properties extends bx_component implements bxIplugin {
  3. static private $instance = array();
  4. private $propConfig = NULL;
  5. private $lastConfigFile = NULL;
  6. private $category = '';
  7. public static function getInstance($mode) {
  8. if (!self::$instance) {
  9. self::$instance = new bx_plugins_admin_properties($mode);
  10. }
  11. return self::$instance;
  12. }
  13. protected function __construct($mode) {
  14. $this->mode = $mode;
  15. //$this->loadConfigFile();
  16. // create instance of property config
  17. }
  18. protected function loadConfigFile($resourceName = null) {
  19. $configFile = null;
  20. if ($resourceName ) {
  21. $configFile = BX_PROJECT_DIR.'conf/properties/'.str_replace("_","/",$resourceName).'.xml';
  22. if (!file_exists($configFile)) {
  23. $configFile = BX_PROJECT_DIR.'conf/properties/'.dirname($resourceName). '.xml';
  24. if (!file_exists($configFile)) {
  25. $configFile = null;
  26. }
  27. }
  28. }
  29. if (!$configFile) {
  30. if (!(defined('BX_PROPERTYCONFIG_FILENAME'))) {
  31. //define('BX_PROPERTYCONFIG_FILENAME',BX_PROJECT_DIR.'conf/properties/audio/mpeg.xml');
  32. define('BX_PROPERTYCONFIG_FILENAME',BX_PROJECT_DIR.'conf/properties/properties.xml');
  33. }
  34. $configFile = BX_PROPERTYCONFIG_FILENAME;
  35. }
  36. if ($this->lastConfigFile != $configFile) {
  37. $this->propConfig = new bx_propertyconfig($configFile);
  38. $this->lastConfigFile = $configFile;
  39. }
  40. // get category from request var
  41. $categories = $this->propConfig->getCategories();
  42. $this->category = isset($_REQUEST['category']) ? $_REQUEST['category'] : NULL;
  43. if(!in_array($this->category, array_keys($categories))) {
  44. $this->category = $this->propConfig->getDefaultCategory();
  45. }
  46. }
  47. public function getIdByRequest($path, $name = NULL, $ext = NULL) {
  48. if ($ext) {
  49. return $name.".$ext";
  50. } else {
  51. return $name;
  52. }
  53. }
  54. protected function getFullPath($name, $ext) {
  55. if(!empty($ext)) {
  56. return "/$name.$ext";
  57. } else {
  58. return "/$name";
  59. }
  60. }
  61. public function getResourceById($path, $id, $mock = false) {
  62. print $id;
  63. }
  64. public function getContentUriById($path, $id) {
  65. }
  66. public function getContentById($path, $id) {
  67. $perm = bx_permm::getInstance();
  68. if (!$perm->isAllowed(substr($id, 0, strrpos($id, '/', -1)+1),array('collection-back-properties'))) {
  69. throw new BxPageNotAllowedException();
  70. }
  71. $fullPath = $id;
  72. $dom = new domDocument();
  73. $dom->appendChild($dom->createElement('properties'));
  74. $dom->documentElement->setAttribute('path', $fullPath);
  75. if (isset($_GET['updateTree']) ) {
  76. $dom->documentElement->setAttribute('updateTree',$_GET['updateTree']);
  77. }
  78. $parts = bx_collections::getCollectionAndFileParts($fullPath, $this->mode);
  79. $coll = $parts['coll'];
  80. $id = $parts['rawname'];
  81. if($coll instanceof bx_collection) {
  82. if ($id == "") {
  83. $res = $coll;
  84. } else {
  85. $plugin = $coll->getPluginById($id);
  86. if (!$plugin) {
  87. $res = new bx_resources_file($path,$id);
  88. } else {
  89. $res = $plugin->getResourceById($coll->uri, $id);
  90. }
  91. }
  92. // create node containing all categories
  93. $this->loadConfigFile($res->getMimeType());
  94. $categories = $this->propConfig->getCategoriesByResourceName($res->getMimeType());
  95. $catNode = $dom->createElement('categories');
  96. foreach($categories as $category => $cat) {
  97. $node = $dom->createElement('category');
  98. $node->setAttribute('name', $category);
  99. if(!empty($cat['hasProperties'])) {
  100. $node->setAttribute('hasProperties', 'true');
  101. }
  102. // mark active category
  103. if($category == $this->category) {
  104. $node->setAttribute('active', 'true');
  105. }
  106. $catNode->appendChild($node);
  107. }
  108. $dom->documentElement->appendChild($catNode);
  109. if($res instanceof bxIresource) {
  110. // get all properties for this resource
  111. $resourceProperties = $res->getAllProperties();
  112. // get all properties for the current category and resource name
  113. $categoryProperties = $this->propConfig->getPropertiesByCategoryAndResourceName($this->category, $res->getMimeType());
  114. $categoryMetadatas = $this->propConfig->getMetadatasByCategoryAndResourceName($this->category, $res->getMimeType());
  115. if(!empty($categoryProperties)) {
  116. foreach($categoryProperties as $key => $property) {
  117. $propNode = $dom->createElement('property');
  118. $propNode->setAttribute('path', $fullPath);
  119. $propNode->setAttribute('namespace', $property['namespace']);
  120. $propNode->setAttribute('name', $property['name']);
  121. if (isset($property['niceName'])) {
  122. $propNode->setAttribute('niceName', $property['niceName']);
  123. }
  124. // use an already existing value or get a default one from the metadata or the property config
  125. if(isset($resourceProperties[$key]['value'])) {
  126. $value = $resourceProperties[$key]['value'];
  127. } else if(method_exists($categoryMetadatas[$key], 'getDefaultValue')) {
  128. $value = $categoryMetadatas[$key]->getDefaultValue();
  129. } else if(!empty($categoryProperties[$key]['defaultValue'])) {
  130. $value = $categoryProperties[$key]['defaultValue'];
  131. } else {
  132. $value = NULL;
  133. }
  134. $propNode->setAttribute('value', html_entity_decode($value, ENT_NOQUOTES, "UTF-8"));
  135. $propNode->setAttribute('fieldname', $key);
  136. if($categoryMetadatas[$key] instanceof bx_metadata) {
  137. $import = $dom->importNode($categoryMetadatas[$key]->serializeToDOM(), TRUE);
  138. $propNode->appendChild($import);
  139. }
  140. $dom->documentElement->appendChild($propNode);
  141. }
  142. }
  143. }
  144. }
  145. return $dom;
  146. }
  147. public function handlePOST($path, $id, $data) {
  148. $perm = bx_permm::getInstance();
  149. if (!$perm->isAllowed('/'.substr($id, 0, strrpos($id, '/', -1)+1),array('collection-back-properties'))) {
  150. throw new BxPageNotAllowedException();
  151. }
  152. // rewrite name and extension from extracted fullpath
  153. $fullPath = str_replace("//","/","/".$id);
  154. $parts = bx_collections::getCollectionAndFileParts($fullPath, $this->mode);
  155. $coll = $parts['coll'];
  156. $id = $parts['rawname'];
  157. if(!empty($data[$fullPath])) {
  158. if ($id == "") {
  159. $res = $coll;
  160. } else {
  161. $plugin = $coll->getPluginById($id);
  162. if ($plugin instanceof bxIplugin) {
  163. $res = $plugin->getResourceById($coll->uri,$id);
  164. } else {
  165. //FIXME: show better alerts...
  166. print "No matching plugin found ... Can't save properties";
  167. return false;
  168. }
  169. }
  170. // get all properties for this resource
  171. $resourceProperties = $res->getAllProperties();
  172. $this->loadConfigFile($res->getMimeType());
  173. // get all properties for the current category and resource name
  174. $categoryProperties = $this->propConfig->getPropertiesByCategoryAndResourceName($this->category, $res->getMimeType());
  175. $categoryMetadatas = $this->propConfig->getMetadatasByCategoryAndResourceName($this->category, $res->getMimeType());
  176. if(!empty($categoryProperties)) {
  177. foreach($categoryProperties as $key => $property) {
  178. if($categoryMetadatas[$key]->isChangeable()) {
  179. $value = isset($data[$fullPath][$key]) ? $data[$fullPath][$key] : NULL;
  180. // let metadata class handle the posted value if possible
  181. if(method_exists($categoryMetadatas[$key], 'getPropertyValueFromPOSTValue')) {
  182. $value = $categoryMetadatas[$key]->getPropertyValueFromPOSTValue($value,$res,$property['name']);
  183. }
  184. // look for changed or new values and update them
  185. if((isset($resourceProperties[$key]) && ($value !== $resourceProperties[$key]['value'])) || (!isset($resourceProperties[$key]) && !(empty($value) && $value !== "0"))) {
  186. bx_log::log("set property $key = $value for $fullPath");
  187. $res->setProperty($property['name'], bx_helpers_string::utf2entities($value), $property['namespace']);
  188. }
  189. // delete empty properties if required
  190. // but not if $value === 0
  191. if(empty($value) && $property['deleteOnEmpty'] && isset($resourceProperties[$key]) && $value !== "0") {
  192. bx_log::log("removed property $key for $fullPath");
  193. $res->removeProperty($property['name'], $property['namespace']);
  194. }
  195. }
  196. }
  197. }
  198. // we obvisously have handled some POST data
  199. return TRUE;
  200. }
  201. return FALSE;
  202. }
  203. public function getContentUri($path,$name, $ext) {
  204. return FALSE;
  205. }
  206. public function getResourceByRequest($path, $name, $ext) {
  207. return FALSE;
  208. }
  209. /** bx_plugin::getPipelineParametersById */
  210. public function getPipelineParametersById($path, $id) {
  211. return array();
  212. }
  213. public function resourceExists($path, $name, $ext) {
  214. return TRUE;
  215. }
  216. public function getChildren($uri) {
  217. return FALSE;
  218. }
  219. public function getEditorsByRequest($path, $name, $ext) {
  220. return FALSE;
  221. }
  222. public function getDataUri() {
  223. return NULL;
  224. }
  225. public function getStylesheetNameById() {
  226. }
  227. public function adminResourceExists($path, $id, $ext=null) {
  228. return $this;
  229. }
  230. public function stripRoot() {
  231. return false;
  232. }
  233. }
  234. ?>