1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203:
<?PHP
defined('ICMS_ROOT_PATH') or die();
class icms_form_elements_select_Image extends icms_form_elements_Select {
private $_optgroups = array();
private $_optgroupsID = array();
public function __construct($caption, $name, $value = NULL, $cat = NULL) {
parent::__construct($caption, $name, $value);
$this->addOptGroupArray($this->getImageList($cat));
}
public function addOptGroup($value = array(), $name = " ") {
$this->_optgroups[$name] = $value;
}
public function addOptGroupArray($options) {
if (is_array($options)) {
foreach ($options as $k=>$v) {
$this->addOptGroup($v, $k);
}
}
}
public function getImageList($cat = NULL) {
$ret = array();
if (!is_object(icms::$user)) {
$group = array(XOOPS_GROUP_ANONYMOUS);
} else {
$group =& icms::$user->getGroups();
}
$imgcat_handler = icms::handler('icms_image_category');
$catlist =& $imgcat_handler->getList($group, 'imgcat_read', 1);
if (is_array($cat) && count($catlist) > 0) {
foreach ($catlist as $k=>$v) {
if (!in_array($k, $cat)) {
unset($catlist[$k]);
}
}
} elseif (is_int($cat)) {
$catlist = array_key_exists($cat, $catlist) ? array($cat=>$catlist[$cat]) : array();
}
$image_handler = icms::handler('icms_image');
foreach ($catlist as $k=>$v) {
$this->_optgroupsID[$v] = $k;
$criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('imgcat_id', $k));
$criteria->add(new icms_db_criteria_Item('image_display', 1));
$total = $image_handler->getCount($criteria);
if ($total > 0) {
$imgcat =& $imgcat_handler->get($k);
$storetype = $imgcat->getVar('imgcat_storetype');
if ($storetype == 'db') {
$images =& $image_handler->getObjects($criteria, FALSE, TRUE);
} else {
$images =& $image_handler->getObjects($criteria, FALSE, FALSE);
}
foreach ($images as $i) {
if ($storetype == "db"){
$ret[$v]["/image.php?id=" . $i->getVar('image_id')] = $i->getVar('image_nicename');
} else {
$categ_path = $imgcat_handler->getCategFolder($imgcat);
$categ_path = str_replace(ICMS_ROOT_PATH, '', $categ_path);
$path = (substr($categ_path,-1) != '/') ? $categ_path . '/' : $categ_path;
$ret[$v][$path . $i->getVar('image_name')] = $i->getVar('image_nicename');
}
}
} else {
$ret[$v] = "";
}
}
return $ret;
}
public function getOptGroups() {
return $this->_optgroups;
}
public function getOptGroupsID() {
return $this->_optgroupsID;
}
public function render(){
if (!is_object(icms::$user)) {
$group = array(XOOPS_GROUP_ANONYMOUS);
} else {
$group =& icms::$user->getGroups();
}
$imgcat_handler = icms::handler('icms_image_category');
$catlist =& $imgcat_handler->getList($group, 'imgcat_write', 1);
$catlist_total = count($catlist);
$optIds = $this->getOptGroupsID();
$ret = "<select onchange='if(this.options[this.selectedIndex].value != \"\"){ document.getElementById(\""
. $this->getName() . "_img\").src=\"" . ICMS_URL . "\"+this.options[this.selectedIndex].value;}else{document.getElementById(\"" . $this->getName() . "_img\").src=\""
. ICMS_URL . "/images/blank.gif\";}' size='" . $this->getSize() . "'" . $this->getExtra() . "";
if ($this->isMultiple() != false) {
$ret .= " name='" . $this->getName() . "[]' id='" . $this->getName() . "[]' multiple='multiple'>\n";
} else {
$ret .= " name='" . $this->getName() . "' id='" . $this->getName() . "'>\n";
}
$ret .= "<option value=''>" . _SELECT . "</option>\n";
foreach ($this->getOptGroups() as $nome => $valores) {
$ret .= '\n<optgroup id="img_cat_' . $optIds[$nome] . '" label="' . $nome . '">';
if (is_array($valores)) {
foreach ($valores as $value => $name) {
$ret .= "<option value='" . htmlspecialchars($value, ENT_QUOTES) . "'";
if (count($this->getValue()) > 0 && in_array($value, $this->getValue())) {
$ret .= " selected='selected'";
$imagem = $value;
}
$ret .= ">" . $name . "</option>\n";
}
}
$ret .= '</optgroup>\n';
}
$browse_url = ICMS_URL."/modules/system/admin/images/browser.php";
$ret .= "</select>";
$ret .= ($catlist_total > 0)
? " <input type='button' value='" . _ADDIMAGE . "' onclick=\"window.open('$browse_url?target=" . $this->getName() . "','formImage','resizable=yes,scrollbars=yes,width=985,height=470,left='+(screen.availWidth/2-492)+',top='+(screen.availHeight/2-235)+'');return false;\">"
: "" ;
$ret .= "<br /><img id='" . $this->getName() . "_img' src='" . ((!empty($imagem)) ? ICMS_URL.$imagem : ICMS_URL . "/images/blank.gif") . "'>";
return $ret;
}
}