| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaBaseView UNA Base Representation Classes
- * @{
- */
- /**
- * @see BxDolReport
- */
- class BxBaseReport extends BxDolReport
- {
- protected static $_sTmplContentElementBlock;
- protected static $_sTmplContentElementInline;
- protected static $_sTmplContentDoReport;
- protected static $_sTmplContentDoReportLabel;
- protected $_bCssJsAdded;
- protected $_sJsObjClass;
- protected $_sJsObjName;
- protected $_sStylePrefix;
- protected $_aHtmlIds;
- protected $_aElementDefaults;
- public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
- {
- parent::__construct($sSystem, $iId, $iInit, $oTemplate);
- $this->_bCssJsAdded = false;
- $this->_sJsObjClass = 'BxDolReport';
- $this->_sJsObjName = 'oReport' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
- $this->_sStylePrefix = 'bx-report';
- $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
- $this->_aHtmlIds = array(
- 'main' => 'bx-report-' . $sHtmlId,
- 'counter' => 'bx-report-counter-' . $sHtmlId,
- 'do_link' => 'bx-report-do-link-' . $sHtmlId,
- 'do_popup' => 'bx-report-do-popup-' . $sHtmlId,
- 'do_form' => 'bx-report-do-form-' . $sHtmlId,
- 'by_popup' => 'bx-report-by-popup-' . $sHtmlId
- );
- $this->_aElementDefaults = array(
- 'show_do_report_as_button' => false,
- 'show_do_report_as_button_small' => false,
- 'show_do_report_icon' => true,
- 'show_do_report_label' => false,
- 'show_counter' => true
- );
- if(empty(self::$_sTmplContentElementBlock))
- self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('report_element_block.html');
- if(empty(self::$_sTmplContentElementInline))
- self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('report_element_inline.html');
- if(empty(self::$_sTmplContentDoReport))
- self::$_sTmplContentDoReport = $this->_oTemplate->getHtml('report_do_report.html');
- if(empty(self::$_sTmplContentDoReportLabel))
- self::$_sTmplContentDoReportLabel = $this->_oTemplate->getHtml('report_do_report_label.html');
- }
- public function getJsObjectName()
- {
- return $this->_sJsObjName;
- }
- public function getJsScript($bDynamicMode = false)
- {
- $aParams = array(
- 'sObjName' => $this->_sJsObjName,
- 'sSystem' => $this->getSystemName(),
- 'iAuthorId' => $this->_getAuthorId(),
- 'iObjId' => $this->getId(),
- 'sRootUrl' => BX_DOL_URL_ROOT,
- 'sStylePrefix' => $this->_sStylePrefix,
- 'aHtmlIds' => $this->_aHtmlIds
- );
- $sCode = "var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
- return $this->_oTemplate->_wrapInTagJsCode($sCode);
- }
- public function getJsClick()
- {
- return $this->getJsObjectName() . '.report(this)';
- }
- public function getCounter($aParams = array())
- {
- $sJsObject = $this->getJsObjectName();
- $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
- $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
- $aReport = $this->_oQuery->getReport($this->getId());
- $sClass = $this->_sStylePrefix . '-counter';
- if($bShowDoReportAsButtonSmall)
- $sClass .= ' bx-btn-small-height';
- if($bShowDoReportAsButton)
- $sClass .= ' bx-btn-height';
- return $this->_oTemplate->parseLink('javascript:void(0)', (int)$aReport['count'] > 0 ? $this->_getLabelCounter($aReport['count']) : '', array(
- 'id' => $this->_aHtmlIds['counter'],
- 'class' => $sClass,
- 'title' => _t('_report_do_report_by'),
- 'onclick' => 'javascript:' . $sJsObject . '.toggleByPopup(this)'
- ));
- }
- public function getElementBlock($aParams = array())
- {
- $aParams['usage'] = BX_DOL_REPORT_USAGE_BLOCK;
- return $this->getElement($aParams);
- }
- public function getElementInline($aParams = array())
- {
- $aParams['usage'] = BX_DOL_REPORT_USAGE_INLINE;
- return $this->getElement($aParams);
- }
- public function getElement($aParams = array())
- {
- $aParams = array_merge($this->_aElementDefaults, $aParams);
- $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
- $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
- $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
- $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedReportView();
- $iObjectId = $this->getId();
- $iAuthorId = $this->_getAuthorId();
- $aReport = $this->_oQuery->getReport($iObjectId);
- if(!$this->isAllowedReport() && (!$this->isAllowedReportView() || (int)$aReport['count'] == 0))
- return '';
- $aParams['is_reported'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
- $sTmplName = self::${'_sTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_REPORT_USAGE_DEFAULT)};
- return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
- 'style_prefix' => $this->_sStylePrefix,
- 'html_id' => $this->_aHtmlIds['main'],
- 'class' => $this->_sStylePrefix . ($bShowDoReportAsButton ? '-button' : '') . ($bShowDoReportAsButtonSmall ? '-button-small' : ''),
- 'count' => $aReport['count'],
- 'do_report' => $this->_getDoReport($aParams),
- 'bx_if:show_counter' => array(
- 'condition' => $bShowCounter,
- 'content' => array(
- 'style_prefix' => $this->_sStylePrefix,
- 'bx_if:show_hidden' => array(
- 'condition' => (int)$aReport['count'] == 0,
- 'content' => array()
- ),
- 'counter' => $this->getCounter($aParams)
- )
- ),
- 'script' => $this->getJsScript($bDynamicMode)
- ));
- }
- protected function _getDoReport($aParams = array())
- {
- $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
- $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
- $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
- $bDisabled = !$this->isAllowedReport() || $bReported;
- $sClass = '';
- if($bShowDoReportAsButton)
- $sClass = 'bx-btn';
- else if ($bShowDoReportAsButtonSmall)
- $sClass = 'bx-btn bx-btn-small';
- if($bDisabled)
- $sClass .= $bShowDoReportAsButton || $bShowDoReportAsButtonSmall ? ' bx-btn-disabled' : 'bx-report-disabled';
- return $this->_oTemplate->parseHtmlByContent(self::$_sTmplContentDoReport, array(
- 'style_prefix' => $this->_sStylePrefix,
- 'html_id' => $this->_aHtmlIds['do_link'],
- 'class' => $sClass,
- 'title' => _t('_report_do_report'),
- 'bx_if:show_onclick' => array(
- 'condition' => !$bDisabled,
- 'content' => array(
- 'js_object' => $this->getJsObjectName()
- )
- ),
- 'do_report' => $this->_getLabelDoReport($aParams),
- ));
- }
- protected function _getLabelCounter($iCount)
- {
- return _t('_report_counter', $iCount);
- }
- protected function _getLabelDoReport($aParams = array())
- {
- $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
- return $this->_oTemplate->parseHtmlByContent(self::$_sTmplContentDoReportLabel, array(
- 'bx_if:show_icon' => array(
- 'condition' => isset($aParams['show_do_report_icon']) && $aParams['show_do_report_icon'] == true,
- 'content' => array(
- 'name' => $this->_getIconDoReport($bReported)
- )
- ),
- 'bx_if:show_text' => array(
- 'condition' => isset($aParams['show_do_report_label']) && $aParams['show_do_report_label'] == true,
- 'content' => array(
- 'text' => _t($this->_getTitleDoReport($bReported))
- )
- )
- ));
- }
- protected function _getReport()
- {
- if (!$this->isEnabled())
- return array('code' => 1, 'message' => _t('_report_err_not_enabled'));
- if(!$this->isAllowedReport())
- return array('code' => 2, 'message' => $this->msgErrAllowedReport());
- $oForm = $this->_getFormObject();
- $oForm->setId($this->_aHtmlIds['do_form']);
- $oForm->setName($this->_aHtmlIds['do_form']);
- $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
- $oForm->aInputs['sys']['value'] = $this->_sSystem;
- $oForm->aInputs['object_id']['value'] = $this->_iId;
- $oForm->aInputs['action']['value'] = 'Report';
- $oForm->initChecker();
- if($oForm->isSubmittedAndValid()) {
- $iObjectId = $oForm->getCleanValue('object_id');
- $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
- $iAuthorId = $this->_getAuthorId();
- $iAuthorNip = ip2long($this->_getAuthorIp());
- if(!$this->isAllowedReport(true))
- return array('code' => 2, 'message' => $this->msgErrAllowedReport());
- $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
- if($bPerformed)
- return array('code' => 4, 'message' => _t('_report_err_duplicate_report'));
- $sType = $oForm->getCleanValue('type');
- if(!in_array($sType, $this->_aTypes))
- return array('code' => 5, 'message' => _t('_report_err_wrong_type'));
- $sText = $oForm->getCleanValue('text');
- $sText = bx_process_input($sText, BX_DATA_TEXT_MULTILINE);
- $oForm->setSubmittedValue('text', $sText, $oForm->aFormAttrs['method']);
- $iId = (int)$oForm->insert(array('author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'date' => time()));
- if($iId != 0 && $this->_oQuery->putReport($iObjectId)) {
- $this->_trigger();
- $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Reported', array(
- 'report_type' => $sType,
- 'report_text' => $sText,
- 'report_url' => $this->getBaseUrl(),
- ));
- if($aTemplate)
- sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
- $oZ = new BxDolAlerts($this->_sSystem, 'doReport', $iObjectId, $iAuthorId, array('report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
- $oZ->alert();
-
- $oZ = new BxDolAlerts('report', 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
- $oZ->alert();
- $aReport = $this->_oQuery->getReport($iObjectId);
- return array(
- 'eval' => $this->getJsObjectName() . '.onReport(oData, oElement)',
- 'code' => 0,
- 'id' => $iId,
- 'count' => $aReport['count'],
- 'countf' => (int)$aReport['count'] > 0 ? $this->_getLabelCounter($aReport['count']) : '',
- 'label_icon' => $this->_getIconDoReport(!$bPerformed),
- 'label_title' => _t($this->_getTitleDoReport(!$bPerformed)),
- 'disabled' => !$bPerformed
- );
- }
- return array('code' => 3, 'message' => _t('_report_err_cannot_perform_action'));
- }
- $sPopupId = $this->_aHtmlIds['do_popup'];
- $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('report_do_report_form.html', array(
- 'style_prefix' => $this->_sStylePrefix,
- 'js_object' => $this->getJsObjectName(),
- 'form' => $oForm->getCode(),
- 'form_id' => $oForm->id,
- )));
- return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
- }
- protected function _getReportedBy()
- {
- $aTmplReports = array();
- $aTypes = BxDolForm::getDataItems('sys_report_types');
- $aReports = $this->_oQuery->getPerformedBy($this->getId());
- foreach($aReports as $aReport) {
- list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
- $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
- $aTmplReports[] = array(
- 'style_prefix' => $this->_sStylePrefix,
- 'user_unit' => $sUserUnit,
- 'type' => $aTypes[$aReport['type']],
- 'bx_if:show_text' => array(
- 'condition' => strlen($sText) > 0,
- 'content' => array(
- 'text' => $sText
- )
- )
- );
- }
- if(empty($aTmplReports))
- $aTmplReports = MsgBox(_t('_Empty'));
- return $this->_oTemplate->parseHtmlByName('report_by_list.html', array(
- 'style_prefix' => $this->_sStylePrefix,
- 'bx_repeat:list' => $aTmplReports
- ));
- }
- }
- /** @} */
|