/**
  * Builds the SPARQL query
  */
 private function _buildQueries()
 {
     $query = new Erfurt_Sparql_Query2();
     $uri = new Erfurt_Sparql_Query2_IriRef($this->_uri);
     $predVar = new Erfurt_Sparql_Query2_Var('predicate');
     $objVar = new Erfurt_Sparql_Query2_Var('object');
     $query->addTriple($uri, $predVar, $objVar);
     $query->addFilter(new Erfurt_Sparql_Query2_UnaryExpressionNot(new Erfurt_Sparql_Query2_isBlank($objVar)));
     if (!empty($this->_ignoredPredicates)) {
         $or = new Erfurt_Sparql_Query2_ConditionalAndExpression();
         $filter = new Erfurt_Sparql_Query2_Filter($or);
         foreach ($this->_ignoredPredicates as $ignored) {
             $or->addElement(new Erfurt_Sparql_Query2_UnaryExpressionNot(new Erfurt_Sparql_Query2_sameTerm($predVar, new Erfurt_Sparql_Query2_IriRef($ignored))));
         }
         $query->getWhere()->addElement($filter);
     }
     $query->setDistinct(true)->addProjectionVar($predVar)->addProjectionVar($objVar)->getOrder()->add($predVar);
     $queries = array();
     $closure = Erfurt_App::getInstance()->getStore()->getImportsClosure($this->_model->getModelUri(), true);
     $queryGraphs = array_merge(array($this->_graph), $closure);
     foreach ($queryGraphs as $currentGraph) {
         $query->setFroms(array($currentGraph));
         $queries[$currentGraph] = clone $query;
     }
     return $queries;
 }
 /**
  *
  * Enter description here ...
  *
  * @param   $uri         string to convert to nice uri
  * @param   $titleHelper TitleHelper instance to use to get titles for URIs
  */
 private function generateUriFromSparql($uri, $titleHelper)
 {
     $schema = $this->loadNamingSchema($uri);
     $properties = array();
     foreach ($schema as $element) {
         if (is_string($this->_config->property->{$element})) {
             $properties[$this->_config->property->{$element}] = array('element' => $element, 'rank' => '1');
         } elseif (is_array($this->_config->property->{$element}->toArray())) {
             $countDeep = 0;
             foreach ($this->_config->property->{$element}->toArray() as $elementDeep) {
                 $properties[(string) $elementDeep] = array('element' => $element, 'rank' => $countDeep++);
             }
         }
     }
     $query = new Erfurt_Sparql_Query2();
     $sRef = new Erfurt_Sparql_Query2_IriRef($uri);
     $pVar = new Erfurt_Sparql_Query2_Var('p');
     $oVar = new Erfurt_Sparql_Query2_Var('o');
     $query->addProjectionVar($pVar);
     $query->addProjectionVar($oVar);
     $query->addTriple($sRef, $pVar, $oVar);
     $query->addFrom((string) $this->_model);
     $query->setLimit(100);
     $query->setDistinct(true);
     $container = new Erfurt_Sparql_Query2_ConditionalOrExpression();
     foreach ($properties as $filterProp => $element) {
         $sameTerm = new Erfurt_Sparql_Query2_sameTerm($pVar, new Erfurt_Sparql_Query2_IriRef($filterProp));
         //$filter = new Erfurt_Sparql_Query2_Filter($sameTerm);
         $container->addElement($sameTerm);
     }
     $query->addFilter($container);
     $result = $this->_owApp->erfurt->getStore()->sparqlQuery($query, array('withImports' => true));
     $replacements = array();
     foreach ($result as $row) {
         if (array_key_exists($row['p'], $properties)) {
             $titleHelper->addResource($row['p']);
             if (Erfurt_Uri::check($row['o'])) {
                 $titleHelper->addResource($row['o']);
             }
             if (array_key_exists($properties[$row['p']]['element'], $replacements)) {
                 $newRank = (int) $properties[$row['p']]['rank'];
                 $minRank = $replacements[$properties[$row['p']]['element']]['rank'];
                 if ($newRank < $minRank) {
                     $replacements[$properties[$row['p']]['element']] = array('rank' => $newRank, 'value' => $row['o'], 'key' => $row['p']);
                 }
             } else {
                 $replacements[$properties[$row['p']]['element']] = array('rank' => $properties[$row['p']]['rank'], 'value' => $row['o'], 'key' => $row['p']);
             }
         }
     }
     $localName = '';
     foreach ($schema as $element) {
         if (array_key_exists($element, $replacements)) {
             if (Erfurt_Uri::check($replacements[$element]['value'])) {
                 $val = $titleHelper->getTitle($replacements[$element]['value']);
             } else {
                 $val = $replacements[$element]['value'];
             }
             $val = $this->convertChars($val);
             $key = $this->convertChars($titleHelper->getTitle($replacements[$element]['key']));
             $localName .= $key . '/' . $val . '/';
         }
     }
     // no meaningful localname created falback to old uri (TODO or md5 a new one?)
     if ($localName === '') {
         return $uri;
         //$localName = 'resource/' . md5($uri . time());
     }
     $base = '';
     if ($this->_model !== null && $this->_model->getBaseIri() !== '') {
         $base = $this->_model->getBaseIri();
         if ($base[strlen($base) - 1] !== '#' && $base[strlen($base) - 1] !== '/') {
             $base .= '/';
         }
     } else {
         $count = 0;
         foreach (explode('/', $uri) as $element) {
             if ($count > 2) {
                 break;
             } else {
                 $count++;
                 $base .= $element . '/';
             }
         }
     }
     return $base . $localName;
 }
 /**
  * Opens a resource context in data markup.
  *
  * Options (all are optional):
  * - resource - resource URI (by default, the current resource in template)
  * - type     - resource type (by default, rdf:type value for the specified resource)
  * - rel      - relation to the resource, string or array of strings
  * - rev      - reverse relation (from the resource), string or array of strings
  * - itemref  - microdata's itemref as an array of IDs
  * - tag      - HTML tag to create context with
  * - id       - HTML id attribute
  * - class    - HTML class attribute
  * - markup   - markup format ("RDFa", "microdata", false)
  *
  * The following options may be specified in template options:
  * rel, rev, itemref, tag, id, class
  */
 public function openContext($options = array())
 {
     $model = OntoWiki::getInstance()->selectedModel;
     $tmplOpt = $this->view->templateOptions();
     $markup = $tmplOpt->getValue('http://ns.ontowiki.net/SysOnt/Site/dataMarkupFormat', 'RDFa');
     $markup = isset($options['markup']) ? $options['markup'] : $markup;
     $resource = isset($options['resource']) ? $options['resource'] : $this->view->resourceUri;
     $html = array();
     $attr = '';
     $iprefix = '';
     $type = null;
     $rel = array();
     $rev = array();
     $itemref = null;
     // can generate new namespaces which weren't included in html element
     //$resource = $this->view->curie($resource);
     if (isset($options['type'])) {
         $type = $options['type'];
     } else {
         $query = new Erfurt_Sparql_Query2();
         $query->addProjectionVar($type = new Erfurt_Sparql_Query2_Var('type'));
         $query->addTriple(new Erfurt_Sparql_Query2_IriRef($resource), new Erfurt_Sparql_Query2_IriRef(EF_RDF_TYPE), $type);
         if ($types = $model->sparqlQuery($query)) {
             $type = $types[0]['type'];
         }
     }
     if (isset($options['rel'])) {
         $rel = $options['rel'];
     } elseif (isset($this->view->rel)) {
         $rel = $this->view->rel;
     }
     if (!is_array($rel)) {
         $rel = array($rel);
     }
     if (isset($options['rev'])) {
         $rev = $options['rev'];
     } elseif (isset($this->view->rev)) {
         $rev = $this->view->rev;
     }
     if (!is_array($rev)) {
         $rev = array($rev);
     }
     if (isset($options['itemref'])) {
         $itemref = $options['itemref'];
     } elseif (isset($this->view->itemref)) {
         $itemref = $this->view->itemref;
     }
     foreach (array('id', 'class') as $name) {
         if (isset($options[$name])) {
             $html[$name] = $options[$name];
         } elseif (isset($this->view->{$name})) {
             $html[$name] = $this->view->{$name};
         }
     }
     switch ($markup) {
         case 'RDFa':
             $attr .= ' resource="' . $resource . '"';
             if ($type !== null) {
                 $attr .= ' typeof="' . $type . '"';
             }
             if ($rel) {
                 $attr .= ' rel="' . implode(' ', $rel) . '"';
             }
             if ($rev) {
                 $attr .= ' rev="' . implode(' ', $rev) . '"';
             }
             break;
         case 'microdata':
             /* some parsers (Google) don't merge properties
                from multiple elements for the same resource */
             if (isset(static::$_itemref[$resource])) {
                 $flag = true;
                 if (!isset($html['id'])) {
                     //throw new OntoWiki_Exception('Attempting to create additional element for the same resource without itemref link (no itemref).');
                     $flag = false;
                 } elseif (!in_array($html['id'], static::$_itemref[$resource])) {
                     //throw new OntoWiki_Exception('Attempting to create additional element for the same resource without itemref link (ID not listed in itemref).');
                     $flag = false;
                 }
                 if ($flag) {
                     break;
                 }
             }
             $attr .= ' itemscope="itemscope"';
             /* "The itemid attribute must not be specified on elements
                that do not have both an itemscope attribute
                and an itemtype attribute specified" */
             if ($type !== null) {
                 $attr .= sprintf(' itemid="%s" itemtype="%s"', $resource, $type);
             }
             if ($rel) {
                 $attr .= sprintf(' itemprop="%s"', implode(' ', $rel));
             }
             //if ($rev)           $iprefix = sprintf('<link itemprop="%s" href="#TODO"/>%s', implode(' ', $rev), $iprefix);
             if ($itemref) {
                 $attr .= sprintf(' itemref="%s"', implode(' ', $itemref));
                 /* remember which elements are linked using itemref
                    so context markup can be disabled for them later */
                 static::$_itemref[$resource] = $itemref;
             } else {
                 static::$_itemref[$resource] = array();
             }
             break;
     }
     foreach ($html as $name => $value) {
         $attr .= ' ' . $name . '="' . $value . '"';
     }
     if (!isset($options['tag'])) {
         if (!isset($this->view->tag)) {
             $this->view->tag = 'span';
         }
         $tag = $this->view->tag;
     } else {
         $tag = $options['tag'];
     }
     // stack information for closecontext
     if (!isset($this->view->dataMarkupContextStack)) {
         $this->view->dataMarkupContextStack = array();
     }
     array_push($this->view->dataMarkupContextStack, array('tag' => $tag));
     return "<{$tag}{$attr}>{$iprefix}";
 }
 public function querylist($query, $template, $templateOptions = array(), $options = array())
 {
     $owapp = OntoWiki::getInstance();
     $store = $owapp->erfurt->getStore();
     $model = $owapp->selectedModel;
     $prefix = isset($options['prefix']) ? $options['prefix'] : '';
     $suffix = isset($options['suffix']) ? $options['suffix'] : '';
     $delimiter = isset($options['delimiter']) ? $options['delimiter'] : '';
     $property = isset($options['property']) ? $options['property'] : '';
     if ($this->_titleHelper == null) {
         $this->_titleHelper = new OntoWiki_Model_TitleHelper($model);
     }
     if ($property !== '') {
         // construct query to retrieve specified property of the current resource
         $query = new Erfurt_Sparql_Query2();
         $resourceUriVar = new Erfurt_Sparql_Query2_Var('resourceUri');
         $query->addProjectionVar($resourceUriVar);
         $query->addTriple(new Erfurt_Sparql_Query2_IriRef($this->view->resourceUri), new Erfurt_Sparql_Query2_IriRef($property), $resourceUriVar);
     }
     try {
         $result = $model->sparqlQuery($query);
     } catch (Exception $e) {
         // executions failed (return nothing)
         return $e->getMessage();
     }
     // pre-fill the title helper
     foreach ($result as $row) {
         foreach ($row as $value) {
             if (Erfurt_Uri::check($value)) {
                 $this->_titleHelper->addResource($value);
             }
         }
     }
     /*
      * If the ordering doesn't work using the 'ORDER BY' clause you should add ASC() around the
      * variable which should by sorted. E.g. "… } ORDER BY ASC(?start) ASC(?end)"
      */
     if (!stristr($query, 'ORDER BY')) {
         // sort results by resource title
         usort($result, array('Site_View_Helper_Querylist', '_cmpTitles'));
     }
     $return = '';
     $count = count($result);
     $current = 0;
     $odd = true;
     foreach ($result as $row) {
         // shift status vars
         $current++;
         $odd = !$odd;
         // prepare a first / last hint for the template
         $listhint = $current == 1 ? 'first' : null;
         $listhint = $current == $count ? 'last' : $listhint;
         $row['listhint'] = $listhint;
         // prepare other template vars
         $row['oddclass'] = $odd ? 'odd' : 'even';
         $row['rowcount'] = $count;
         $row['current'] = $current;
         if (!Erfurt_Uri::check($row['resourceUri'])) {
             $row['title'] = $row['resourceUri'];
         } else {
             $row['title'] = $this->_titleHelper->getTitle($row['resourceUri']);
         }
         $row = array_merge($row, $templateOptions);
         // render the template
         $return .= $prefix . $this->view->partial($template, $row) . $suffix;
         if ($current < $count) {
             $return .= $delimiter;
         }
     }
     return $return;
 }