/kernel/common/ezwordtoimageoperator.php

https://github.com/GunioRobot/ezpublish · PHP · 488 lines · 358 code · 50 blank · 80 comment · 62 complexity · 22c198cef616fdb02b527d8183cc707e MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZWordtoimageoperator class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZWordToImageOperator ezwordtoimageoperator.php
  12. \brief The class eZWordToImageOperator does
  13. */
  14. class eZWordToImageOperator
  15. {
  16. /*!
  17. Initializes the object with the name $name, default is "wash".
  18. */
  19. function eZWordToImageOperator()
  20. {
  21. $this->Operators = array( "wordtoimage",
  22. "mimetype_icon", "class_icon", "classgroup_icon", "action_icon", "icon",
  23. "flag_icon", "icon_info" );
  24. $this->IconInfo = array();
  25. }
  26. /*!
  27. Returns the template operators.
  28. */
  29. function operatorList()
  30. {
  31. return $this->Operators;
  32. }
  33. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
  34. {
  35. switch ( $operatorName )
  36. {
  37. case "wordtoimage":
  38. {
  39. $ini = eZINI::instance("wordtoimage.ini");
  40. $iconRoot = $ini->variable( 'WordToImageSettings', 'IconRoot' );
  41. $replaceText = $ini->variable( 'WordToImageSettings', 'ReplaceText' );
  42. $replaceIcon = $ini->variable( 'WordToImageSettings', 'ReplaceIcon' );
  43. $wwwDirPrefix = "";
  44. if ( strlen( eZSys::wwwDir() ) > 0 )
  45. $wwwDirPrefix = eZSys::wwwDir() . "/";
  46. foreach( $replaceIcon as $icon )
  47. {
  48. // Issue 015718, constructing alt text from icon name
  49. $aReplaceIconName = explode( '.', $icon );
  50. $altText = $aReplaceIconName[0];
  51. $icons[] = '<img src="' . $wwwDirPrefix . $iconRoot .'/' . $icon . '" alt="'.$altText.'"/>';
  52. }
  53. $operatorValue = str_replace( $replaceText, $icons, $operatorValue );
  54. } break;
  55. // icon_info( <type> ) => array() containing:
  56. // - repository - Repository path
  57. // - theme - Theme name
  58. // - theme_path - Theme path
  59. // - size_path_list - Associative array of size paths
  60. // - size_info_list - Associative array of size info (width and height)
  61. // - icons - Array of icon files, relative to theme and size path
  62. // - default - Default icon file, relative to theme and size path
  63. case 'icon_info':
  64. {
  65. if ( !isset( $operatorParameters[0] ) )
  66. {
  67. $tpl->missingParameter( $operatorName, 'type' );
  68. return;
  69. }
  70. $type = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace );
  71. // Check if we have it cached
  72. if ( isset( $this->IconInfo[$type] ) )
  73. {
  74. $operatorValue = $this->IconInfo[$type];
  75. return;
  76. }
  77. $ini = eZINI::instance( 'icon.ini' );
  78. $repository = $ini->variable( 'IconSettings', 'Repository' );
  79. $theme = $ini->variable( 'IconSettings', 'Theme' );
  80. $groups = array( 'mimetype' => 'MimeIcons',
  81. 'class' => 'ClassIcons',
  82. 'classgroup' => 'ClassGroupIcons',
  83. 'action' => 'ActionIcons',
  84. 'icon' => 'Icons' );
  85. $configGroup = $groups[$type];
  86. $mapNames = array( 'mimetype' => 'MimeMap',
  87. 'class' => 'ClassMap',
  88. 'classgroup' => 'ClassGroupMap',
  89. 'action' => 'ActionMap',
  90. 'icon' => 'IconMap' );
  91. $mapName = $mapNames[$type];
  92. // Check if the specific icon type has a theme setting
  93. if ( $ini->hasVariable( $configGroup, 'Theme' ) )
  94. {
  95. $theme = $ini->variable( $configGroup, 'Theme' );
  96. }
  97. // Load icon settings from the theme
  98. $themeINI = eZINI::instance( 'icon.ini', $repository . '/' . $theme );
  99. $sizes = $themeINI->variable( 'IconSettings', 'Sizes' );
  100. if ( $ini->hasVariable( 'IconSettings', 'Sizes' ) )
  101. {
  102. $sizes = array_merge( $sizes,
  103. $ini->variable( 'IconSettings', 'Sizes' ) );
  104. }
  105. $sizePathList = array();
  106. $sizeInfoList = array();
  107. if ( is_array( $sizes ) )
  108. {
  109. foreach ( $sizes as $key => $size )
  110. {
  111. $pathDivider = strpos( $size, ';' );
  112. if ( $pathDivider !== false )
  113. {
  114. $sizePath = substr( $size, $pathDivider + 1 );
  115. $size = substr( $size, 0, $pathDivider );
  116. }
  117. else
  118. {
  119. $sizePath = $size;
  120. }
  121. $width = false;
  122. $height = false;
  123. $xDivider = strpos( $size, 'x' );
  124. if ( $xDivider !== false )
  125. {
  126. $width = (int)substr( $size, 0, $xDivider );
  127. $height = (int)substr( $size, $xDivider + 1 );
  128. }
  129. $sizePathList[$key] = $sizePath;
  130. $sizeInfoList[$key] = array( $width, $height );
  131. }
  132. }
  133. $map = array();
  134. // Load mapping from theme
  135. if ( $themeINI->hasVariable( $configGroup, $mapName ) )
  136. {
  137. $map = array_merge( $map,
  138. $themeINI->variable( $configGroup, $mapName ) );
  139. }
  140. // Load override mappings if they exist
  141. if ( $ini->hasVariable( $configGroup, $mapName ) )
  142. {
  143. $map = array_merge( $map,
  144. $ini->variable( $configGroup, $mapName ) );
  145. }
  146. $default = false;
  147. if ( $themeINI->hasVariable( $configGroup, 'Default' ) )
  148. $default = $themeINI->variable( $configGroup, 'Default' );
  149. if ( $ini->hasVariable( $configGroup, 'Default' ) )
  150. $default = $ini->variable( $configGroup, 'Default' );
  151. // Build return value
  152. $iconInfo = array( 'repository' => $repository,
  153. 'theme' => $theme,
  154. 'theme_path' => $repository . '/' . $theme,
  155. 'size_path_list' => $sizePathList,
  156. 'size_info_list' => $sizeInfoList,
  157. 'icons' => $map,
  158. 'default' => $default );
  159. $this->IconInfo[$type] = $iconInfo;
  160. $operatorValue = $iconInfo;
  161. } break;
  162. case 'flag_icon':
  163. {
  164. $ini = eZINI::instance( 'icon.ini' );
  165. $repository = $ini->variable( 'FlagIcons', 'Repository' );
  166. $theme = $ini->variable( 'FlagIcons', 'Theme' );
  167. // Load icon settings from the theme
  168. $themeINI = eZINI::instance( 'icon.ini', $repository . '/' . $theme );
  169. $iconFormat = $themeINI->variable( 'FlagIcons', 'IconFormat' );
  170. if ( $ini->hasVariable( 'FlagIcons', 'IconFormat' ) )
  171. {
  172. $iconFormat = $ini->variable( 'FlagIcons', 'IconFormat' );
  173. }
  174. $icon = $operatorValue . '.' . $iconFormat;
  175. $iconPath = $repository . '/' . $theme . '/' . $icon;
  176. if ( !is_readable( $iconPath ) )
  177. {
  178. $defaultIcon = $themeINI->variable( 'FlagIcons', 'DefaultIcon' );
  179. $iconPath = $repository . '/' . $theme . '/' . $defaultIcon . '.' . $iconFormat;
  180. }
  181. if ( strlen( eZSys::wwwDir() ) > 0 )
  182. $wwwDirPrefix = eZSys::wwwDir() . '/';
  183. else
  184. $wwwDirPrefix = '/';
  185. $operatorValue = $wwwDirPrefix . $iconPath;
  186. } break;
  187. case 'mimetype_icon':
  188. case 'class_icon':
  189. case 'classgroup_icon':
  190. case 'action_icon':
  191. case 'icon':
  192. {
  193. // Determine whether we should return only the image URI instead of the whole HTML code.
  194. if ( isset( $operatorParameters[2] ) )
  195. $returnURIOnly = $tpl->elementValue( $operatorParameters[2], $rootNamespace, $currentNamespace );
  196. else
  197. $returnURIOnly = false;
  198. $ini = eZINI::instance( 'icon.ini' );
  199. $repository = $ini->variable( 'IconSettings', 'Repository' );
  200. $theme = $ini->variable( 'IconSettings', 'Theme' );
  201. $groups = array( 'mimetype_icon' => 'MimeIcons',
  202. 'class_icon' => 'ClassIcons',
  203. 'classgroup_icon' => 'ClassGroupIcons',
  204. 'action_icon' => 'ActionIcons',
  205. 'icon' => 'Icons' );
  206. $configGroup = $groups[$operatorName];
  207. // Check if the specific icon type has a theme setting
  208. if ( $ini->hasVariable( $configGroup, 'Theme' ) )
  209. {
  210. $theme = $ini->variable( $configGroup, 'Theme' );
  211. }
  212. // Load icon settings from the theme
  213. $themeINI = eZINI::instance( 'icon.ini', $repository . '/' . $theme );
  214. if ( isset( $operatorParameters[0] ) )
  215. {
  216. $sizeName = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace );
  217. }
  218. else
  219. {
  220. $sizeName = $ini->variable( 'IconSettings', 'Size' );
  221. // Check if the specific icon type has a size setting
  222. if ( $ini->hasVariable( $configGroup, 'Size' ) )
  223. {
  224. $theme = $ini->variable( $configGroup, 'Size' );
  225. }
  226. }
  227. $sizes = $themeINI->variable( 'IconSettings', 'Sizes' );
  228. if ( $ini->hasVariable( 'IconSettings', 'Sizes' ) )
  229. {
  230. $sizes = array_merge( $sizes,
  231. $ini->variable( 'IconSettings', 'Sizes' ) );
  232. }
  233. if ( isset( $sizes[$sizeName] ) )
  234. {
  235. $size = $sizes[$sizeName];
  236. }
  237. else
  238. {
  239. $size = $sizes[0];
  240. }
  241. $pathDivider = strpos( $size, ';' );
  242. if ( $pathDivider !== false )
  243. {
  244. $sizePath = substr( $size, $pathDivider + 1 );
  245. $size = substr( $size, 0, $pathDivider );
  246. }
  247. else
  248. {
  249. $sizePath = $size;
  250. }
  251. $width = false;
  252. $height = false;
  253. $xDivider = strpos( $size, 'x' );
  254. if ( $xDivider !== false )
  255. {
  256. $width = (int)substr( $size, 0, $xDivider );
  257. $height = (int)substr( $size, $xDivider + 1 );
  258. }
  259. if ( isset( $operatorParameters[1] ) )
  260. {
  261. $altText = $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace );
  262. }
  263. else
  264. {
  265. $altText = $operatorValue;
  266. }
  267. if ( $operatorName == 'mimetype_icon' )
  268. {
  269. $icon = $this->iconGroupMapping( $ini, $themeINI,
  270. 'MimeIcons', 'MimeMap',
  271. strtolower( $operatorValue ) );
  272. }
  273. else if ( $operatorName == 'class_icon' )
  274. {
  275. $icon = $this->iconDirectMapping( $ini, $themeINI,
  276. 'ClassIcons', 'ClassMap',
  277. strtolower( $operatorValue ) );
  278. }
  279. else if ( $operatorName == 'classgroup_icon' )
  280. {
  281. $icon = $this->iconDirectMapping( $ini, $themeINI,
  282. 'ClassGroupIcons', 'ClassGroupMap',
  283. strtolower( $operatorValue ) );
  284. }
  285. else if ( $operatorName == 'action_icon' )
  286. {
  287. $icon = $this->iconDirectMapping( $ini, $themeINI,
  288. 'ActionIcons', 'ActionMap',
  289. strtolower( $operatorValue ) );
  290. }
  291. else if ( $operatorName == 'icon' )
  292. {
  293. $icon = $this->iconDirectMapping( $ini, $themeINI,
  294. 'Icons', 'IconMap',
  295. strtolower( $operatorValue ) );
  296. }
  297. $iconPath = '/' . $repository . '/' . $theme;
  298. $iconPath .= '/' . $sizePath;
  299. $iconPath .= '/' . $icon;
  300. $wwwDirPrefix = "";
  301. if ( strlen( eZSys::wwwDir() ) > 0 )
  302. $wwwDirPrefix = eZSys::wwwDir();
  303. $sizeText = '';
  304. if ( $width !== false and $height !== false )
  305. {
  306. $sizeText = ' width="' . $width . '" height="' . $height . '"';
  307. }
  308. // The class will be detected by ezpngfix.js, which will force alpha blending in IE.
  309. if ( ( !isset( $sizeName ) || $sizeName == 'normal' || $sizeName == 'original' ) && strstr( strtolower( $iconPath ), ".png" ) )
  310. {
  311. $class = 'class="transparent-png-icon" ';
  312. }
  313. else
  314. {
  315. $class = '';
  316. }
  317. if ( $returnURIOnly )
  318. $operatorValue = $wwwDirPrefix . $iconPath;
  319. else
  320. $operatorValue = '<img ' . $class . 'src="' . $wwwDirPrefix . $iconPath . '"' . $sizeText . ' alt="' . htmlspecialchars( $altText ) . '" title="' . htmlspecialchars( $altText ) . '" />';
  321. } break;
  322. default:
  323. {
  324. eZDebug::writeError( "Unknown operator: $operatorName", "ezwordtoimageoperator.php" );
  325. }
  326. }
  327. }
  328. /*!
  329. \private
  330. Tries to find icon file by considering \a $matchItem as a single value.
  331. It will first try to match the whole \a $matchItem value in the mapping table.
  332. \return The relative path to the icon file.
  333. Example
  334. \code
  335. $icon = $this->iconDirectMapping( $ini, $themeINI, 'ClassIcons', 'ClassMap', 'Folder' );
  336. \endcode
  337. \sa iconGroupMapping
  338. */
  339. function iconDirectMapping( &$ini, &$themeINI, $iniGroup, $mapName, $matchItem )
  340. {
  341. $map = array();
  342. // Load mapping from theme
  343. if ( $themeINI->hasVariable( $iniGroup, $mapName ) )
  344. {
  345. $map = array_merge( $map,
  346. $themeINI->variable( $iniGroup, $mapName ) );
  347. }
  348. // Load override mappings if they exist
  349. if ( $ini->hasVariable( $iniGroup, $mapName ) )
  350. {
  351. $map = array_merge( $map,
  352. $ini->variable( $iniGroup, $mapName ) );
  353. }
  354. $icon = false;
  355. if ( isset( $map[$matchItem] ) )
  356. {
  357. $icon = $map[$matchItem];
  358. }
  359. if ( $icon === false )
  360. {
  361. if ( $themeINI->hasVariable( $iniGroup, 'Default' ) )
  362. $icon = $themeINI->variable( $iniGroup, 'Default' );
  363. if ( $ini->hasVariable( $iniGroup, 'Default' ) )
  364. $icon = $ini->variable( $iniGroup, 'Default' );
  365. }
  366. return $icon;
  367. }
  368. /*!
  369. \private
  370. Tries to find icon file by considering \a $matchItem as a group,
  371. split into two parts and separated by a slash.
  372. It will first try to match the whole \a $matchItem value and then
  373. the group name.
  374. \return The relative path to the icon file.
  375. Example
  376. \code
  377. $icon = $this->iconGroupMapping( $ini, $themeINI, 'MimeIcons', 'MimeMap', 'image/jpeg' );
  378. \endcode
  379. \sa iconDirectMapping
  380. */
  381. function iconGroupMapping( &$ini, &$themeINI, $iniGroup, $mapName, $matchItem )
  382. {
  383. $map = array();
  384. // Load mapping from theme
  385. if ( $themeINI->hasVariable( $iniGroup, $mapName ) )
  386. {
  387. $map = array_merge( $map,
  388. $themeINI->variable( $iniGroup, $mapName ) );
  389. }
  390. // Load override mappings if they exist
  391. if ( $ini->hasVariable( $iniGroup, $mapName ) )
  392. {
  393. $map = array_merge( $map,
  394. $ini->variable( $iniGroup, $mapName ) );
  395. }
  396. $icon = false;
  397. // See if we have a match for the whole match item
  398. if ( isset( $map[$matchItem] ) )
  399. {
  400. $icon = $map[$matchItem];
  401. }
  402. else
  403. {
  404. // If not we have to check the group (first part)
  405. $pos = strpos( $matchItem, '/' );
  406. if ( $pos !== false )
  407. {
  408. $mimeGroup = substr( $matchItem, 0, $pos );
  409. if ( isset( $map[$mimeGroup] ) )
  410. {
  411. $icon = $map[$mimeGroup];
  412. }
  413. }
  414. }
  415. // No icon? If so use default
  416. if ( $icon === false )
  417. {
  418. if ( $themeINI->hasVariable( $iniGroup, 'Default' ) )
  419. $icon = $themeINI->variable( $iniGroup, 'Default' );
  420. if ( $ini->hasVariable( $iniGroup, 'Default' ) )
  421. $icon = $ini->variable( $iniGroup, 'Default' );
  422. }
  423. return $icon;
  424. }
  425. /// \privatesection
  426. public $Operators;
  427. public $IconInfo;
  428. }
  429. ?>