Web Tech/PHP2017.02.11 00:03

class나 function 선언시 function_exists나 class_exists를 이용하지 않으면 미리보기시 해당 class나 function이 두번 선언되어서 에러가 발생할 수 있음


상수 define, defined를 이용하거나  function_exists나 class_exists를 이용하여 회피


아래 예제 코드는 xeschool 에 있는 pop opener입니다.


예제 코드

<?php

    if(!defined("__XE__")) exit();

    if(Context::get('module') == 'admin') return;


    if($called_position == 'before_module_proc') {

        // 팝업 스크립트 처리

        function getPopupScript($val) {


            if($val->content) {

                $order = array("\r\n", "\n", "\r");

                $replace = '';

                $val->content = str_replace($order, $replace, $val->content);

                $val->content = str_replace("'", '&#39;', $val->content);

            }


            $popup_content = "{id:'".$val->popup_srl."'"

                .($val->popup_type?",popup_type:'".$val->popup_type."'":"")

                .($val->content?",content:'".$val->content."'":"")

                .($val->popup_url?",url:'".$val->popup_url."'":"")

                .($val->popup_link?",link:'".$val->popup_link."'":"")

                .($val->popup_link_type?",link_type:'".$val->popup_link_type."'":"")

                .($val->open_type?",open_type:'".$val->open_type."'":"")

                .($val->top?",top:'".$val->top."'":"")

                .($val->left?",left:'".$val->left."'":"")

                .($val->width?",width:'".$val->width."'":"")

                .($val->height?",height:'".$val->height."'":"")

                .($val->exp_days?",exp_days:'".$val->exp_days."'":"")

.($val->popup_style?",popup_style:'".$val->popup_style."'":"")

.($val->popup_checkbox?",popup_checkbox:'".$val->popup_checkbox."'":"")

.($val->element_id?",element_id:'".$val->element_id."'":"")

                ."}";


            return $popup_content;

        }


        // jQuery로 팝업 열기

        function setPopupScript($popupList) {


            Context::addCssFile('./addons/popup_opener/popup_opener.css');

            Context::addJsFile('./addons/popup_opener/jquery.popup_opener.1.5.4.3.js');


Context::loadLang(_XE_PATH_.'modules/popup/lang');

$msg_xe_popup = Context::getLang('msg_popup_do_not_display');


            $addPopupScript = '<script type="text/javascript">//<![CDATA['."\n";

$addPopupScript .= "var msg_popup_do_not_display = '".$msg_xe_popup."';\n";

$addPopupScript .= 'jQuery(function(){'."\n";


            if($popupList) {

                if(!is_array($popupList)) $popupList = array($popupList);


                foreach($popupList as $val){

                    $addPopupScript .= "jQuery('<div></div>',{id:'xe_popup".$val->popup_srl."'})";

                    $addPopupScript .= ".css({'position':'absolute','width':'".$val->width."px'})";

                    $addPopupScript .= ".xe_popup(".getPopupScript($val).");\n";

                }

            }


            $addPopupScript .= '});'."\n";

            $addPopupScript .= '//]]></script>'."\n";


            Context::addHtmlFooter($addPopupScript);

        }


// 팝업 본문 구하기

function getPopupContent($document_srl) {

$oDocumentModel = &getModel('document');

$oDocument = $oDocumentModel->getDocument($document_srl);


return $oDocument->get('content');

}


if($this->module_info->module_srl) {


$oModuleModel = &getModel('module');

$isActionPopupOpen = false;


// 팝업 모듈 확인

$args->site_srl = $this->module_info->site_srl;

            $popup_module_info = $oModuleModel->getModuleInfoByMid('popup', $args->site_srl);

            if(!$popup_module_info) return;


// 유효일자 팝업 목록 구하기

$args->curdate = date("Ymd", mktime());

$output = executeQueryArray('popup.getPopupValidDate', $args);

if(!$output->toBool()) return;


if(count($output->data) > 0) {

foreach($output->data as $k => $pop) {

$pop->element_id = $addon_info->element_id;


if($pop->target_type == 'action' && $this->act == $pop->target_actions) {

$pop->content = getPopupContent($pop->document_srl);

$actionPopupList[$k] = $pop;

$isActionPopupOpen = true;

} else if ($pop->target_type == 'module') {

if ($this->module_srl == $pop->target_srl || $popup_module_info->module_srl == $pop->target_srl) {

$pop->content = getPopupContent($pop->document_srl);

$modulePopupList[$k] = $pop;

}

}

}

}


// 팝업 세팅

if($isActionPopupOpen) setPopupScript($actionPopupList);

else setPopupScript($modulePopupList);

}


    }

?>


수정 코드

<?php

if (!defined('popup_opener_addon_func_loaded')) {

define('popup_opener_addon_func_loaded', 1);


// 팝업 스크립트 처리

function getPopupScript($val) {


if ($val->content) {

$order        = array("\r\n", "\n", "\r");

$replace      = '';

$val->content = str_replace($order, $replace, $val->content);

$val->content = str_replace("'", '&#39;', $val->content);

}


$popup_content = "{id:'" . $val->popup_srl . "'"

. ($val->popup_type ? ",popup_type:'" . $val->popup_type . "'" : "")

. ($val->content ? ",content:'" . $val->content . "'" : "")

. ($val->popup_url ? ",url:'" . $val->popup_url . "'" : "")

. ($val->popup_link ? ",link:'" . $val->popup_link . "'" : "")

. ($val->popup_link_type ? ",link_type:'" . $val->popup_link_type . "'" : "")

. ($val->open_type ? ",open_type:'" . $val->open_type . "'" : "")

. ($val->top ? ",top:'" . $val->top . "'" : "")

. ($val->left ? ",left:'" . $val->left . "'" : "")

. ($val->width ? ",width:'" . $val->width . "'" : "")

. ($val->height ? ",height:'" . $val->height . "'" : "")

. ($val->exp_days ? ",exp_days:'" . $val->exp_days . "'" : "")

. ($val->popup_style ? ",popup_style:'" . $val->popup_style . "'" : "")

. ($val->popup_checkbox ? ",popup_checkbox:'" . $val->popup_checkbox . "'" : "")

. ($val->element_id ? ",element_id:'" . $val->element_id . "'" : "")

. "}";


return $popup_content;

}


// jQuery로 팝업 열기

function setPopupScript($popupList) {


Context::addCssFile('./addons/popup_opener/popup_opener.css');

Context::addJsFile('./addons/popup_opener/jquery.popup_opener.1.5.4.3.js');


Context::loadLang(_XE_PATH_ . 'modules/popup/lang');

$msg_xe_popup = Context::getLang('msg_popup_do_not_display');


$addPopupScript = '<script type="text/javascript">//<![CDATA[' . "\n";

$addPopupScript .= "var msg_popup_do_not_display = '" . $msg_xe_popup . "';\n";

$addPopupScript .= 'jQuery(function(){' . "\n";


if ($popupList) {

if (!is_array($popupList)) $popupList = array($popupList);


foreach ($popupList as $val) {

$addPopupScript .= "jQuery('<div></div>',{id:'xe_popup" . $val->popup_srl . "'})";

$addPopupScript .= ".css({'position':'absolute','width':'" . $val->width . "px'})";

$addPopupScript .= ".xe_popup(" . getPopupScript($val) . ");\n";

}

}


$addPopupScript .= '});' . "\n";

$addPopupScript .= '//]]></script>' . "\n";


Context::addHtmlFooter($addPopupScript);

}


// 팝업 본문 구하기

function getPopupContent($document_srl) {

$oDocumentModel = &getModel('document');

$oDocument      = $oDocumentModel->getDocument($document_srl);


return $oDocument->get('content');

}


}



if (!defined("__XE__")) exit();

if (Context::get('module') == 'admin') return;


if ($called_position == 'before_module_proc') {

if ($this->module_info->module_srl) {


$oModuleModel      = &getModel('module');

$isActionPopupOpen = false;


// 팝업 모듈 확인

$args->site_srl    = $this->module_info->site_srl;

$popup_module_info = $oModuleModel->getModuleInfoByMid('popup', $args->site_srl);

if (!$popup_module_info) return;


// 유효일자 팝업 목록 구하기

$args->curdate = date("Ymd", mktime());

$output        = executeQueryArray('popup.getPopupValidDate', $args);

if (!$output->toBool()) return;


if (count($output->data) > 0) {

foreach ($output->data as $k => $pop) {

$pop->element_id = $addon_info->element_id;


if ($pop->target_type == 'action' && $this->act == $pop->target_actions) {

$pop->content        = getPopupContent($pop->document_srl);

$actionPopupList[$k] = $pop;

$isActionPopupOpen   = true;

} else if ($pop->target_type == 'module') {

if ($this->module_srl == $pop->target_srl || $popup_module_info->module_srl == $pop->target_srl) {

$pop->content        = getPopupContent($pop->document_srl);

$modulePopupList[$k] = $pop;

}

}

}

}


// 팝업 세팅

if ($isActionPopupOpen) setPopupScript($actionPopupList);

else setPopupScript($modulePopupList);

}


}

저작자 표시 비영리 동일 조건 변경 허락
신고
Posted by ned3y2k