PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/widget_helper.php

http://github.com/imagecms/ImageCMS
PHP | 170 lines | 104 code | 37 blank | 29 comment | 25 complexity | eeda8554b5393bdf08c05aea4cf83f89 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. if (!defined('BASEPATH')) {
  3. exit('No direct script access allowed');
  4. }
  5. /**
  6. * ImageCMS
  7. * Widgets helper
  8. * @property CI_DB_active_record $db
  9. */
  10. if (!function_exists('widget')) {
  11. /**
  12. * Run widget
  13. *
  14. * @param bool|string $name - widget name
  15. * @param bool|int $cache - cache ttl in minutes
  16. * @return array
  17. */
  18. function widget($name = FALSE, $cache = FALSE) {
  19. $ci = &get_instance();
  20. $widget = [];
  21. $query = $ci->db->limit(1)->get_where('widgets', ['name' => $name]);
  22. if ($query->num_rows() == 1) {
  23. $widget = $query->row_array();
  24. } else {
  25. log_message('error', 'Can\'t run widget <b>' . $name . '</b>');
  26. }
  27. if (($data = $ci->cache->fetch('widget' . $name, 'widgets')) != FALSE AND $cache != FALSE) {
  28. return $data;
  29. } else {
  30. $widget['settings'] = unserialize($widget['settings']);
  31. switch ($widget['type']) {
  32. case 'module':
  33. $subpath = isset($widget['settings']['subpath']) ? $widget['settings']['subpath'] . '/' : '';
  34. $method = $widget['method'];
  35. $result = $ci->load->module($widget['data'] . '/' . $subpath . $widget['data'] . '_widgets')->$method($widget);
  36. break;
  37. case 'html':
  38. $locale = MY_Controller::getCurrentLocale();
  39. $id = $widget['id'];
  40. $sql = "select * from widget_i18n where locale = '$locale' and id = '$id'";
  41. $w_i18 = $ci->db->query($sql)->row_array();
  42. $result = $w_i18['data'];
  43. break;
  44. }
  45. if ($cache != FALSE AND is_integer($cache)) {
  46. $ci->cache->store('widget' . $name, $result, $cache * 60, 'widgets');
  47. }
  48. return $result;
  49. }
  50. }
  51. }
  52. if (!function_exists('widget_ajax')) {
  53. /**
  54. * @param string $name
  55. * @param string $container
  56. */
  57. function widget_ajax($name, $container) {
  58. echo "
  59. <script type=text/javascript>
  60. $(window).load(function(){
  61. $.ajax({
  62. async : 'false',
  63. type : 'post',
  64. url : locale+'/shop/ajax/widget/$name',
  65. success : function(data){
  66. $('$container').html(data);
  67. $(document).trigger({type: 'widget_ajax', el: $('$container')})
  68. }
  69. })
  70. })
  71. </script>
  72. ";
  73. }
  74. }
  75. if (!function_exists('getWidgetName')) {
  76. /**
  77. *
  78. * @param string $name
  79. * @return string
  80. */
  81. function getWidgetName($name) {
  82. $ci = &get_instance();
  83. $query = $ci->db->limit(1)->get_where('widgets', ['name' => $name]);
  84. if ($query->num_rows() == 1) {
  85. $widget = $query->row_array();
  86. } else {
  87. log_message('error', 'Can\'t run widget <b>' . $name . '</b>');
  88. }
  89. $widget = unserialize($widget['settings']);
  90. return $widget['title'];
  91. }
  92. }
  93. if (!function_exists('getWidgeTitle')) {
  94. /**
  95. * @param string $name
  96. * @return string
  97. */
  98. function getWidgetTitle($name) {
  99. $ci = &get_instance();
  100. $locale = MY_Controller::getCurrentLocale();
  101. $query = $ci->db
  102. ->join('widget_i18n', 'widget_i18n.id=widgets.id AND locale="' . $locale . '"', 'left')
  103. ->get_where('widgets', ['name' => $name]);
  104. if ($query->num_rows() == 1) {
  105. $widget = $query->row_array();
  106. $title = $widget['title'];
  107. $settings = @unserialize($widget[settings]);
  108. if ($settings) {
  109. $title = $title ? : $settings['title'];
  110. }
  111. return $title;
  112. } else {
  113. log_message('error', 'Can\'t run widget <b>' . $name . '</b>');
  114. }
  115. return '';
  116. }
  117. }
  118. if (!function_exists('getProductViewsCount')) {
  119. /**
  120. * @return int
  121. */
  122. function getProductViewsCount() {
  123. $ci = &get_instance();
  124. $views = $ci->session->userdata('page');
  125. if ($views) {
  126. $count = count($views);
  127. } else {
  128. $count = 0;
  129. }
  130. return $count;
  131. }
  132. }
  133. /* End of widget_helper.php */