protected function prepareAttributesForHtml(array $attributes) { $attributes = parent::prepareAttributesForHtml($attributes); list($attributes['href'], $attributes['params']) = $this->getBaseHref(true); if (array_key_exists('params', $attributes)) { if (!empty($attributes['params'])) { $attributes['href'] = $this->buildUrl(dmString::getBaseFromUrl($attributes['href']), array_merge(dmString::getDataFromUrl($attributes['href']), $attributes['params'])); /* * if last href char is a =, remove it * fixes http://github.com/diem-project/diem/issues/#issue/6 */ if ('=' === substr($attributes['href'], -1)) { $attributes['href'] = substr($attributes['href'], 0, strlen($attributes['href']) - 1); } } unset($attributes['params']); } if (isset($attributes['anchor'])) { $attributes['href'] .= '#' . $attributes['anchor']; } // makes unit testing easier ksort($attributes); return $attributes; }
public function initialize($source, $isDefault = false) { $this->source = $source; if (empty($source)) { } elseif (is_string($source)) { if (strncmp($source, 'media:', 6) === 0) { $mediaId = preg_replace('|^media:(\\d+).*|', '$1', $source); if ($media = dmDb::table('DmMedia')->findOneByIdWithFolder($mediaId)) { $this->fromMedia($media); } else { throw new dmException(sprintf('%s is not a valid media resource. The media with id %s does not exist', $source, $mediaId)); } } elseif (0 === strncmp($source, 'http://', 7)) { $this->type = self::REMOTE; $this->remotePath = $source; $this->mime = $this->mimeTypeResolver->getGroupByFilename(dmString::getBaseFromUrl($source)); $this->mime = $this->mime ? $this->mime : 'image'; } else { $this->type = self::FILE; // allow culture variable in source if (strpos($source, '%culture%') !== false) { $source = str_replace('%culture%', $this->culture, $source); } /* * Server full path */ if (strpos($source, sfConfig::get('sf_web_dir')) === 0) { $this->pathFromWebDir = str_replace(sfConfig::get('sf_web_dir'), '', $source); } elseif (strncmp($source, '/', 1) === 0) { $this->pathFromWebDir = $source; } elseif (strncmp($source, 'dm', 2) === 0) { $type = preg_replace('|^dm(\\w+)/.+$|', '$1', $source); $this->pathFromWebDir = '/' . sfConfig::get('dm_' . dmString::modulize($type) . '_asset') . '/' . str_replace('dm' . $type . '/', '', $source); } else { // and now some magic to allow to use "images/file.png" writing only "file.png" if (strncmp($source, 'images/', 7) !== 0 && file_exists($this->theme->getFullPath('images/' . $source)) && !file_exists($this->theme->getFullPath($source))) { $this->pathFromWebDir = $this->theme->getPath('images/' . $source); } else { $this->pathFromWebDir = $this->theme->getPath($source); } } $this->mime = $this->mimeTypeResolver->getGroupByFilename(dmString::getBaseFromUrl($source)); } } elseif ($source instanceof DmMedia) { if ($source->isNew()) { } else { $this->fromMedia($source); } } else { throw new dmException('Not a valid media source : ' . $source); } if (!$this->getMime()) { if ($isDefault) { $this->pathFromWebDir = ''; $this->mime = 'image'; } else { $this->initialize(sfConfig::get('dm_media_default'), true); } } return $this; }
public function initialize($source) { $this->source = $source; $this->params = false; if (empty($source)) { $this->type = 'page'; $this->subject = dmDb::table('DmPage')->findOneBySource(null); } elseif (is_string($source)) { /* * If a blank space is found in the source, * remove characters after it * because they are just a comment * i.e. page:1?var=val Home */ if ($blankSpacePos = strpos($source, ' ')) { $source = substr($source, 0, $blankSpacePos); } /* * Extract url parameters from source string */ $this->params = dmString::getDataFromUrl($source); $source = dmString::getBaseFromUrl($source); if ('@homepage' === $source) { $source = 'main/root'; } if (strncmp($source, 'page:', 5) === 0) { if ($page = dmDb::table('DmPage')->findOneBySource($source)) { $this->type = 'page'; $this->subject = $page; } else { throw new dmException(sprintf('%s is not a valid link resource', $source)); } } elseif (strncmp($source, 'media:', 6) === 0) { if ($media = dmDb::table('DmMedia')->findOneByIdWithFolder(substr($source, 6))) { $this->type = 'media'; $this->subject = $media; } else { throw new dmException(sprintf('%s is not a valid link resource', $source)); } } elseif (strncmp($source, 'app:', 4) === 0) { $app = substr($source, 4); /* * A slug may be added to the app name, extract it */ if ($slashPos = strpos($app, '/')) { $slug = substr($app, $slashPos); $app = substr($app, 0, $slashPos); } else { $slug = ''; } $this->type = 'uri'; $this->subject = dmContext::hasInstance() ? dmContext::getInstance()->get('script_name_resolver')->get($app) . $slug : $slug; } elseif (strncmp($source, '@', 1) === 0) { $this->type = 'route'; $this->subject = $source; } elseif (strncmp($source, 'http://', 7) === 0 || strncmp($source, 'https://', 7) === 0 || strncmp($source, 'ftp://', 6) === 0 || strncmp($source, 'mailto:', 7) === 0 || strncmp($source, '#', 1) === 0) { $this->type = 'uri'; $this->subject = $source; } elseif (strncmp($source, '/', 1) === 0) { $this->type = 'uri'; $this->subject = $source; } elseif (strncmp($source, '+/', 2) === 0) { $this->type = 'action'; $this->subject = substr($source, 2); } elseif (substr_count($source, '/') === 1) { if ($page = dmDb::table('DmPage')->findOneBySource($source)) { $this->type = 'page'; $this->subject = $page; } else { throw new dmException(sprintf('%s is not a valid link resource', $source)); } } else { throw new dmException(sprintf('%s is not a valid link resource', $source)); } } elseif (is_object($source)) { if ($source instanceof DmPage) { $this->type = 'page'; $this->subject = $source; } elseif ($source instanceof DmMedia) { $this->type = 'media'; $this->subject = $source; } elseif ($source instanceof dmDoctrineRecord) { if ($module = $source->getDmModule()) { if ($module->hasPage()) { $this->type = 'record'; $this->subject = $source; } else { throw new dmException(sprintf('%s module has no page', $module)); } } else { throw new dmException(sprintf('%s object can not be associated to a page', get_class($source))); } } elseif ($source instanceof Exception) { $this->type = 'error'; $this->subject = $source; } } elseif (is_array($source)) { if (isset($source[1])) { if (is_object($source[1])) { $this->type = 'action'; $this->subject = array('sf_route' => $source[0], 'sf_subject' => $source[1]); } } else { $this->type = 'action'; $this->subject = $source; } } if (empty($this->type) || empty($this->subject)) { throw new dmException(sprintf('dmFrontLinkResource can not determine type of %s', $source)); } return $this; }