/mod_news_pro_gk5/admin/elements/vmshoppergroups.php

https://bitbucket.org/ghazalp/news-show-pro-gk5 · PHP · 118 lines · 76 code · 15 blank · 27 comment · 22 complexity · 58b202ef77254731cb5ff4ece59a1a4e MD5 · raw file

  1. <?php
  2. /**
  3. * JElementVMMultiCategories - additional element for module XML file
  4. * @package News Show Pro GK5
  5. * @Copyright (C) 2009-2013 Gavick.com
  6. * @ All rights reserved
  7. * @ Joomla! is Free Software
  8. * @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
  9. * @version $Revision: 1.0 $
  10. **/
  11. // access denied
  12. defined('JPATH_BASE') or die();
  13. class JFormFieldVMShoppergroups extends JFormFieldList {
  14. // name of element
  15. protected $type = 'VMShopperGroups';
  16. var $options = array();
  17. protected function getInput() {
  18. // Initialize variables.
  19. $html = array();
  20. $attr = '';
  21. // Initialize some field attributes.
  22. $attr .= $this->element['class'] ? ' class="'.(string) $this->element['class'].'"' : '';
  23. // To avoid user's confusion, readonly="true" should imply disabled="true".
  24. if ( (string) $this->element['readonly'] == 'true' || (string) $this->element['disabled'] == 'true') {
  25. $attr .= ' disabled="disabled"';
  26. }
  27. $attr .= $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';
  28. $attr .= $this->multiple ? ' multiple="multiple"' : '';
  29. // Initialize JavaScript field attributes.
  30. $attr .= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';
  31. // Get the field options.
  32. $options = (array) $this->getOptions();
  33. // Create a read-only list (no name) with a hidden input to store the value.
  34. if ((string) $this->element['readonly'] == 'true') {
  35. $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
  36. $html[] = '<input type="hidden" name="'.$this->name.'" value="'.$this->value.'"/>';
  37. }
  38. // Create a regular list.
  39. else {
  40. if($options[0]!=''){
  41. $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
  42. } else {
  43. return '<select id="jform_params_vm_shoppergroups" style="display:none"></select><strong style="line-height: 2.6em">VirtueMart is not installed or any VirtueMart shopper groups are available.</strong>';
  44. }
  45. }
  46. return implode($html);
  47. }
  48. protected function getOptions() {
  49. // Initialize variables.
  50. $session = JFactory::getSession();
  51. $attr = '';
  52. // Initialize some field attributes.
  53. $attr .= $this->element['class'] ? ' class="'.(string) $this->element['class'].'"' : '';
  54. // To avoid user's confusion, readonly="true" should imply disabled="true".
  55. if ( (string) $this->element['readonly'] == 'true' || (string) $this->element['disabled'] == 'true') {
  56. $attr .= ' disabled="disabled"';
  57. }
  58. $attr .= $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';
  59. $attr .= $this->multiple ? ' multiple="multiple"' : '';
  60. // Initialize JavaScript field attributes.
  61. $attr .= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';
  62. $db = JFactory::getDBO();
  63. // generating query
  64. $db->setQuery("SELECT sg.shopper_group_name AS name, sg.virtuemart_shoppergroup_id AS id FROM #__virtuemart_shoppergroups AS sg ORDER BY sg.shopper_group_name ASC");
  65. // getting results
  66. $results = $db->loadObjectList();
  67. if(count($results)){
  68. // iterating
  69. $temp_options = array();
  70. array_push($temp_options, array(-1, 'none', 0));
  71. foreach ($results as $item) {
  72. array_push($temp_options, array($item->id, $item->name, 0));
  73. }
  74. foreach ($temp_options as $option) {
  75. if($option[2] == 0) {
  76. $this->options[] = JHtml::_('select.option', $option[0], $option[1]);
  77. $this->recursive_options($temp_options, 1, $option[0]);
  78. }
  79. }
  80. return $this->options;
  81. } else {
  82. return $this->options;
  83. }
  84. }
  85. // bind function to save
  86. function bind( $array, $ignore = '' ) {
  87. if (key_exists( 'field-name', $array ) && is_array( $array['field-name'] )) {
  88. $array['field-name'] = implode( ',', $array['field-name'] );
  89. }
  90. return parent::bind( $array, $ignore );
  91. }
  92. function recursive_options($temp_options, $level, $parent){
  93. foreach ($temp_options as $option) {
  94. if($option[2] == $parent) {
  95. $level_string = '';
  96. for($i = 0; $i < $level; $i++) $level_string .= '- - ';
  97. $this->options[] = JHtml::_('select.option', $option[0], $level_string . $option[1]);
  98. $this->recursive_options($temp_options, $level+1, $option[0]);
  99. }
  100. }
  101. }
  102. }
  103. // EOF