|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Cmf\Bundle\SimpleCmsBundle\Model; |
| 4 | + |
| 5 | +use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; |
| 6 | +use Symfony\Component\Validator\Constraints as Assert; |
| 7 | +use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishableInterface; |
| 8 | +use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodInterface; |
| 9 | +use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * This is the standard Page document. |
| 13 | + * |
| 14 | + * It adds the following to the base document |
| 15 | + * |
| 16 | + * - Translatable |
| 17 | + * - Publish Workflow |
| 18 | + * - Tags |
| 19 | + * |
| 20 | + * Additionally you can store "extra" string values in it for application |
| 21 | + * specific purposes. |
| 22 | + * |
| 23 | + * @PHPCRODM\Document(translator="attribute") |
| 24 | + */ |
| 25 | +class Page extends PageBase implements |
| 26 | + PublishTimePeriodInterface, |
| 27 | + PublishableInterface, |
| 28 | + TranslatableInterface |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var \DateTime |
| 32 | + */ |
| 33 | + protected $publishStartDate; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \DateTime |
| 37 | + */ |
| 38 | + protected $publishEndDate; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var boolean |
| 42 | + */ |
| 43 | + protected $publishable = true; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var boolean |
| 47 | + */ |
| 48 | + protected $addLocalePattern; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string |
| 52 | + */ |
| 53 | + protected $locale; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var array |
| 57 | + */ |
| 58 | + protected $tags = array(); |
| 59 | + |
| 60 | + /** |
| 61 | + * Extra values an application can store along with a page |
| 62 | + */ |
| 63 | + protected $extras; |
| 64 | + |
| 65 | + /** |
| 66 | + * Overwrite to be able to create route without pattern |
| 67 | + * |
| 68 | + * @param Boolean $addFormatPattern whether to add ".{_format}" to the route pattern |
| 69 | + * also implicitly sets a default/require on "_format" to "html" |
| 70 | + * @param Boolean $addLocalePattern whether to add "/{_locale}" to the route pattern |
| 71 | + */ |
| 72 | + public function __construct($addFormatPattern = false, $addLocalePattern = true) |
| 73 | + { |
| 74 | + parent::__construct($addFormatPattern); |
| 75 | + $this->addLocalePattern = $addLocalePattern; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public function getLocale() |
| 80 | + { |
| 81 | + return $this->locale; |
| 82 | + } |
| 83 | + |
| 84 | + public function setLocale($locale) |
| 85 | + { |
| 86 | + $this->locale = $locale; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * {@inheritDoc} |
| 91 | + * |
| 92 | + * automatically prepend the _locale to the pattern |
| 93 | + * |
| 94 | + * @see MultilangRouteProvider::getCandidates() |
| 95 | + */ |
| 96 | + public function getStaticPrefix() |
| 97 | + { |
| 98 | + if (!$this->addLocalePattern) { |
| 99 | + return parent::getStaticPrefix(); |
| 100 | + } |
| 101 | + |
| 102 | + $prefix = $this->getPrefix(); |
| 103 | + $path = substr(parent::getId(), strlen($prefix)); |
| 104 | + $path = $prefix.'/{_locale}'.$path; |
| 105 | + |
| 106 | + return $this->generateStaticPrefix($path, $this->idPrefix); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@inheritDoc} |
| 111 | + */ |
| 112 | + public function getDate() |
| 113 | + { |
| 114 | + return $this->publishStartDate ? $this->publishStartDate : $this->createDate; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@inheritDoc} |
| 119 | + */ |
| 120 | + public function getPublishStartDate() |
| 121 | + { |
| 122 | + return $this->publishStartDate; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * {@inheritDoc} |
| 127 | + */ |
| 128 | + public function setPublishStartDate(\DateTime $publishStartDate = null) |
| 129 | + { |
| 130 | + $this->publishStartDate = $publishStartDate; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * {@inheritDoc} |
| 135 | + */ |
| 136 | + public function getPublishEndDate() |
| 137 | + { |
| 138 | + return $this->publishEndDate; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * {@inheritDoc} |
| 143 | + */ |
| 144 | + public function setPublishEndDate(\DateTime $publishEndDate = null) |
| 145 | + { |
| 146 | + $this->publishEndDate = $publishEndDate; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * {@inheritDoc} |
| 151 | + */ |
| 152 | + public function isPublishable() |
| 153 | + { |
| 154 | + return $this->publishable; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * {@inheritDoc} |
| 159 | + */ |
| 160 | + public function setPublishable($publishable) |
| 161 | + { |
| 162 | + $this->publishable = $publishable; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Content method: Get tags of this page |
| 167 | + * |
| 168 | + * @return \Traversable list of tag strings |
| 169 | + */ |
| 170 | + public function getTags() |
| 171 | + { |
| 172 | + return $this->tags; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Content method: Set tags of this page |
| 177 | + * |
| 178 | + * @param $tags \Traversable list of tag strings |
| 179 | + */ |
| 180 | + public function setTags($tags) |
| 181 | + { |
| 182 | + $this->tags = $tags; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Get extras - a flat key-value hashmap |
| 187 | + * |
| 188 | + * @return array with only string values |
| 189 | + */ |
| 190 | + public function getExtras() |
| 191 | + { |
| 192 | + return $this->extras; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Set extras - applications can store additional information on a page |
| 197 | + * without needing to extend the document class. |
| 198 | + * |
| 199 | + * @param array $extras a flat key-value hashmap. The values are cast to |
| 200 | + * string as PHPCR stores multivalue data with only one data type for |
| 201 | + * all values. |
| 202 | + */ |
| 203 | + public function setExtras($extras) |
| 204 | + { |
| 205 | + foreach ($extras as $key => $value) { |
| 206 | + $extras[$key] = (string) $value; |
| 207 | + } |
| 208 | + |
| 209 | + $this->extras = $extras; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Add a single key - value pair to extras |
| 214 | + * |
| 215 | + * @param string $key |
| 216 | + * @param string $value - if this is not a string it is cast to one |
| 217 | + */ |
| 218 | + public function addExtra($key, $value) |
| 219 | + { |
| 220 | + $this->extras[$key] = (string) $value; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Remove a single key - value pair from extras, if it was set. |
| 225 | + * |
| 226 | + * @param string $key |
| 227 | + */ |
| 228 | + public function removeExtra($key) |
| 229 | + { |
| 230 | + if (array_key_exists($key, $this->extras)) { |
| 231 | + unset($this->extras[$key]); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Return a single extras value for the provided key or the $default if |
| 237 | + * the key is not defined. |
| 238 | + * |
| 239 | + * @param string $key |
| 240 | + * @param string|null $default |
| 241 | + * |
| 242 | + * @return string|null The value at $key or if not existing $default |
| 243 | + */ |
| 244 | + public function getExtra($key, $default = null) |
| 245 | + { |
| 246 | + return array_key_exists($key, $this->extras) ? $this->extras[$key] : $default; |
| 247 | + } |
| 248 | +} |
0 commit comments