PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/legacy/viewplugins/function.array_pop.php

https://github.com/antoniom/core
PHP | 78 lines | 27 code | 10 blank | 41 comment | 10 complexity | 441cac295f74af4c68d95f36d87e517f MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula_View
  10. * @subpackage Template_Plugins
  11. *
  12. * Please see the NOTICE file distributed with this source code for further
  13. * information regarding copyright and licensing.
  14. */
  15. /**
  16. * Pop a field of an array and assign its value to a template variable.
  17. *
  18. * Available attributes:
  19. * - array (string) Name of the template array variable to process
  20. * - field (string) Name of the array field to assign then unset
  21. * - unset (bool) Flag to specify if the field must be unset or not (default: true)
  22. * - assign (string) Name of the assign variable to setup (optional)
  23. *
  24. * Example:
  25. *
  26. * Having an $objarray in our template, we want to process its fields in different
  27. * sections of the template, so we get the needed fields separately on the desired positions,
  28. * clearing the array in the process.
  29. *
  30. * For instance, the $hooks array resulted of notify the 'display_view' hooks, can be
  31. * processed individually using this plugin:
  32. *
  33. * <samp>{array_pop array='hooks' field='EZComments'}</samp>
  34. * <samp>{array_pop array='hooks' field='EZComments' assign='comments'}</samp>
  35. *
  36. * And display later the remaining ones with a foreach.
  37. *
  38. * @param array $params All attributes passed to this function from the template.
  39. * @param Zikula_View $view Reference to the {@link Zikula_View} object.
  40. *
  41. * @return mixed False on failure, void if the value is assigned, or the value extracted itself.
  42. */
  43. function smarty_function_array_pop($params, Zikula_View $view)
  44. {
  45. if (!isset($params['array']) || !$params['array']) {
  46. $view->trigger_error(__f('Error! in %1$s: the %2$s parameter must be specified.', array('assign_cache', 'var')));
  47. return false;
  48. }
  49. if (!isset($params['field']) || !$params['field']) {
  50. $view->trigger_error(__f('Error! in %1$s: the %2$s parameter must be specified.', array('assign_cache', 'value')));
  51. return false;
  52. }
  53. $unset = isset($params['unset']) ? (bool)$params['unset'] : true;
  54. $value = null;
  55. $array = $view->getTplVar($params['array']);
  56. if ($array && isset($array[$params['field']])) {
  57. $value = $array[$params['field']];
  58. if ($unset) {
  59. unset($array[$params['field']]);
  60. }
  61. $view->assign($params['array'], $array);
  62. }
  63. if (isset($params['assign']) && $params['assign']) {
  64. $view->assign($params['assign'], $value);
  65. } else {
  66. return $value;
  67. }
  68. }