PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Meta/View/Helper/MetaHelper.php

https://github.com/kareypowell/croogo
PHP | 131 lines | 87 code | 14 blank | 30 comment | 10 complexity | f906e2b1a12d249fac3906e83d2435ff MD5 | raw file
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. /**
  4. * Meta Helper
  5. *
  6. * @category Meta.View/Helper
  7. * @package Croogo.Meta
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class MetaHelper extends AppHelper {
  14. /**
  15. * Helpers
  16. */
  17. public $helpers = array(
  18. 'Html',
  19. 'Form',
  20. );
  21. /**
  22. * beforeRender
  23. */
  24. public function beforeRender($viewFile) {
  25. if ($this->_View->Layout->isLoggedIn()) {
  26. return $this->_View->Croogo->adminScript('Meta.admin');
  27. }
  28. }
  29. /**
  30. * Meta tags
  31. *
  32. * @return string
  33. */
  34. public function meta($metaForLayout = array()) {
  35. $_metaForLayout = array();
  36. if (is_array(Configure::read('Meta'))) {
  37. $_metaForLayout = Configure::read('Meta');
  38. }
  39. if (count($metaForLayout) == 0 &&
  40. isset($this->_View->viewVars['node']['CustomFields']) &&
  41. count($this->_View->viewVars['node']['CustomFields']) > 0) {
  42. $metaForLayout = array();
  43. foreach ($this->_View->viewVars['node']['CustomFields'] as $key => $value) {
  44. if (strstr($key, 'meta_')) {
  45. $key = str_replace('meta_', '', $key);
  46. $metaForLayout[$key] = $value;
  47. }
  48. }
  49. }
  50. $metaForLayout = array_merge($_metaForLayout, $metaForLayout);
  51. $output = '';
  52. foreach ($metaForLayout as $name => $content) {
  53. if (is_array($content) && isset($content['content'])) {
  54. $attr = key($content);
  55. $attrValue = $content[$attr];
  56. $value = $content['content'];
  57. } else {
  58. $attr = 'name';
  59. $attrValue = $name;
  60. $value = $content;
  61. }
  62. $output .= '<meta ' . $attr . '="' . $attrValue . '" content="' . $value . '" />';
  63. }
  64. return $output;
  65. }
  66. /**
  67. * Meta field: with key/value fields
  68. *
  69. * @param string $key (optional) key
  70. * @param string $value (optional) value
  71. * @param integer $id (optional) ID of Meta
  72. * @param array $options (optional) options
  73. * @return string
  74. */
  75. public function field($key = '', $value = null, $id = null, $options = array()) {
  76. $_options = array(
  77. 'key' => array(
  78. 'label' => __d('croogo', 'Key'),
  79. 'value' => $key,
  80. 'class' => 'span12'
  81. ),
  82. 'value' => array(
  83. 'label' => __d('croogo', 'Value'),
  84. 'value' => $value,
  85. 'class' => 'span12',
  86. 'type' => 'textarea',
  87. 'rows' => 2,
  88. ),
  89. );
  90. $options = Hash::merge($_options, $options);
  91. $uuid = String::uuid();
  92. $fields = '';
  93. if ($id != null) {
  94. $fields .= $this->Form->input('Meta.' . $uuid . '.id', array('type' => 'hidden', 'value' => $id));
  95. $this->Form->unlockField('Meta.' . $uuid . '.id');
  96. }
  97. $fields .= $this->Form->input('Meta.' . $uuid . '.key', $options['key']);
  98. $fields .= $this->Form->input('Meta.' . $uuid . '.value', $options['value']);
  99. $this->Form->unlockField('Meta.' . $uuid . '.key');
  100. $this->Form->unlockField('Meta.' . $uuid . '.value');
  101. $fields = $this->Html->tag('div', $fields, array('class' => 'fields'));
  102. $id = is_null($id) ? $uuid : $id;
  103. $deleteUrl = array(
  104. 'admin' => true, 'plugin' => 'meta',
  105. 'controller' => 'meta', 'action' => 'delete_meta'
  106. );
  107. $deleteUrl[] = $id;
  108. $actions = $this->Html->link(
  109. __d('croogo', 'Remove'),
  110. $deleteUrl,
  111. array('class' => 'remove-meta', 'rel' => $id)
  112. );
  113. $actions = $this->Html->tag('div', $actions, array('class' => 'actions'));
  114. $output = $this->Html->tag('div', $actions . $fields, array('class' => 'meta'));
  115. return $output;
  116. }
  117. }