|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Content. |
| 4 | + * |
| 5 | + * @copyright Axel Guckelsberger (Zikula) |
| 6 | + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
| 7 | + * @author Axel Guckelsberger <[email protected]>. |
| 8 | + * @link https://ziku.la |
| 9 | + * @version Generated by ModuleStudio 1.4.0 (https://modulestudio.de). |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Zikula\ContentModule\Needle\Base; |
| 13 | + |
| 14 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 15 | +use Symfony\Component\Routing\RouterInterface; |
| 16 | +use Zikula\Common\Translator\TranslatorInterface; |
| 17 | +use Zikula\ContentModule\Entity\Factory\EntityFactory; |
| 18 | +use Zikula\ContentModule\Helper\EntityDisplayHelper; |
| 19 | +use Zikula\ContentModule\Helper\PermissionHelper; |
| 20 | + |
| 21 | +/** |
| 22 | + * PageNeedle base class. |
| 23 | + */ |
| 24 | +abstract class AbstractPageNeedle |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Translator instance |
| 28 | + * |
| 29 | + * @var TranslatorInterface |
| 30 | + */ |
| 31 | + protected $translator; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var RouterInterface |
| 35 | + */ |
| 36 | + protected $router; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var PermissionHelper |
| 40 | + */ |
| 41 | + protected $permissionHelper; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var EntityFactory |
| 45 | + */ |
| 46 | + protected $entityFactory; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var EntityDisplayHelper |
| 50 | + */ |
| 51 | + protected $entityDisplayHelper; |
| 52 | + |
| 53 | + /** |
| 54 | + * Bundle name |
| 55 | + * |
| 56 | + * @var string |
| 57 | + */ |
| 58 | + protected $bundleName; |
| 59 | + |
| 60 | + /** |
| 61 | + * The name of this needle |
| 62 | + * |
| 63 | + * @var string |
| 64 | + */ |
| 65 | + protected $name; |
| 66 | + |
| 67 | + /** |
| 68 | + * PageNeedle constructor. |
| 69 | + * |
| 70 | + * @param TranslatorInterface $translator |
| 71 | + * @param RouterInterface $router |
| 72 | + * @param PermissionHelper $permissionHelper |
| 73 | + * @param EntityFactory $entityFactory |
| 74 | + * @param EntityDisplayHelper $entityDisplayHelper |
| 75 | + */ |
| 76 | + public function __construct( |
| 77 | + TranslatorInterface $translator, |
| 78 | + RouterInterface $router, |
| 79 | + PermissionHelper $permissionHelper, |
| 80 | + EntityFactory $entityFactory, |
| 81 | + EntityDisplayHelper $entityDisplayHelper |
| 82 | + ) { |
| 83 | + $this->translator = $translator; |
| 84 | + $this->router = $router; |
| 85 | + $this->permissionHelper = $permissionHelper; |
| 86 | + $this->entityFactory = $entityFactory; |
| 87 | + $this->entityDisplayHelper = $entityDisplayHelper; |
| 88 | + |
| 89 | + $nsParts = explode('\\', get_class($this)); |
| 90 | + $vendor = $nsParts[0]; |
| 91 | + $nameAndType = $nsParts[1]; |
| 92 | + |
| 93 | + $this->bundleName = $vendor . $nameAndType; |
| 94 | + $this->name = str_replace('Needle', '', array_pop($nsParts)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns the bundle name. |
| 99 | + * |
| 100 | + * @return string |
| 101 | + */ |
| 102 | + public function getBundleName() |
| 103 | + { |
| 104 | + return $this->bundleName; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns the name of this needle. |
| 109 | + * |
| 110 | + * @return string |
| 111 | + */ |
| 112 | + public function getName() |
| 113 | + { |
| 114 | + return $this->name; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns the icon name (FontAwesome icon code suffix, e.g. "pencil"). |
| 119 | + * |
| 120 | + * @return string |
| 121 | + */ |
| 122 | + public function getIcon() |
| 123 | + { |
| 124 | + return 'circle-o'; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the title of this needle. |
| 129 | + * |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + public function getTitle() |
| 133 | + { |
| 134 | + return $this->translator->__('Pages', 'zikulacontentmodule'); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Returns the description of this needle. |
| 139 | + * |
| 140 | + * @return string |
| 141 | + */ |
| 142 | + public function getDescription() |
| 143 | + { |
| 144 | + return $this->translator->__('Links to the list of pages and specific pages.', 'zikulacontentmodule'); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns usage information shown on settings page. |
| 149 | + * |
| 150 | + * @return string |
| 151 | + */ |
| 152 | + public function getUsageInfo() |
| 153 | + { |
| 154 | + return 'CONTENT{PAGES|PAGE-pageId}'; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns whether this needle is active or not. |
| 159 | + * |
| 160 | + * @return boolean |
| 161 | + */ |
| 162 | + public function isActive() |
| 163 | + { |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns whether this needle is case sensitive or not. |
| 169 | + * |
| 170 | + * @return boolean |
| 171 | + */ |
| 172 | + public function isCaseSensitive() |
| 173 | + { |
| 174 | + return true; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Returns the needle subject entries. |
| 179 | + * |
| 180 | + * @return string[] |
| 181 | + */ |
| 182 | + public function getSubjects() |
| 183 | + { |
| 184 | + return ['CONTENTPAGES', 'CONTENTPAGE-']; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Applies the needle functionality. |
| 189 | + * |
| 190 | + * @param string $needleId |
| 191 | + * @param string $needleText |
| 192 | + * |
| 193 | + * @return string Replaced value for the needle |
| 194 | + */ |
| 195 | + public function apply($needleId, $needleText) |
| 196 | + { |
| 197 | + // cache the results |
| 198 | + static $cache; |
| 199 | + if (!isset($cache)) { |
| 200 | + $cache = []; |
| 201 | + } |
| 202 | + |
| 203 | + if (isset($cache[$needleId])) { |
| 204 | + // needle is already in cache array |
| 205 | + return $cache[$needleId]; |
| 206 | + } |
| 207 | + |
| 208 | + // strip application prefix from needle |
| 209 | + $needleText = str_replace('CONTENT', '', $needleText); |
| 210 | + |
| 211 | + if ('PAGES' == $needleText) { |
| 212 | + if (!$this->permissionHelper->hasComponentPermission('page', ACCESS_READ)) { |
| 213 | + $cache[$needleId] = ''; |
| 214 | + } else { |
| 215 | + $cache[$needleId] = '<a href="' . $this->router->generate('zikulacontentmodule_page_view', [], UrlGeneratorInterface::ABSOLUTE_URL) . '" title="' . $this->translator->__('View pages', 'zikulacontentmodule') . '">' . $this->translator->__('Pages', 'zikulacontentmodule') . '</a>'; |
| 216 | + } |
| 217 | + |
| 218 | + return $cache[$needleId]; |
| 219 | + } |
| 220 | + |
| 221 | + $entityId = intval($needleId); |
| 222 | + if (!$entityId) { |
| 223 | + $cache[$needleId] = ''; |
| 224 | + |
| 225 | + return $cache[$needleId]; |
| 226 | + } |
| 227 | + |
| 228 | + $repository = $this->entityFactory->getRepository('page'); |
| 229 | + $entity = $repository->selectById($entityId, false); |
| 230 | + if (null === $entity) { |
| 231 | + $cache[$needleId] = '<em>' . $this->translator->__f('Page with id %id% could not be found', ['%id%' => $entityId], 'zikulacontentmodule') . '</em>'; |
| 232 | + |
| 233 | + return $cache[$needleId]; |
| 234 | + } |
| 235 | + |
| 236 | + if (!$this->permissionHelper->mayRead($entity)) { |
| 237 | + $cache[$needleId] = ''; |
| 238 | + |
| 239 | + return $cache[$needleId]; |
| 240 | + } |
| 241 | + |
| 242 | + $title = $this->entityDisplayHelper->getFormattedTitle($entity); |
| 243 | + $cache[$needleId] = '<a href="' . $this->router->generate('zikulacontentmodule_page_display', $entity->createUrlArgs(), UrlGeneratorInterface::ABSOLUTE_URL) . '" title="' . str_replace('"', '', $title) . '">' . $title . '</a>'; |
| 244 | + |
| 245 | + return $cache[$needleId]; |
| 246 | + } |
| 247 | +} |
0 commit comments