Просмотр исходного кода

Ticket #438 - Favorites engine: Allow to Undo.

AntonLV 10 лет назад
Родитель
Сommit
4d7c8ee2dd

+ 30 - 18
inc/classes/BxDolFavorite.php

@@ -113,6 +113,7 @@ class BxDolFavorite extends BxDolObject
                     `name` AS `name`,
                     `table_track` AS `table_track`,
                     `is_on` AS `is_on`,
+                    `is_undo` AS `is_undo`,
                     `is_public` AS `is_public`,
                     `base_url` AS `base_url`,
                     `trigger_table` AS `trigger_table`,
@@ -148,11 +149,6 @@ class BxDolFavorite extends BxDolObject
     /**
      * Permissions functions
      */
-    public function isPublic()
-    {
-        return (int)$this->_aSystem['is_public'] == 1;
-    }
-
     public function isAllowedFavorite($isPerformAction = false)
     {
         if(isAdmin())
@@ -182,6 +178,19 @@ class BxDolFavorite extends BxDolObject
         return $this->checkActionErrorMsg('favorite_view');
     }
 
+    /**
+     * Auxiliary functions
+     */
+    public function isUndo()
+    {
+        return (int)$this->_aSystem['is_undo'] == 1;
+    }
+
+    public function isPublic()
+    {
+        return (int)$this->_aSystem['is_public'] == 1;
+    }
+
 	/**
      * Internal functions
      */
@@ -190,28 +199,31 @@ class BxDolFavorite extends BxDolObject
         if (!$this->isEnabled())
            return array('code' => 1, 'msg' => _t('_favorite_err_not_enabled'));
 
-	    if(!$this->isAllowedFavorite())
-            return array('code' => 2, 'msg' => $this->msgErrAllowedFavorite());
-
         $iObjectId = $this->getId();
         $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
         $iAuthorId = $this->_getAuthorId();
 
+        $bUndo = $this->isUndo();
         $bPerformed = $this->_oQuery->isPerformed($iObjectId, $iAuthorId);
-        if($bPerformed)
+        $bPerformUndo = $bPerformed && $bUndo ? true : false;
+
+        if(!$bPerformUndo && !$this->isAllowedFavorite())
+            return array('code' => 2, 'msg' => $this->msgErrAllowedFavorite());
+
+        if($bPerformed && !$bUndo)
         	return array('code' => 3, 'msg' => _t('_favorite_err_duplicate_favorite'));
 
-        $iId = (int)$this->_oQuery->doFavorite($iObjectId, $iAuthorId);
+        $iId = (int)$this->_oQuery->{($bPerformUndo ? 'un' : '') . 'doFavorite'}($iObjectId, $iAuthorId);
         if($iId == 0)
             return array('code' => 4, 'msg' => _t('_favorite_err_cannot_perform_action'));
 
-        if(!$this->isAllowedFavorite(true))
-            return array('code' => 2, 'msg' => $this->msgErrAllowedFavorite());
+        if(!$bPerformUndo)
+            $this->isAllowedFavorite(true);
 
-        $this->_trigger();
+        $this->_triggerValue($bPerformUndo ? -1 : 1);
 
-        bx_alert($this->_sSystem, 'favorite', $iObjectId, $iAuthorId, array('favorite_id' => $iId, 'favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
-        bx_alert('favorite', 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
+        bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'favorite', $iObjectId, $iAuthorId, array('favorite_id' => $iId, 'favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
+        bx_alert('favorite', ($bPerformUndo ? 'un' : '') . 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
 
         $aFavorite = $this->_oQuery->getFavorite($iObjectId);
         return array(
@@ -222,18 +234,18 @@ class BxDolFavorite extends BxDolObject
         	'countf' => (int)$aFavorite['count'] > 0 ? $this->_getLabelCounter($aFavorite['count']) : '',
             'label_icon' => $this->_getIconDoFavorite(!$bPerformed),
             'label_title' => _t($this->_getTitleDoFavorite(!$bPerformed)),
-            'disabled' => !$bPerformed
+            'disabled' => !$bPerformed && !$bUndo
         );
     }
 
     protected function _getIconDoFavorite($bPerformed)
     {
-    	return 'star';
+    	return $bPerformed && $this->isUndo() ?  'star-o' : 'star';
     }
 
     protected function _getTitleDoFavorite($bPerformed)
     {
-    	return '_favorite_do_favorite';
+    	return $bPerformed && $this->isUndo() ? '_favorite_do_unfavorite' : '_favorite_do_favorite';
     }
 }
 

+ 0 - 6
inc/classes/BxDolFavoriteQuery.php

@@ -55,12 +55,6 @@ class BxDolFavoriteQuery extends BxDolObjectQuery
         $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
         return (int)$this->query($sQuery) > 0;
     }
-
-    public function updateTriggerTable($iObjectId)
-    {
-        $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
-        return (int)$this->query($sQuery) > 0;
-    }
 }
 
 /** @} */

+ 18 - 0
inc/classes/BxDolObject.php

@@ -238,6 +238,9 @@ class BxDolObject extends BxDol
         return $oProfile;
     }
 
+    /**
+     * Update Trigger table using data which is automatically gotten from object's internal table.
+     */
 	protected function _trigger()
     {
         if(!$this->_aSystem['trigger_table'])
@@ -250,6 +253,21 @@ class BxDolObject extends BxDol
         return $this->_oQuery->updateTriggerTable($iId);
     }
 
+    /**
+     * Update (increment/decrement) Trigger table using provided value.
+     */
+    protected function _triggerValue($iValue)
+    {
+        if(!$this->_aSystem['trigger_table'])
+            return false;
+
+        $iId = $this->getId();
+        if(!$iId)
+            return false;
+
+        return $this->_oQuery->updateTriggerTableValue($iId, $iValue);
+    }
+
     /**
      * Replace provided markers in a string
      * @param $mixed string or array to replace markers in

+ 6 - 0
inc/classes/BxDolObjectQuery.php

@@ -118,6 +118,12 @@ class BxDolObjectQuery extends BxDolDb
         return $this->_updateTriggerTable($iObjectId, $aEntry);
     }
 
+    public function updateTriggerTableValue($iObjectId, $iValue)
+    {
+        $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + ? WHERE `{$this->_sTriggerFieldId}` = ?", (int)$iValue, $iObjectId);
+        return (int)$this->query($sQuery) > 0;
+    }
+
     protected function _updateTriggerTable($iObjectId, $aEntry)
     {
     	$sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);

+ 0 - 2
inc/js/classes/BxDolFavorite.js

@@ -54,7 +54,6 @@ BxDolFavorite.prototype.favorite = function(oLink) {
 
 BxDolFavorite.prototype.onFavorite = function(oData, oElement)
 {
-	console.log(oData);
 	if(oData && oData.msg != undefined && oData.msg.length > 0)
         alert(oData.msg);
 
@@ -73,7 +72,6 @@ BxDolFavorite.prototype.onFavorite = function(oData, oElement)
 		$(oElement).removeAttr('onclick').addClass($(oElement).hasClass('bx-btn') ? 'bx-btn-disabled' : 'bx-favorite-disabled');
 
     var oCounter = this._getCounter(oElement);
-    console.log(oCounter, oData.countf);
     if(oCounter && oCounter.length > 0) {
     	oCounter.html(oData.countf);
 

+ 1 - 0
install/sql/system.sql

@@ -1186,6 +1186,7 @@ CREATE TABLE `sys_objects_favorite` (
   `name` varchar(64) NOT NULL,
   `table_track` varchar(32) NOT NULL,
   `is_on` tinyint(4) NOT NULL default '1',
+  `is_undo` tinyint(4) NOT NULL default '1',
   `is_public` tinyint(4) NOT NULL default '1',
   `base_url` varchar(256) NOT NULL default '',
   `trigger_table` varchar(32) NOT NULL,

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

@@ -34,6 +34,7 @@
 	<string name="_Failed"><![CDATA[Failed]]></string>
 	<string name="_favorite_counter"><![CDATA[{0}]]></string>
 	<string name="_favorite_do_favorite"><![CDATA[Favorite]]></string>
+	<string name="_favorite_do_unfavorite"><![CDATA[Unfavorite]]></string>
 	<string name="_favorite_do_favorite_by"><![CDATA[Favorited by]]></string>
 	<string name="_favorite_err_cannot_perform_action"><![CDATA[Cannot perform the action.]]></string>
 	<string name="_favorite_err_duplicate_favorite"><![CDATA[You already favorited this item.]]></string>

+ 2 - 2
modules/boonex/market/install/sql/install.sql

@@ -494,8 +494,8 @@ INSERT INTO `sys_objects_view` (`name`, `table_track`, `period`, `is_on`, `trigg
 ('bx_market', 'bx_market_views_track', '86400', '1', 'bx_market_products', 'id', 'views', '', '');
 
 -- FAFORITES
-INSERT INTO `sys_objects_favorite` (`name`, `table_track`, `is_on`, `is_public`, `base_url`, `trigger_table`, `trigger_field_id`, `trigger_field_author`, `trigger_field_count`, `class_name`, `class_file`) VALUES 
-('bx_market', 'bx_market_favorites_track', '1', '1', 'page.php?i=view-product&id={object_id}', 'bx_market_products', 'id', 'author', 'favorites', '', '');
+INSERT INTO `sys_objects_favorite` (`name`, `table_track`, `is_on`, `is_undo`, `is_public`, `base_url`, `trigger_table`, `trigger_field_id`, `trigger_field_author`, `trigger_field_count`, `class_name`, `class_file`) VALUES 
+('bx_market', 'bx_market_favorites_track', '1', '1', '1', 'page.php?i=view-product&id={object_id}', 'bx_market_products', 'id', 'author', 'favorites', '', '');
 
 -- STUDIO: page & widget
 INSERT INTO `sys_std_pages`(`index`, `name`, `header`, `caption`, `icon`) VALUES

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

@@ -34,6 +34,7 @@
 	<string name="_Failed"><![CDATA[Неудача]]></string>
 	<string name="_favorite_counter"><![CDATA[{0}]]></string>
 	<string name="_favorite_do_favorite"><![CDATA[В избранное]]></string>
+	<string name="_favorite_do_unfavorite"><![CDATA[Из избранного]]></string>
 	<string name="_favorite_do_favorite_by"><![CDATA[Добавили в избранное]]></string>
 	<string name="_favorite_err_cannot_perform_action"><![CDATA[Невозможно выполнить действие.]]></string>
 	<string name="_favorite_err_duplicate_favorite"><![CDATA[Вы уже добавляли данный объект в избранное ранее.]]></string>

+ 8 - 6
template/scripts/BxBaseFavorite.php

@@ -145,8 +145,10 @@ class BxBaseFavorite extends BxDolFavorite
 		$iObjectId = $this->getId();
 		$iAuthorId = $this->_getAuthorId();
         $aFavorite = $this->_oQuery->getFavorite($iObjectId);
+        $bCount = (int)$aFavorite['count'] != 0;
 
-        if(!$this->isAllowedFavorite() && (!$this->isAllowedFavoriteView() || (int)$aFavorite['count'] == 0))
+        $bAllowedFavorite = $this->isAllowedFavorite();
+        if(!$bAllowedFavorite && (!$this->isAllowedFavoriteView() || !$bCount))
             return '';
 
         $aParams['is_favorited'] = $this->_oQuery->isPerformed($iObjectId, $iAuthorId) ? true : false;
@@ -157,13 +159,13 @@ class BxBaseFavorite extends BxDolFavorite
             'html_id' => $this->_aHtmlIds['main'],
             'class' => $this->_sStylePrefix . ($bShowDoFavoriteAsButton ? '-button' : '') . ($bShowDoFavoriteAsButtonSmall ? '-button-small' : ''),
             'count' => $aFavorite['count'],
-            'do_favorite' => $this->_getDoFavorite($aParams),
+            'do_favorite' => $this->_getDoFavorite($aParams, $bAllowedFavorite),
             'bx_if:show_counter' => array(
                 'condition' => $bShowCounter,
                 'content' => array(
                     'style_prefix' => $this->_sStylePrefix,
         			'bx_if:show_hidden' => array(
-        				'condition' => (int)$aFavorite['count'] == 0,
+        				'condition' => !$bCount,
         				'content' => array()
         			),
                     'counter' => $this->getCounter()
@@ -173,12 +175,12 @@ class BxBaseFavorite extends BxDolFavorite
         ));
     }
 
-    protected function _getDoFavorite($aParams = array())
+    protected function _getDoFavorite($aParams = array(), $bAllowedFavorite = true)
     {
     	$bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
         $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
         $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
-		$bDisabled = !$this->isAllowedFavorite() || $bFavorited;
+		$bDisabled = !$bAllowedFavorite || ($bFavorited  && !$this->isUndo());
 
         $sClass = '';
 		if($bShowDoFavoriteAsButton)
@@ -193,7 +195,7 @@ class BxBaseFavorite extends BxDolFavorite
             'style_prefix' => $this->_sStylePrefix,
         	'html_id' => $this->_aHtmlIds['do_link'],
             'class' => $sClass,
-            'title' => _t('_favorite_do_favorite'),
+            'title' => bx_html_attribute(_t($this->_getTitleDoFavorite($bFavorited))),
         	'bx_if:show_onclick' => array(
         		'condition' => !$bDisabled,
         		'content' => array(