예제 #1
0
파일: item.php 프로젝트: ming-hai/XoopsCore
    /**
     * Build the images tab
     *
     * @param PublisherItem      $obj       data source
     * @param ContainerInterface $imagesTab add elements to this tab/form
     *
     * @return void
     */
    private function buildImagesTab(PublisherItem $obj, ContainerInterface $imagesTab)
    {
        $xoops = Xoops::getInstance();
        $group = $xoops->getUserGroups();
        // IMAGE
        if ($this->isGranted(_PUBLISHER_IMAGE_ITEM)) {
            $imgcat_handler = Images::getInstance()->getHandlerCategories();
            $image_handler = Images::getInstance()->getHandlerImages();
            $objimages = $obj->getImages();
            $mainarray = is_object($objimages['main']) ? array($objimages['main']) : array();
            $mergedimages = array_merge($mainarray, $objimages['others']);
            $objimage_array = array();
            /* @var $imageObj ImagesImage */
            foreach ($mergedimages as $imageObj) {
                $objimage_array[$imageObj->getVar('image_name')] = $imageObj->getVar('image_nicename');
            }
            $catlist = $imgcat_handler->getListByPermission($group, 'imgcat_read', 1);
            $catids = array_keys($catlist);
            $imageObjs = array();
            if (!empty($catids)) {
                $criteria = new CriteriaCompo(new Criteria('imgcat_id', '(' . implode(',', $catids) . ')', 'IN'));
                $criteria->add(new Criteria('image_display', 1));
                $criteria->setSort('image_nicename');
                $criteria->setOrder('ASC');
                $imageObjs = $image_handler->getObjects($criteria, true);
                unset($criteria);
            }
            $image_array = array();
            foreach ($imageObjs as $imageObj) {
                $image_array[$imageObj->getVar('image_name')] = $imageObj->getVar('image_nicename');
            }
            $image_array = array_diff($image_array, $objimage_array);
            $image_select = new Xoops\Form\Select('', 'image_notused', '', 5);
            $image_select->addOptionArray($image_array);
            $image_select->set('onchange', 'showImgSelected("image_display", "image_notused", "uploads/", "", "' . \XoopsBaseConfig::get('url') . '")');
            unset($image_array);
            $image_select2 = new Xoops\Form\Select('', 'image_item', '', 5, true);
            $image_select2->addOptionArray($objimage_array);
            $image_select2->set('onchange', 'publisher_updateSelectOption("image_item", "image_featured"), ' . 'showImgSelected("image_display", "image_item", "uploads/", "", "' . \XoopsBaseConfig::get('url') . '");');
            $buttonadd = new Xoops\Form\Button('', 'buttonadd', _CO_PUBLISHER_ADD);
            $buttonadd->set('onclick', 'publisher_appendSelectOption("image_notused", "image_item"), ' . 'publisher_updateSelectOption("image_item", "image_featured");');
            $buttonremove = new Xoops\Form\Button('', 'buttonremove', _CO_PUBLISHER_REMOVE);
            $buttonremove->set('onclick', 'publisher_appendSelectOption("image_item", "image_notused"), ' . 'publisher_updateSelectOption("image_item", "image_featured");');
            $opentable = new Xoops\Form\Label('', "<table><tr><td>");
            $addcol = new Xoops\Form\Label('', "</td><td>");
            $addbreak = new Xoops\Form\Label('', "<br />");
            $closetable = new Xoops\Form\Label('', "</td></tr></table>");
            $xoops->theme()->addScript(PUBLISHER_URL . '/js/ajaxupload.3.9.js');
            //todo, find replacement for error class
            $js_data = new Xoops\Form\Label('', '
<script type= "text/javascript">/*<![CDATA[*/
$(document).ready(function(){
    var button = $("#publisher_upload_button"), interval;
    new AjaxUpload(button,{
        action: "' . PUBLISHER_URL . '/include/ajax_upload.php", // I disabled uploads in this example for security reasons
        responseType: "text/html",
        name: "publisher_upload_file",
        onSubmit : function(file, ext){
            // change button text, when user selects file
            $("#publisher_upload_message").html(" ");
            button.html("<img src=\'' . PUBLISHER_URL . '/images/loadingbar.gif\'/>"); this.setData({
                "image_nicename": $("#image_nicename").val(),
                "imgcat_id" : $("#imgcat_id").val()
            });
            // If you want to allow uploading only 1 file at time,
            // you can disable upload button
            this.disable();
            interval = window.setInterval(function(){
            }, 200);
        },
        onComplete: function(file, response){
            button.text("' . _CO_PUBLISHER_IMAGE_UPLOAD_NEW . '");
            window.clearInterval(interval);
            // enable upload button
            this.enable();
            // add file to the list
            var result = eval(response);
            if (result[0] == "success") {
                 $("#image_item").append("<option value=\'" + result[1] + "\' selected=\'selected\'>" + result[2] + "</option>");
                 publisher_updateSelectOption(\'image_item\', \'image_featured\');
                 showImgSelected(\'image_display\', \'image_item\', \'uploads/\', \'\', \'' . \XoopsBaseConfig::get('url') . '\')
            } else {
                 $("#publisher_upload_message").html("<div class=\'errorMsg\'>" + result[1] + "</div>");
            }
        }
    });
});
/*]]>*/</script>
');
            $messages = new Xoops\Form\Label('', "<div id='publisher_upload_message'></div>");
            $button = new Xoops\Form\Label('', "<div id='publisher_upload_button'>" . _CO_PUBLISHER_IMAGE_UPLOAD_NEW . "</div>");
            $nicename = new Xoops\Form\Text('', 'image_nicename', 30, 30, _CO_PUBLISHER_IMAGE_NICENAME);
            $catlist = $imgcat_handler->getListByPermission($group, 'imgcat_read', 1);
            $imagecat = new Xoops\Form\Select('', 'imgcat_id', '', 1);
            $imagecat->addOptionArray($catlist);
            $image_upload_tray = new Xoops\Form\ElementTray(_CO_PUBLISHER_IMAGE_UPLOAD, '');
            $image_upload_tray->addElement($js_data);
            $image_upload_tray->addElement($messages);
            $image_upload_tray->addElement($opentable);
            $image_upload_tray->addElement($imagecat);
            $image_upload_tray->addElement($addbreak);
            $image_upload_tray->addElement($nicename);
            $image_upload_tray->addElement($addbreak);
            $image_upload_tray->addElement($button);
            $image_upload_tray->addElement($closetable);
            $imagesTab->addElement($image_upload_tray);
            $image_tray = new Xoops\Form\ElementTray(_CO_PUBLISHER_IMAGE_ITEMS, '');
            $image_tray->addElement($opentable);
            $image_tray->addElement($image_select);
            $image_tray->addElement($addbreak);
            $image_tray->addElement($buttonadd);
            $image_tray->addElement($addcol);
            $image_tray->addElement($image_select2);
            $image_tray->addElement($addbreak);
            $image_tray->addElement($buttonremove);
            $image_tray->addElement($closetable);
            $image_tray->setDescription(_CO_PUBLISHER_IMAGE_ITEMS_DSC);
            $imagesTab->addElement($image_tray);
            $imagename = is_object($objimages['main']) ? $objimages['main']->getVar('image_name') : '';
            $imageforpath = $imagename != '' ? $imagename : 'blank.gif';
            $image_select3 = new Xoops\Form\Select(_CO_PUBLISHER_IMAGE_ITEM, 'image_featured', $imagename, 1);
            $image_select3->addOptionArray($objimage_array);
            $image_select3->set('onchange', 'showImgSelected("image_display", "image_featured", "uploads/", "", "' . \XoopsBaseConfig::get('url') . '");');
            $image_select3->setDescription(_CO_PUBLISHER_IMAGE_ITEM_DSC);
            $imagesTab->addElement($image_select3);
            $imgTag = new Xoops\Html\Img(['src' => $xoops->url('uploads/' . $imageforpath), 'width' => 500, 'name' => 'image_display', 'id' => 'image_display', 'alt' => '']);
            $image_preview = new Xoops\Form\Label(_CO_PUBLISHER_IMAGE_PREVIEW, $imgTag->render());
            $imagesTab->addElement($image_preview);
        }
    }