public function link($options = array()) { $model = OntoWiki::getInstance()->selectedModel; $titleHelper = new OntoWiki_Model_TitleHelper($model); // check for options and assign local vars or null $uri = isset($options['uri']) ? (string) $options['uri'] : null; $literal = isset($options['literal']) ? $options['literal'] : null; $text = isset($options['text']) ? $options['text'] : null; $property = isset($options['property']) ? $options['property'] : null; $class = isset($options['class']) ? ' class="' . $options['class'] . '"' : ''; $prefix = isset($options['prefix']) ? $options['prefix'] : ''; $suffix = isset($options['suffix']) ? $options['suffix'] : ''; $iprefix = isset($options['iprefix']) ? $options['iprefix'] : ''; $isuffix = isset($options['isuffix']) ? $options['isuffix'] : ''; $direct = isset($options['direct']) ? true : false; // resolve short forms (overwrite full name values with short forms values) $uri = isset($options['r']) ? (string) $options['r'] : $uri; $literal = isset($options['l']) ? $options['l'] : $literal; $text = isset($options['t']) ? $options['t'] : $text; $property = isset($options['p']) ? $options['p'] : $property; // if an uri is given, we do not need to search for if (isset($uri)) { // resolve qnames and check uri input $uri = Erfurt_Uri::getFromQnameOrUri((string) $uri, $model); } else { // if no uri is given, we need to search by using the literal if (!isset($literal)) { throw new Exception('The link helper needs at least one parameter literal or uri'); } // if a property is given, use <properyuri> instead of a variable part in the query $property = $property ? '<' . Erfurt_Uri::getFromQnameOrUri($property, $model) . '>' : '?property'; // build the query including PREFIX declarations $query = ''; foreach ($model->getNamespaces() as $ns => $nsprefix) { $query .= 'PREFIX ' . $nsprefix . ': <' . $ns . '>' . PHP_EOL; } $query .= 'SELECT DISTINCT ?resourceUri WHERE {?resourceUri ' . $property . ' ?literal FILTER (!isBLANK(?resourceUri)) FILTER (REGEX(?literal, "^' . $literal . '$", "i")) } LIMIT 1'; $result = $model->sparqlQuery($query); if (!$result) { // resource not found, so return plain literal or given text return isset($text) ? $text : $literal; } else { $uri = $result[0]['resourceUri']; } } // generate the link URL from the resource URI if ($direct == true) { $url = $uri; } else { $url = new OntoWiki_Url(array('route' => 'properties'), array('r')); $url->setParam('r', $uri, true); $url = (string) $url; } // link text comes from title helper or option $text = isset($text) ? $text : $titleHelper->getTitle($uri); return "{$prefix}<a{$class} href='{$url}'>{$iprefix}{$text}{$isuffix}</a>{$suffix}"; }
public function table($options = array()) { $model = OntoWiki::getInstance()->selectedModel; $titleHelper = new OntoWiki_Model_TitleHelper($model); // check for options and assign local vars or default values $class = isset($options['class']) ? $options['class'] : ''; $id = isset($options['id']) ? $options['id'] : ''; $tag = isset($options['tag']) ? $options['tag'] : 'span'; $prefix = isset($options['prefix']) ? $options['prefix'] : ''; $suffix = isset($options['suffix']) ? $options['suffix'] : ''; $iprefix = isset($options['iprefix']) ? $options['iprefix'] : ''; $isuffix = isset($options['isuffix']) ? $options['isuffix'] : ''; // choose, which uri to use: option over helper default over view value $uri = isset($this->resourceUri) ? $this->resourceUri : null; $uri = isset($options['selectedResource']) ? (string) $options['selectedResource'] : $uri; $uri = isset($options['uri']) ? (string) $options['uri'] : $uri; $uri = Erfurt_Uri::getFromQnameOrUri($uri, $model); // choose, which properties to use (todo: allow multple properties) $contentProperties = isset($options['property']) ? array($options['property']) : null; $contentProperties = !$contentProperties ? $this->contentProperties : $contentProperties; foreach ($contentProperties as $key => $value) { try { $validatedValue = Erfurt_Uri::getFromQnameOrUri($value, $model); $contentProperties[$key] = $validatedValue; } catch (Exception $e) { unset($contentProperties[$key]); } } // create description from resource URI $resource = new OntoWiki_Resource($uri, $model); $description = $resource->getDescription(); $description = $description[$uri]; // get the table size $tableRows = 0; // the URI of the main content property $tableColumns = 0; // the URI of the main content property foreach ($contentProperties as $contentProperty) { if (isset($description[$contentProperty . 'Rows'])) { $tableRows = (int) $description[$contentProperty . 'Rows'][0]['value']; } if (isset($description[$contentProperty . 'Columns'])) { $tableColumns = (int) $description[$contentProperty . 'Columns'][0]['value']; } if (null != $tableRows && null != $tableColumns) { $mainProperty = $contentProperty; break; } } $content = ''; // filter and render the table if (0 < $tableRows && 0 < $tableColumns) { $content .= '<table id=' . $id . '><thead>'; for ($row = 1; $row <= $tableRows; $row++) { $content .= '<tr id="R' . $row . '">'; for ($column = 1; $column <= $tableColumns; $column++) { $content .= (1 == $row ? '<th' : '<td') . ' id="R' . $row . 'C' . $column . '">'; $currentMainProperty = $mainProperty . 'R' . $row . 'C' . $column; unset($firstLiteral); // search for language tag foreach ($description[$currentMainProperty] as $literalNumber => $literal) { $currentLanguage = OntoWiki::getInstance()->getConfig()->languages->locale; if (isset($literal['lang']) && $currentLanguage == $literal['lang']) { $firstLiteral = $description[$currentMainProperty][$literalNumber]; break; } } if (!isset($firstLiteral)) { $firstLiteral = $description[$currentMainProperty][0]; } $cellContent = $firstLiteral['value']; // execute the helper markup on the content (after the extensions) $cellContent = $this->view->executeHelperMarkup($cellContent); // filter by using available extensions if (isset($firstLiteral['datatype'])) { $datatype = $firstLiteral['datatype']; $cellContent = $this->view->displayLiteralPropertyValue($cellContent, $currentMainProperty, $datatype); } else { $cellContent = $this->view->displayLiteralPropertyValue($cellContent, $currentMainProperty); } $curie = $this->view->curie($currentMainProperty); $cellContent = $iprefix . $cellContent . $isuffix; $content .= "{$prefix}<{$tag} class='{$class}' property='{$curie}'>{$cellContent}</{$tag}>{$suffix}"; $content .= 1 == $row ? '</th>' : '</td>'; } $content .= '</tr>'; $content .= 1 == $row ? '</thead><tbody>' : ''; } $content .= '</tbody></table>'; } return $content; }
public function img($options = array()) { $owapp = OntoWiki::getInstance(); $store = $owapp->erfurt->getStore(); $model = $owapp->selectedModel; $extManager = $owapp->extensionManager; // check for options and assign local vars or null $src = isset($options['src']) ? (string) $options['src'] : null; $class = isset($options['class']) ? ' class="' . $options['class'] . '"' : ''; $alt = isset($options['alt']) ? ' alt="' . $options['alt'] . '"' : ''; $filter = isset($options['filter']) ? $options['filter'] : null; $prefix = isset($options['prefix']) ? $options['prefix'] : ''; $suffix = isset($options['suffix']) ? $options['suffix'] : ''; $nordfa = isset($options['nordfa']) ? true : false; // choose, which uri to use: option over helper default over view value $uri = isset($this->resourceUri) ? $this->resourceUri : null; $uri = isset($options['selectedResource']) ? (string) $options['selectedResource'] : $uri; $uri = isset($options['uri']) ? (string) $options['uri'] : $uri; // in case qname is given, transform to full URI $uri = Erfurt_Uri::getFromQnameOrUri($uri, $model); // look for a valid image url somewhere if ($src == null) { // choose, which properties to use for lookup (todo: allow multple properties) $properties = isset($options['property']) ? array($options['property']) : null; $properties = !$properties ? $this->_properties : $properties; // validate each given property foreach ($properties as $key => $value) { try { $validatedValue = Erfurt_Uri::getFromQnameOrUri($value, $model); $properties[$key] = $validatedValue; } catch (Exception $e) { // unset invalid properties unset($properties[$key]); } } // create description from resource URI $resource = new OntoWiki_Resource($uri, $model); $description = $resource->getDescription(); $description = $description[$uri]; // select the used property $imgProperty = null; foreach ($properties as $property) { if (isset($description[$property])) { $imgProperty = $property; break; } } if ($imgProperty != null) { //search for language tag foreach ($description[$imgProperty] as $literalNumber => $literal) { $currentLanguage = OntoWiki::getInstance()->getConfig()->languages->locale; if (isset($literal['lang']) && $currentLanguage == $literal['lang']) { $imgSrc = $description[$imgProperty][$literalNumber]['value']; break; } } if (!isset($imgSrc)) { $imgSrc = $description[$imgProperty][0]['value']; } } else { // we do not have an image src return ''; } } else { $imgSrc = $src; } // modify the image imgSrc for the IPC extension // @todo: use an event here if ($filter && $extManager->isExtensionRegistered('ipc')) { $ipcSrc = $owapp->config->urlBase . '/ipc/get'; $ipcSrc = $ipcSrc . '?img=' . urlencode($imgSrc) . '&filter=' . urlencode($filter); $imgSrc = $ipcSrc; } // build the image tag for output $return = $prefix; $return .= '<img' . $class . $alt; if ($nordfa == false) { $return .= !$src ? ' about="' . $this->view->curie($uri) . '"' : ''; $return .= !$src ? ' property="' . $this->view->curie($imgProperty) . '"' : ''; // this property is needed since ipc maybe rewrites the src $return .= !$src ? ' resource="' . htmlentities($imgSrc) . '"' : ''; if (isset($options['attributes']) && is_array($options['attributes'])) { foreach ($options['attributes'] as $key => $value) { $return .= ' ' . $key . '="' . $value . '"'; } } } $return .= ' src="' . htmlentities($imgSrc) . '" />'; $return .= $suffix; return $return; }
protected function _selectMainProperty($model, $description, $options) { // choose, which properties to use (todo: allow multple properties) $contentProperties = isset($options['property']) ? array($options['property']) : null; $contentProperties = !$contentProperties ? $this->contentProperties : $contentProperties; foreach ($contentProperties as $key => $value) { try { $validatedValue = Erfurt_Uri::getFromQnameOrUri($value, $model); $contentProperties[$key] = $validatedValue; } catch (Exception $e) { unset($contentProperties[$key]); } } // select the main property from existing ones $mainProperty = null; // the URI of the main content property foreach ($contentProperties as $contentProperty) { if (isset($description[$contentProperty])) { $mainProperty = $contentProperty; break; } } return $mainProperty; }
/** * @expectedException Erfurt_Uri_Exception */ public function testGetFromQnameInvalidUri() { $model = $this->_getMockedModel(); $input = 'nothing:good'; Erfurt_Uri::getFromQnameOrUri($input, $model); }