PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/views/php-templates/page.php

http://puelia-php.googlecode.com/
PHP | 235 lines | 201 code | 32 blank | 2 comment | 24 complexity | 88fa2923bf186da7dd7bd99529f7f15a MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. require 'lib/moriarty/graphpath.class.php';
  3. class PueliaPage {
  4. var $data;
  5. var $config;
  6. var $request;
  7. var $api;
  8. var $base;
  9. var $endpointType;
  10. var $uri=null;
  11. function __construct($pageUri, $data,$config,$request){
  12. $this->uri = $pageUri;
  13. $this->data = $data;
  14. $this->config = $config;
  15. $this->request = $request;
  16. $this->apiUri = $this->config->getApiUri();
  17. $this->base = $this->request->getBaseAndSubDir();
  18. $this->endpointType = $this->config->getEndpointType();
  19. $this->datasetUri = $this->config->get_first_resource($this->apiUri, API.'dataset');
  20. $this->datasetName = $this->config->get_label($this->datasetUri);
  21. $this->topic = new PueliaItem($data->get_first_resource($request->getUri(), FOAF.'primaryTopic'), $data, $this->config);
  22. }
  23. function getTitle(){
  24. return $this->config->get_label($this->apiUri);
  25. }
  26. function getDescription(){
  27. return $this->config->get_description($this->apiUri);
  28. }
  29. function getViewerLinksAndLabels(){
  30. $links = array();
  31. $viewerUris = $this->data->get_subject_property_values($this->uri, DCT.'hasVersion');
  32. foreach($viewerUris as $vUri){
  33. $name = $this->data->get_label($vUri['value']);
  34. $links[$vUri['value']] = $name;
  35. }
  36. return $links;
  37. }
  38. function getFormatLinksAndLabels(){
  39. $links = array();
  40. $formatUris = $this->data->get_subject_property_values($this->uri, DCT.'hasFormat');
  41. foreach($formatUris as $fUri){
  42. $name = $this->data->get_label($fUri['value']);
  43. $links[$fUri['value']] = $name;
  44. }
  45. return $links;
  46. }
  47. function getNext(){
  48. return $this->data->get_first_resource($this->uri, XHV.'next');
  49. }
  50. function getPrev(){
  51. return $this->data->get_first_resource($this->uri, XHV.'prev');
  52. }
  53. function getFirst(){
  54. return $this->data->get_first_resource($this->uri, XHV.'first');
  55. }
  56. function getEndpointLinks(){
  57. $endpointUris = $this->config->get_resource_triple_values($this->apiUri, API.'endpoint');
  58. $endpointLinks = array();
  59. foreach($endpointUris as $uri){
  60. if($this->config->has_resource_triple($uri, RDF_TYPE, API.'ListEndpoint')){
  61. if($linkUri = $this->base.$this->config->get_first_literal($uri,API.'uriTemplate') AND !strpos($linkUri,'{')){
  62. $endpointLinks[$linkUri] = $this->config->get_label($uri);
  63. }
  64. }
  65. }
  66. return $endpointLinks;
  67. }
  68. function getItems(){
  69. if($listUri = '_:itemsList'){
  70. //$this->data->get_first_resource($this->uri, API.'items')){
  71. $list = $this->data->get_list_values($listUri);
  72. $ObjectList = array();
  73. foreach($list as $uri) $ObjectList[]=new PueliaItem($uri, $this->data, $this->config);
  74. return $ObjectList;
  75. }
  76. }
  77. }
  78. class PueliaItem {
  79. var $uri, $data, $docUri;
  80. var $img=false;
  81. var $label = false;
  82. var $description=false;
  83. var $isMappable= false;
  84. var $latitude = false;
  85. var $longitude=false;
  86. var $GraphPath = false;
  87. var $gotProperties = array();
  88. function __construct($uri, $data, $config){
  89. $this->uri = $uri;
  90. $this->docUri = $config->dataUriToEndpointItem($uri);
  91. if(!$this->docUri) $this->docUri = $this->uri;
  92. $this->config = $config;
  93. $this->data = $data;
  94. $this->description = $data->get_description($uri);
  95. $this->img = $this->getImg();
  96. $this->label = $this->data->get_label($uri);
  97. $this->latitude = $this->data->get_first_literal($uri, GEO.'lat');
  98. $this->longitude = $this->data->get_first_literal($uri, GEO.'long');
  99. // $this->GraphPath = new GraphPath();
  100. }
  101. function __get($name){
  102. foreach ($this->data->get_subject_properties($this->uri) as $prop) {
  103. if(substr($prop, (0-strlen($name)))==$name ) # is $name the localname of a property?
  104. {
  105. $this->gotProperties[]=$prop;
  106. $first = array_shift($this->data->get_subject_property_values($this->uri, $prop));
  107. return $first['value'];
  108. }
  109. }
  110. if(in_array($name, array_keys(get_object_vars($this)))) {
  111. return $this->$name;
  112. } else {
  113. return false;
  114. }
  115. }
  116. function __call($name, $arguments){
  117. if(in_array($name, get_class_methods($this))){
  118. call_user_method($name, $this, $arguments);
  119. } else if($uri = $this->$name){
  120. return new PueliaItem($this->$name, $this->data, $this->config);
  121. }
  122. else if(in_array($name, array_keys(get_object_vars($this)))) {
  123. return new PueliaItem($this->$name, $this->data, $this->config);
  124. } else {
  125. return new PueliaItem('_:false', $this->data, $this->config);
  126. }
  127. }
  128. function all($name){
  129. foreach ($this->data->get_subject_properties($this->uri) as $prop) {
  130. if(substr($prop, (0-strlen($name)))==$name ) # is $name the localname of a property?
  131. {
  132. $this->gotProperties[]=$prop;
  133. $objects = $this->data->get_subject_property_values($this->uri, $prop);
  134. $values= array();
  135. foreach($objects as $o) $values[]=$o['value'];
  136. return $values;
  137. }
  138. }
  139. }
  140. function classes($uriList){
  141. $types = array();
  142. foreach($uriList as $uri){
  143. $types = array_merge($this->data->get_resource_triple_values($uri, RDF_TYPE), $types);
  144. }
  145. return array_unique($types);
  146. }
  147. function filter($p, $o, $uriList=false){
  148. if(!$uriList){
  149. $uriList = $this->data->get_subjects();
  150. }
  151. $filteredUris = array();
  152. foreach($uriList as $index => $s){
  153. if($vals = $this->data->get_subject_property_values($s, $p)){
  154. foreach($vals as $object){
  155. if($object['value']==$o) $filteredUris[]=$s;
  156. }
  157. }
  158. }
  159. return $filteredUris;
  160. }
  161. function has($properties){
  162. foreach($properties as $property){
  163. if($this->$property) return true;
  164. }
  165. }
  166. function getImg(){
  167. $imgProps = array(FOAF.'depiction', FOAF.'img', FOAF.'logo');
  168. foreach($imgProps as $p){
  169. if($img = $this->data->get_first_resource($this->uri, $p)){
  170. $this->gotProperties[]=$p;
  171. return $img;
  172. }
  173. }
  174. }
  175. function isMappable(){
  176. if(!empty($this->latitude) AND !empty($this->longitude)) return true;
  177. else return false;
  178. }
  179. function otherPropertyValues(){
  180. $properties = $this->data->get_subject_properties($this->uri);
  181. $otherProperties = array_diff($properties, $this->gotProperties);
  182. $otherPropertyValues = array();
  183. foreach($otherProperties as $prop){
  184. $otherPropertyValues[$prop] = $this->data->get_subject_property_values($this->uri, $prop);
  185. }
  186. return $otherPropertyValues;
  187. }
  188. function uri($name){
  189. foreach ($this->data->get_subject_properties($this->uri) as $prop) {
  190. if(substr($prop, (0-strlen($name)))==$name ) # is $name the localname of a property?
  191. {
  192. return $prop;
  193. }
  194. }
  195. return false;
  196. }
  197. }
  198. function isImg($uri){
  199. $uri = parse_url($uri, PHP_URL_PATH);
  200. if( $ext = array_pop(explode('.',$uri))) return in_array($ext,array('jpg','gif','png','jpeg','tiff','ico'));
  201. else return false;
  202. }
  203. ?>