PageRenderTime 22ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions.ext/freebedit/FreebeditController.php

https://code.google.com/p/ontowiki/
PHP | 272 lines | 173 code | 38 blank | 61 comment | 18 complexity | 4b9ab629e513b92af24bafda1912f478 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. */
  4. require_once 'OntoWiki/Controller/Component.php';
  5. /**
  6. * Component controller for the Freeb Editor.
  7. *
  8. * @author Kurt Jacobson <kurtjx at gmail>
  9. */
  10. class FreebeditController extends OntoWiki_Controller_Component
  11. {
  12. public function init()
  13. {
  14. parent::init();
  15. // m is automatically used and selected (as in FoafEdit)
  16. if ((!isset($this->_request->m)) && (!$this->_owApp->selectedModel)) {
  17. require_once 'OntoWiki/Exception.php';
  18. throw new OntoWiki_Exception('No model pre-selected model and missing parameter m (model)!');
  19. exit;
  20. } else {
  21. $this->model = $this->_owApp->selectedModel;
  22. $this->view->resourceUri = (string) $this->_owApp->selectedResource;
  23. }
  24. }
  25. /**
  26. * sparql query and return the array of defaultProperty's
  27. */
  28. private function getDefaultProperties($type){
  29. require_once 'Erfurt/Sparql/SimpleQuery.php';
  30. $query = new Erfurt_Sparql_SimpleQuery();
  31. // build SPARQL query for getting class defualt properties
  32. $query->setProloguePart('SELECT DISTINCT ?p')
  33. ->setWherePart('WHERE {<' . $type . '> <' . $this->_privateConfig->defaultProperty . '> ?p. }');
  34. // query the store
  35. if ($result = $this->model->sparqlQuery($query)) {
  36. foreach ($result as $row){
  37. $dprops[]=$row['p'];
  38. }
  39. return $dprops;
  40. }
  41. else{
  42. return null;
  43. }
  44. }
  45. /**
  46. * check if the property is an object property, return true if it is
  47. * TODO: there must be a more clever way
  48. */
  49. private function isObjectProperty($prop){
  50. require_once 'Erfurt/Sparql/SimpleQuery.php';
  51. $query = new Erfurt_Sparql_SimpleQuery();
  52. $query->setProloguePart('SELECT DISTINCT ?o')
  53. ->setWherePart('WHERE { ?s <' . $prop . '> ?o . }');
  54. if ($result = $this->model->sparqlQuery($query)){
  55. $obj = (string) $result[0]['o'];
  56. if($this->string_begins_with($obj, "http://")){
  57. return true;
  58. }
  59. else{
  60. return false;
  61. }
  62. }
  63. else{
  64. // if we don't find any triples with this property, we assume a data prop which is stupid probably
  65. // TODO: fix this somehow re: data props and object props
  66. return false;
  67. }
  68. }
  69. private function string_begins_with($string, $search)
  70. {
  71. return (strncmp($string, $search, strlen($search)) == 0);
  72. }
  73. public function thingAction()
  74. {
  75. //$store = $this->_owApp->erfurt->getStore();
  76. $resource = $this->_owApp->selectedResource;
  77. // set the title
  78. $title = $resource->getTitle() ? $resource->getTitle() : OntoWiki_Utils::contractNamespace((string) $resource);
  79. $this->view->placeholder('main.window.title')->append('FreeB view: ' . $title);
  80. $this->addModuleContext('main.window.properties');
  81. $this->view->title = $title;
  82. // we'll need the titleHelper later...
  83. require_once 'OntoWiki/Model/TitleHelper.php';
  84. $titleHelper = new OntoWiki_Model_TitleHelper($this->model);
  85. /*
  86. * get the types for the given URI
  87. */
  88. require_once 'Erfurt/Sparql/SimpleQuery.php';
  89. $query = new Erfurt_Sparql_SimpleQuery();
  90. // build SPARQL query for getting class (rdf:type) of current resource
  91. $query->setProloguePart('SELECT DISTINCT ?t')
  92. ->setWherePart('WHERE {<' . $resource . '> a ?t.}');
  93. // query the store
  94. if ($result = $this->model->sparqlQuery($query)) {
  95. foreach ($result as $row){
  96. $types[]=$row['t'];
  97. }
  98. $this->view->types = $types;
  99. $titleHelper->addResources($types);
  100. // get titles for types
  101. // do this later...
  102. foreach ($types as $type){
  103. $titles[$type] = $titleHelper->getTitle($type, 'en');
  104. }
  105. $this->view->type_titles = $titles;
  106. // if we've got types, get default properties
  107. foreach ($types as $type){
  108. $default_props[$type] = $this->getDefaultProperties($type);
  109. }
  110. $this->view->default_props = $default_props;
  111. }
  112. if (!isset($this->_request->r)) {
  113. require_once 'OntoWiki/Exception.php';
  114. throw new OntoWiki_Exception("Missing parameter 'r'.");
  115. exit;
  116. }
  117. require_once 'OntoWiki/Model/Resource.php';
  118. $resource = new OntoWiki_Model_Resource($this->model->getStore(), $this->model, $this->_request->r);
  119. $this->view->values = $resource->getValues();
  120. $predicates = $resource->getPredicates();
  121. $this->view->predicates = $predicates;
  122. // add graphs
  123. $graphs = array_keys($predicates);
  124. $titleHelper->addResources($graphs);
  125. /*$graphInfo = array();
  126. foreach ($graphs as $g) {
  127. $graphInfo[$g] = $titleHelper->getTitle($g, $this->_config->languages->locale);
  128. }*/
  129. $this->view->graphs = $graphs;//$graphInfo;
  130. // prepare namespaces
  131. /*
  132. * moved this to if statement below
  133. $namespaces = $this->model->getNamespaces();
  134. $graphBase = $this->model->getBaseUri();
  135. if (!array_key_exists($graphBase, $namespaces)) {
  136. $namespaces = array_merge($namespaces, array($graphBase => OntoWiki_Utils::DEFAULT_BASE));
  137. }
  138. $this->view->namespaces = $namespaces;*/
  139. $this->view->graphUri = $this->model->getModelIri();
  140. $this->view->graphBaseUri = $this->model->getBaseIri();
  141. // set RDFa widgets update info for editable graphs
  142. foreach ($graphs as $g) {
  143. if ($this->_erfurt->getAc()->isModelAllowed('edit', $g)) {
  144. $this->view->placeholder('update')->append(array(
  145. 'sourceGraph' => $g,
  146. 'queryEndpoint' => $this->_config->urlBase . 'sparql/',
  147. 'updateEndpoint' => $this->_config->urlBase . 'update/'
  148. ));
  149. }
  150. }
  151. // set up toolbar buttons
  152. if ($this->model->isEditable()) {
  153. // TODO: check acl
  154. $toolbar = $this->_owApp->toolbar;
  155. $toolbar->appendButton(OntoWiki_Toolbar::EDIT, array('name' => 'Edit Properties'));
  156. $params = array(
  157. 'name' => 'Delete Resource',
  158. 'url' => $this->_config->urlBase . 'resource/delete/?r=' . urlencode((string) $this->view->resourceUri)
  159. );
  160. $toolbar->appendButton(OntoWiki_Toolbar::SEPARATOR)
  161. ->appendButton(OntoWiki_Toolbar::DELETE, $params);
  162. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  163. // prepare namespaces
  164. $namespaces = $this->model->getNamespaces();
  165. $graphBase = $this->model->getBaseUri();
  166. if (!array_key_exists($graphBase, $namespaces)) {
  167. $namespaces = array_merge($namespaces, array($graphBase => OntoWiki_Utils::DEFAULT_BASE));
  168. }
  169. $this->view->namespaces = $namespaces;
  170. // add update vocabulary graph definitions
  171. $this->view->placeholder('update')->append(array(
  172. 'sourceGraph' => $this->model->getModelUri(),
  173. 'queryEndpoint' => $this->_config->urlBase . 'sparql/',
  174. 'updateEndpoint' => $this->_config->urlBase . 'update/'
  175. ));
  176. }
  177. /* ********************
  178. * parse the predicates into a freeb array
  179. */
  180. $freeb = array();
  181. $allprops = array();
  182. foreach($types as $type){
  183. $freeb[$type]['title'] = $titleHelper->getTitle($type, 'en');
  184. foreach(array_keys($predicates) as $graph){
  185. //$this->view->prop = $graph;
  186. // check against default URIs
  187. foreach(array_keys($predicates[$graph]) as $prop){
  188. //$this->view->prop = $prop;
  189. if (is_array($default_props[$type])){
  190. foreach($default_props[$type] as $dprop){
  191. $this->view->prop = $default_props;
  192. if (strcmp($prop,$dprop)==0){
  193. $freeb[$type][$graph][$dprop] = $predicates[$graph][$prop];
  194. $allprops[$type][] = $prop;
  195. unset($predicates[$graph][$prop]);
  196. //$predicates[$graph][$prop] = null;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. $this->view->predicates = $predicates;
  204. $this->view->freeb = $freeb;
  205. //$this->view->allprops = $allprops;
  206. /* get an array with just the missing props */
  207. $missingprops = array();
  208. foreach($default_props as $type=>$props){
  209. if(array_key_exists($type, $allprops) && !is_null($props)){
  210. foreach($props as $prop){
  211. if(!in_array($prop,$allprops[$type])){
  212. $titleHelper->addResource($prop);
  213. $missingprops[$type][$prop]['title'] = $titleHelper->getTitle($prop, 'en');
  214. $missingprops[$type][$prop]['object'] = $this->isObjectProperty($prop);
  215. }
  216. }
  217. }
  218. else{
  219. if(!is_null($props)){
  220. $titleHelper->addResources($props);
  221. foreach($props as $prop){
  222. //$missingprops[$type][$prop] = $titleHelper->getTitle($prop, 'en');
  223. $missingprops[$type][$prop]['title'] = $titleHelper->getTitle($prop, 'en');
  224. $missingprops[$type][$prop]['object'] = $this->isObjectProperty($prop);
  225. }
  226. }
  227. }
  228. }
  229. $this->view->missingprops = $missingprops;
  230. }
  231. }
  232. ?>