Sfoglia il codice sorgente

Ticket #516 - improvements in the engine.

AntonLV 9 anni fa
parent
commit
5bdddfdd7c

+ 37 - 2
inc/classes/BxDolView.php

@@ -7,7 +7,11 @@
  * @{
  */
 
-define ('BX_DOL_VIEW_OLD_VIEWS', 3 * 86400); ///< views older than this number of seconds will be deleted automatically
+define('BX_DOL_VIEW_OLD_VIEWS', 3 * 86400); ///< views older than this number of seconds will be deleted automatically
+
+define('BX_DOL_VIEW_USAGE_BLOCK', 'block');
+define('BX_DOL_VIEW_USAGE_INLINE', 'inline');
+define('BX_DOL_VIEW_USAGE_DEFAULT', BX_DOL_VIEW_USAGE_BLOCK);
 
 /**
  * @page objects
@@ -132,7 +136,15 @@ class BxDolView extends BxDolObject
         return $iResult;
     }
 
-    function doView()
+    public function actionGetViewedBy()
+    {
+        if (!$this->isEnabled())
+           return '';
+
+        return $this->_getViewedBy();
+    }
+
+    public function doView()
     {
         if(!$this->isEnabled())
             return false;
@@ -152,6 +164,29 @@ class BxDolView extends BxDolObject
 
         return false;
     }
+
+	/**
+     * Permissions functions
+     * 
+     * Note. The 'view' action is performed automatically and therefore the manual action is not allowed.
+     */
+    public function isAllowedView($isPerformAction = false)
+    {
+        return false;
+    }
+
+	/**
+     * Internal functions
+     */
+    protected function _getIconDo()
+    {
+    	return 'eye';
+    }
+
+    protected function _getTitleDo()
+    {
+    	return '_view_do_view';
+    }
 }
 
 /** @} */

+ 17 - 0
inc/classes/BxDolViewQuery.php

@@ -22,6 +22,23 @@ class BxDolViewQuery extends BxDolObjectQuery
         $this->_iPeriod = (int)$aSystem['period'];
     }
 
+    public function isPerformed($iObjectId, $iAuthorId)
+    {
+        $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `viewer_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
+        return (int)$this->getOne($sQuery) > 0;
+    }
+
+	public function getPerformedBy($iObjectId)
+    {
+        $sQuery = $this->prepare("SELECT `viewer_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=? ORDER BY `date` DESC", $iObjectId);
+        return $this->getColumn($sQuery);
+    }
+
+	public function getView($iObjectId)
+    {
+        return array('count' => $this->getObjectCount($iObjectId));
+    }
+
     public function doView($iObjectId, $iAuthorId, $sAuthorIp)
     {
         $iAuthorNip = ip2long($sAuthorIp);

+ 3 - 0
modules/boonex/english/data/langs/system/en.xml

@@ -2371,6 +2371,9 @@ If it is not enabled then please consider implement this optimization, since it
 	<string name="_top"><![CDATA[Top]]></string>
 	<string name="_uknown"><![CDATA[unknown]]></string>
 	<string name="_undefined"><![CDATA[undefined]]></string>
+	<string name="_view_counter"><![CDATA[{0}]]></string>
+	<string name="_view_do_view"><![CDATA[View]]></string>
+	<string name="_view_do_view_by"><![CDATA[Viewed by]]></string>
 	<string name="_vote_counter"><![CDATA[{0}]]></string>
 	<string name="_vote_do_like"><![CDATA[Thank you!]]></string>
 	<string name="_vote_do_unlike"><![CDATA[No thank you]]></string>

+ 3 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -2369,6 +2369,9 @@
 	<string name="_top"><![CDATA[Топ]]></string>
 	<string name="_uknown"><![CDATA[неизвестно]]></string>
 	<string name="_undefined"><![CDATA[неопределено]]></string>
+	<string name="_view_counter"><![CDATA[{0}]]></string>
+	<string name="_view_do_view"><![CDATA[Посмотреть]]></string>
+	<string name="_view_do_view_by"><![CDATA[Просмотрено]]></string>
 	<string name="_vote_counter"><![CDATA[{0}]]></string>
 	<string name="_vote_do_like"><![CDATA[Спасибо!]]></string>
 	<string name="_vote_do_unlike"><![CDATA[Нет, спасибо]]></string>

+ 47 - 0
template/css/view.css

@@ -0,0 +1,47 @@
+/*--- Comment Viewed By ---*/
+div.bx-view-by-list {}
+div.bx-view-by-list-cnt {
+	max-height: 200px;
+	max-height: 12.5rem;
+	min-width: 220px;
+	min-width: 13.75rem;
+	overflow: auto;
+}
+div.bx-view-by-list div.bx-def-unit, 
+div.bx-view-by-list div.bx-base-pofile-unit {
+	float: none;
+	margin-right: 0px;
+	margin-bottom: 0px;
+}
+
+/*--- View Element Styles ---*/
+div.bx-view-element-holder,
+div.bx-view-counter-holder {
+	position: relative;
+	float: left;
+	
+	line-height: 20px;
+    line-height: 1.25rem;
+}
+
+.bx-view-button .bx-view-element-holder,
+.bx-view-button .bx-view-counter-holder {
+	line-height: 36px;
+	line-height: 2.2rem;
+}
+
+.bx-view-button-small .bx-view-element-holder,
+.bx-view-button-small .bx-view-counter-holder {
+	line-height: 24px;
+	line-height: 1.6rem;
+}
+
+.bx-view-element-holder .bx-view-do-view.bx-view-disabled,
+.bx-view-element-holder .bx-view-do-view.bx-view-disabled .sys-icon {
+	color: #ddd;
+}
+
+.bx-view-button .bx-view-counter-holder,
+.bx-view-button-small .bx-view-counter-holder {
+	font-size: 1.5rem;
+}

+ 254 - 0
template/scripts/BxBaseView.php

@@ -12,9 +12,263 @@
  */
 class BxBaseView extends BxDolView
 {
+    protected $_bCssJsAdded;
+    protected $_sStylePrefix;
+    protected $_sJsObjName;
+    protected $_aHtmlIds;
+
+    protected $_aElementDefaults;
+
+    protected $_sTmplNameCounter;
+    protected $_sTmplNameDoView;
+    protected $_sTmplNameDoViewLabel;
+    protected $_sTmplNameByList;
+
     public function __construct($sSystem, $iId, $iInit = 1)
     {
         parent::__construct($sSystem, $iId, $iInit);
+
+        $this->_bCssJsAdded = false;
+
+        $this->_sStylePrefix = 'bx-view';
+        $this->_sJsObjName = 'oView' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
+        
+        $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
+        $this->_aHtmlIds = array(
+            'main' => 'bx-view-' . $sHtmlId,
+            'counter' => 'bx-view-counter-' . $sHtmlId,
+            'by_popup' => 'bx-view-by-popup-' . $sHtmlId
+        );
+
+        $this->_aElementDefaults = array(
+            'show_do_view_as_button' => false,
+            'show_do_view_as_button_small' => false,
+            'show_do_view_icon' => true,
+            'show_do_view_label' => false,
+            'show_counter' => true
+        );
+
+        $this->_sTmplNameCounter = 'view_counter.html';
+        $this->_sTmplNameDoView = 'view_do_view.html';
+        $this->_sTmplNameDoViewLabel = 'view_do_view_label.html';
+        $this->_sTmplNameByList = 'view_by_list.html';
+    }
+
+    public function addCssJs($bDynamicMode = false)
+    {
+    	if($bDynamicMode || $this->_bCssJsAdded)
+    		return;
+
+    	$this->_oTemplate->addJs(array('BxDolView.js'));
+        $this->_oTemplate->addCss(array('view.css'));
+
+        $this->_bCssJsAdded = true;
+    }
+
+    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 = $this->_sJsObjName . " = new BxDolView(" . json_encode($aParams) . ");";
+
+        if($bDynamicMode) {
+			$sCode = "var " . $this->_sJsObjName . " = null; 
+			$.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolView.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
+				bx_get_style('" . bx_js_string($this->_oTemplate->getCssUrl('view.css'), BX_ESCAPE_STR_APOS) . "');
+				" . $sCode . "
+        	}); ";
+        }
+        else
+        	$sCode = "var " . $sCode;
+
+        $this->addCssJs($bDynamicMode);
+        return $this->_oTemplate->_wrapInTagJsCode($sCode);
+    }
+
+    public function getCounter($aParams = array())
+    {
+        $sJsObject = $this->getJsObjectName();
+
+        $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
+        $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
+
+        $aView = $this->_oQuery->getView($this->getId());
+        $sClass = $this->_sStylePrefix . '-counter';
+        if($bShowDoViewAsButtonSmall)
+            $sClass .= ' bx-btn-small-height';
+        if($bShowDoViewAsButton)
+            $sClass .= ' bx-btn-height';
+
+        return $this->_oTemplate->parseHtmlByName($this->_sTmplNameCounter, array(
+            'href' => 'javascript:void(0)',
+            'title' => _t('_view_do_view_by'),
+            'bx_repeat:attrs' => array(
+                array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
+                array('key' => 'class', 'value' => $sClass),
+                array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)')
+            ),
+            'content' => (int)$aView['count'] > 0 ? $this->_getLabelCounter($aView['count']) : ''
+        ));
+    }
+
+    public function getElementBlock($aParams = array())
+    {
+        $aParams['usage'] = BX_DOL_VIEW_USAGE_BLOCK;
+
+        return $this->getElement($aParams);
+    }
+
+    public function getElementInline($aParams = array())
+    {
+        $aParams['usage'] = BX_DOL_VIEW_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;
+
+        $bShowDoView = !isset($aParams['show_do_view']) || $aParams['show_do_view'] == true;
+        $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
+        $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
+        $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
+
+		$iObjectId = $this->getId();
+		$iAuthorId = $this->_getAuthorId();
+		$aView = $this->_oQuery->getView($iObjectId);
+        $bCount = (int)$aView['count'] != 0;
+
+        $isAllowedView = $this->isAllowedView();
+
+        //--- Do View
+        $bTmplVarsDoView = $bShowDoView && ($bCount || $isAllowedView);
+        $aTmplVarsDoView = array();
+        if($bTmplVarsDoView)
+        	$aTmplVarsDoView = array(
+				'style_prefix' => $this->_sStylePrefix,
+				'do_view' => $this->_getDoView($aParams, $isAllowedView),
+			);
+
+        //--- Counter
+        $bTmplVarsCounter = $bShowCounter && ($bCount || $isAllowedView);
+        $aTmplVarsCounter = array();
+        if($bTmplVarsCounter)
+        	$aTmplVarsCounter = array(
+				'style_prefix' => $this->_sStylePrefix,
+				'bx_if:show_hidden' => array(
+					'condition' => !$bCount,
+					'content' => array()
+				),
+				'counter' => $this->getCounter($aParams)
+        	);
+
+		if(!$bTmplVarsDoView && !$bTmplVarsCounter)
+			return '';
+
+        $sTmplName = 'view_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VIEW_USAGE_DEFAULT) . '.html';
+        return $this->_oTemplate->parseHtmlByName($sTmplName, array(
+            'style_prefix' => $this->_sStylePrefix,
+            'html_id' => $this->_aHtmlIds['main'],
+            'class' => $this->_sStylePrefix . ($bShowDoViewAsButton ? '-button' : '') . ($bShowDoViewAsButtonSmall ? '-button-small' : ''),
+            'count' => $aView['count'],
+        	'bx_if:show_do_view' => array(
+        		'condition' => $bTmplVarsDoView,
+        		'content' => $aTmplVarsDoView
+        	),
+        	'bx_if:show_counter' => array(
+				'condition' => $bTmplVarsCounter,
+				'content' => $aTmplVarsCounter
+			),
+            'script' => $this->getJsScript($bDynamicMode)
+        ));
+    }
+
+    protected function _getDoView($aParams = array(), $isAllowedView = true)
+    {
+        $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
+        $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
+		$bDisabled = !$isAllowedView;
+
+        $sClass = '';
+		if($bShowDoViewAsButton)
+			$sClass = 'bx-btn';
+		else if ($bShowDoViewAsButtonSmall)
+			$sClass = 'bx-btn bx-btn-small';
+
+		if($bDisabled)
+			$sClass .= $bShowDoViewAsButton || $bShowDoViewAsButtonSmall ? ' bx-btn-disabled' : 'bx-view-disabled';
+
+        return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoView, array(
+            'style_prefix' => $this->_sStylePrefix,
+            'class' => $sClass,
+            'title' => _t('_view_do_view'),
+        	'bx_if:show_onclick' => array(
+        		'condition' => !$bDisabled,
+        		'content' => array(
+        			'js_object' => $this->getJsObjectName()
+        		)
+        	),
+            'do_view' => $this->_getLabelDo($aParams),
+        ));
+    }
+
+    protected function _getLabelCounter($iCount)
+    {
+        return _t('_view_counter', $iCount);
+    }
+
+    protected function _getLabelDo($aParams = array())
+    {
+        return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoViewLabel, array(
+        	'bx_if:show_icon' => array(
+        		'condition' => isset($aParams['show_do_view_icon']) && $aParams['show_do_view_icon'] == true,
+        		'content' => array(
+        			'name' => $this->_getIconDo()
+        		)
+        	),
+        	'bx_if:show_text' => array(
+        		'condition' => isset($aParams['show_do_view_label']) && $aParams['show_do_view_label'] == true,
+        		'content' => array(
+        			'text' => _t($this->_getTitleDo())
+        		)
+        	)
+        ));
+    }
+
+    protected function _getViewedBy()
+    {
+        $aTmplUsers = array();
+
+        $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
+        foreach($aUserIds as $iUserId) {
+            list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
+            $aTmplUsers[] = array(
+                'style_prefix' => $this->_sStylePrefix,
+                'user_unit' => $sUserUnit
+            );
+        }
+
+        if(empty($aTmplUsers))
+            $aTmplUsers = MsgBox(_t('_Empty'));
+
+        return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
+            'style_prefix' => $this->_sStylePrefix,
+            'bx_repeat:list' => $aTmplUsers
+        ));
     }
 }
 

+ 9 - 0
template/view_by_list.html

@@ -0,0 +1,9 @@
+<div class="__style_prefix__-by-list">
+	<div class="__style_prefix__-by-list-cnt">
+		<div class="__style_prefix__-by-items">
+			<bx_repeat:list>
+				<div class="__style_prefix__-by-item bx-def-padding-sec bx-clearfix">__user_unit__</div>
+			</bx_repeat:list>
+		</div>
+	</div>
+</div>

+ 1 - 0
template/view_counter.html

@@ -0,0 +1 @@
+<bx_include_auto:bx_a.html />

+ 1 - 0
template/view_do_view.html

@@ -0,0 +1 @@
+<a class="__style_prefix__-do-view __class__" href="javascript:void(0)" <bx_if:show_onclick>onclick="javascript:__js_object__.view(this)"</bx_if:show_onclick> title="__title__">__do_view__</a>

+ 1 - 0
template/view_do_view_label.html

@@ -0,0 +1 @@
+<bx_if:show_icon><i class="sys-icon __name__"></i></bx_if:show_icon><bx_if:show_text><span class="bx-def-margin-sec-left-auto">__text__</span></bx_if:show_text>

+ 11 - 0
template/view_element_block.html

@@ -0,0 +1,11 @@
+<div id="__html_id__" class="__style_prefix__ __class__ bx-def-margin-right-auto bx-def-margin-thd-topbottom" bx_view_data_count="__count__">
+	<div class="__style_prefix__-do bx-clearfix">
+		<bx_if:show_do_view>
+			<div class="__style_prefix__-element-holder">__do_view__</div>
+		</bx_if:show_do_view>
+		<bx_if:show_counter>
+			<div class="__style_prefix__-counter-holder bx-def-margin-sec-left-auto bx-btn-border"<bx_if:show_hidden> style="display:none"</bx_if:show_hidden>>__counter__</div>
+		</bx_if:show_counter>
+	</div>
+</div>
+__script__

+ 4 - 0
template/view_element_inline.html

@@ -0,0 +1,4 @@
+<span id="__html_id__" class="__style_prefix__ __class__ bx-def-margin-right-auto bx-def-margin-thd-topbottom" bx_view_data_count="__count__">
+	<span class="__style_prefix__-do"><bx_if:show_do_view><span class="__style_prefix__-element-holder">__do_view__</span></bx_if:show_do_view><bx_if:show_counter><span class="__style_prefix__-counter-holder bx-def-margin-thd-left-auto bx-btn-border"<bx_if:show_hidden> style="display:none"</bx_if:show_hidden>>__counter__</span></bx_if:show_counter></span>
+</span>
+__script__

+ 30 - 0
view.php

@@ -0,0 +1,30 @@
+<?php
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+require_once('./inc/header.inc.php');
+require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
+
+bx_import('BxDolLanguages');
+
+check_logged();
+
+$sSys = isset($_REQUEST['sys']) ? bx_process_input($_REQUEST['sys']) : false;
+$iObjectId = isset($_REQUEST['id']) ? bx_process_input($_REQUEST['id'], BX_DATA_INT) : 0;
+$sAction = isset($_REQUEST['action']) && preg_match ('/^[A-Za-z_-]+$/', $_REQUEST['action']) ? bx_process_input($_REQUEST['action']) : '';
+
+$oView = BxDolView::getObjectInstance($sSys, $iObjectId, true);
+
+if ($oView && $sSys && $iObjectId && $sAction) {
+    header('Content-Type: text/html; charset=utf-8');
+    $sMethod = 'action' . ucfirst($sAction);
+    if(method_exists($oView, $sMethod))
+        echo $oView->$sMethod();
+}
+
+/** @} */