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

#366 Events: deleting intervals, checking permissions

Alex Trofimov 10 лет назад
Родитель
Сommit
64f37de05f

+ 29 - 12
modules/boonex/events/classes/BxEventsDb.php

@@ -19,6 +19,16 @@ class BxEventsDb extends BxBaseModGroupsDb
         parent::__construct($oConfig);
     }
 
+    public function getContentInfoByIntervalId ($iIntervalId)
+    {
+        $iContentId = $this->getOne("SELECT `event_id` FROM `bx_events_intervals` WHERE `interval_id` = :interval", array(
+            'interval' => $iIntervalId,
+        ));
+        if (!$iContentId)
+            return 0;
+        return $this->getContentInfoById ($iContentId);
+    }
+
     public function getContentInfoById ($iContentId)
     {
         $sQuery = $this->prepare ("SELECT `c`.*, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status`, MAX(`i`.`repeat_stop`) AS `repeat_stop` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` 
@@ -28,6 +38,13 @@ class BxEventsDb extends BxBaseModGroupsDb
             GROUP BY `c`.`id`", $this->_oConfig->getName(), $iContentId);
         return $this->getRow($sQuery);
     }
+
+    public function deleteIntervalById ($iIntervalId)
+    {
+        return $this->query("DELETE FROM `bx_events_intervals` WHERE `interval_id` = :interval", array(
+            'interval' => $iIntervalId,
+        ));
+    }
     
     public function getIntervals ($iContentId) 
     {
@@ -36,7 +53,7 @@ class BxEventsDb extends BxBaseModGroupsDb
         ));
     }
 
-    public function getEntriesByDate($sDateFrom, $sDateTo, $iEventId = 0)
+    public function getEntriesByDate($sDateFrom, $sDateTo, $iEventId = 0, $aSQLPart = array())
     {
         // validate input data
         if (false === ($oDateFrom = date_create($sDateFrom, new DateTimeZone('UTC'))))
@@ -68,27 +85,28 @@ class BxEventsDb extends BxBaseModGroupsDb
                 'year_start' => $oDateFrom->format('Y'),
                 'year' => $oDateIter->format('Y'),
                 'month' => $oDateIter->format('n'),
-                'week_of_month' => $oDateIter->format('W') - $oDateMonthBegin->format('W') + 1,
+                'week_of_month' => $oDateIter->format('W') - $oDateMonthBegin->format('W') + 1, // TODO: sometimes in January it is negative
                 'day_of_month' => $oDateIter->format('j'),
                 'day_of_week' => $oDateIter->format('N'),
             );
-            $sWhere = '';
+
+            $sWhere = isset($aSQLPart['where']) ? $aSQLPart['where'] : '';
             if ((int)$iEventId) {
                 $aBindings['event'] = (int)$iEventId;
-                $sWhere = " AND `e`.`id` = :event ";
+                $sWhere .= " AND `bx_events_data`.`id` = :event ";
             }
-            $a = $this->getAll("SELECT DISTINCT `e`.`id`, `e`.`event_name` AS `title`, `e`.`date_start`, `e`.`date_end`, `e`.`timezone`
-                FROM `bx_events_data` AS `e`
+            $a = $this->getAll("SELECT DISTINCT `bx_events_data`.`id`, `bx_events_data`.`event_name` AS `title`, `bx_events_data`.`date_start`, `bx_events_data`.`date_end`, `bx_events_data`.`timezone`
+                FROM `bx_events_data`
                 LEFT JOIN `bx_events_intervals` AS `i` ON (
-                    `e`.`id` = `i`.`event_id`                    
+                    `bx_events_data`.`id` = `i`.`event_id`
                     AND
-                    `e`.`date_start` < :timestamp_max                    
+                    `bx_events_data`.`date_start` < :timestamp_max
                     AND
-                    `i`.`repeat_stop` >= :timestamp_min
+                    (0 = `i`.`repeat_stop` OR `i`.`repeat_stop` >= :timestamp_min)
                     AND (
                         (0 = `i`.`repeat_year` OR 0 = (:year - :year_start) % `i`.`repeat_year`)
                         AND 
-                        (0 = `i`.`repeat_month` OR  :month = `i`.`repeat_month`)
+                        (0 = `i`.`repeat_month` OR :month = `i`.`repeat_month`)
                         AND 
                         (0 = `i`.`repeat_week_of_month` OR :week_of_month = `i`.`repeat_week_of_month`)
                         AND 
@@ -97,13 +115,12 @@ class BxEventsDb extends BxBaseModGroupsDb
                         (0 = `i`.`repeat_day_of_week` OR :day_of_week = `i`.`repeat_day_of_week`)
                     )
                 )
-                WHERE ((`e`.`date_start` >= :timestamp_min AND `e`.`date_start` <= :timestamp_max AND `i`.`interval_id` IS NULL) OR (`i`.`interval_id` IS NOT NULL)) $sWhere
+                WHERE ((`bx_events_data`.`date_start` >= :timestamp_min AND `bx_events_data`.`date_start` <= :timestamp_max AND `i`.`interval_id` IS NULL) OR (`i`.`interval_id` IS NOT NULL)) $sWhere
             ", $aBindings);
 
             // prepare variables for each event
             $sCurrentDay = $oDateIter->format('Y-m-d');
             foreach ($a as $k => $r) {
-                // TODO: check permissions
                 $oDateStart = new DateTime();
                 $oDateStart->setTimestamp($r['date_start']);
                 $oDateStart->setTimezone(new DateTimeZone('UTC'));

+ 3 - 1
modules/boonex/events/classes/BxEventsFormEntry.php

@@ -236,8 +236,10 @@ class BxEventsFormEntry extends BxBaseModGroupsFormEntry
     
     function addCssJs ()
     {
-        if (!self::$_isCssJsAdded)
+        if (!self::$_isCssJsAdded) {
             $this->oTemplate->addJs('intervals.js');
+            $this->oTemplate->addCss('informer.css');
+        }
 
         parent::addCssJs ();
     }

+ 47 - 9
modules/boonex/events/classes/BxEventsModule.php

@@ -21,9 +21,23 @@ class BxEventsModule extends BxBaseModGroupsModule
 
     public function actionCalendarData()
     {
-        // TODO: check permissions
-        
-        $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'));
+        // check permissions
+        $iContentId = (int)bx_get('event');
+        if ($iContentId) {
+            $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
+            if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aContentInfo)) {
+                $this->_oTemplate->displayAccessDenied();
+                exit;
+            }
+            $aSQLPart = array();
+        }
+        else {
+            $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
+            $aSQLPart = $oPrivacy ? $oPrivacy->getContentPublicAsSQLPart(0, $oPrivacy->getPartiallyVisiblePrivacyGroups()) : array();
+        }
+
+        // get entries
+        $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
         
         header('Content-Type: application/json; charset=utf-8');
         echo json_encode($aEntries);
@@ -33,9 +47,9 @@ class BxEventsModule extends BxBaseModGroupsModule
     {
         if (isset($aData['event'])) {
             $aContentInfo = $this->_oDb->getContentInfoById ((int)$aData['event']);
-            if (!$aContentInfo['repeat_stop']) // don't display calendar for non repeating events
+            if ('' == $aContentInfo['repeat_stop']) // don't display calendar for non repeating events
                 return '';
-        }
+        } 
 
         $o = new BxTemplCalendar(array(
             'eventSources' => array (
@@ -57,11 +71,20 @@ class BxEventsModule extends BxBaseModGroupsModule
         $this->$sMethodName();
     }
 
-    public function subactionRestore()
+    public function subactionRestore($iContentId = 0)
     {
-        // TODO: check permissions
+        if (!$iContentId)
+            $iContentId = (int)bx_get('c');
+
+        if (!($aContentInfo = $this->_oDb->getContentInfoById($iContentId))) {
+            $this->_oTemplate->displayPageNotFound();
+            exit;
+        }
+        if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo)) {
+            $this->_oTemplate->displayAccessDenied();
+            exit;
+        }
 
-        $iContentId = (int)bx_get('c');
         $a = $this->_oDb->getIntervals($iContentId);
         foreach ($a as $iIntervalId => $r) {
             $a[$iIntervalId]['file_id'] = $r['interval_id'];
@@ -76,7 +99,22 @@ class BxEventsModule extends BxBaseModGroupsModule
 
     public function subactionDelete()
     {
-        // TODO: check permissions        
+        header('Content-type: text/html; charset=utf-8');
+
+        $iIntervalId = (int)bx_get('id');
+
+        if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
+            echo _t('_sys_request_page_not_found_cpt');
+        }
+        elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
+            echo $sMsg;
+        } 
+        elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
+            echo _t('_sys_txt_error_occured');
+        } 
+        else {
+            echo 'ok';
+        }
     }
 }
 

+ 1 - 0
modules/boonex/events/install/langs/en.xml

@@ -276,6 +276,7 @@
     <string name="_bx_events_cat_repeat_week_of_month_third_week_of_month"><![CDATA[Every 3rd week of the month]]></string>
     <string name="_bx_events_cat_repeat_week_of_month_fourth_week_of_month"><![CDATA[Every 4th week of the month]]></string>
     <string name="_bx_events_cat_repeat_week_of_month_fifth_week_of_month"><![CDATA[Every 5th week of the month]]></string>
+    <string name="_bx_events_cat_repeat_week_of_month_sixth_week_of_month"><![CDATA[Every 6th week of the month]]></string>
 
     <string name="_bx_events_pre_lists_repeat_day_of_month"><![CDATA[Repeat day of the month]]></string>
     <string name="_bx_events_cat_repeat_day_of_month_every_day"><![CDATA[Every day]]></string>

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

@@ -329,8 +329,8 @@ INSERT INTO `sys_form_pre_values`(`Key`, `Value`, `Order`, `LKey`, `LKey2`) VALU
 ('bx_events_repeat_week_of_month', '2', 2, '_bx_events_cat_repeat_week_of_month_second_week_of_month', ''),
 ('bx_events_repeat_week_of_month', '3', 3, '_bx_events_cat_repeat_week_of_month_third_week_of_month', ''),
 ('bx_events_repeat_week_of_month', '4', 4, '_bx_events_cat_repeat_week_of_month_fourth_week_of_month', ''),
-('bx_events_repeat_week_of_month', '5', 5, '_bx_events_cat_repeat_week_of_month_fifth_week_of_month', '');
-
+('bx_events_repeat_week_of_month', '5', 5, '_bx_events_cat_repeat_week_of_month_fifth_week_of_month', ''),
+('bx_events_repeat_week_of_month', '6', 6, '_bx_events_cat_repeat_week_of_month_sixth_week_of_month', '');
 
 
 INSERT INTO `sys_form_pre_lists`(`key`, `title`, `module`, `use_for_sets`) VALUES

+ 8 - 16
modules/boonex/events/js/intervals.js

@@ -27,7 +27,7 @@ BxDolEventsIntervals.prototype.init = function (options) {
 
     this._sTemplateGhost = options.template_ghost ? options.template_ghost : '<div id="' + this._getIntervalContainerId('{interval_id}') + '"><input type="hidden" name="f[]" value="{interval_id}" />{interval_name} (<a href="javascript:void(0);" onclick="{js_instance_name}.deleteGhost(this, \'{interval_id}\')">delete</a>)</div>';
 
-    this._sTemplateErrorGhosts = options.template_error ? options.template_error : '<div style="color:red;">{error}</div>';
+    this._sTemplateErrorGhosts = options.template_error ? options.template_error : '<div class="bx-informer-msg-error bx-def-padding-sec bx-def-round-corners bx-def-margin-sec-top">{error}</div>';
 
     this._iContentId = undefined == options.content_id || '' == options.content_id ? '' : parseInt(options.content_id);
 };
@@ -36,8 +36,8 @@ BxDolEventsIntervals.prototype._getUrlWithStandardParams = function () {
     return sUrlRoot + 'modules/?r=events/intervals&uid=' + this._sUniqId;
 };
 
-BxDolEventsIntervals.prototype._getIntervalContainerId = function (iIntervalId) {
-	return 'bx-event-interval-' + iIntervalId;
+BxDolEventsIntervals.prototype._getIntervalContainerId = function (iIntervalId) {    
+	return 'bx-events-interval-' + iIntervalId;
 };
 
 BxDolEventsIntervals.prototype.getCurrentIntervalsCount = function () {
@@ -101,17 +101,6 @@ BxDolEventsIntervals.prototype.restoreGhosts = function () {
 
                 if ("undefined" !== typeof(sHTML)) {
                     sHTML = $this._replaceVars(sHTML, oVars);
-/*
-                    // replace vars
-                    for (var i in oVars)
-                        sHTML = sHTML.replace (new RegExp('{'+i+'}', 'g'), oVars[i]);
-
-                    // set selected values
-                    var oHTML = $(sHTML);
-                    for (var i in oVars)
-                        oHTML.find('select[name="' + i + '[]"] option[value="' + oVars[i] + '"]').attr('selected', 'selected');
-                    sHTML = $('<div></div>').append(oHTML).html();
-*/
                     $('#' + $this._sResultContainerId).prepend(sHTML).addWebForms();
                 }
             });
@@ -141,15 +130,18 @@ BxDolEventsIntervals.prototype.deleteGhost = function (e, iIntervalId) {
                     $(this).remove();
                 });
             } else {
-                $('#' + this._sResultContainerId).prepend($this._sTemplateErrorGhosts.replace('{error}', sMsg));
+                $('#' + $this._sResultContainerId).prepend($this._sTemplateErrorGhosts.replace('{error}', sMsg));
             }
         });
     }
 };
 
 BxDolEventsIntervals.prototype._replaceVars = function (sHTML, oVars) {
+
+    oVars = $.extend({}, oVars, {js_instance_name: this._sJsInstanceName});
+
     // replace vars
-    for (var i in oVars)
+    for (var i in oVars) 
         sHTML = sHTML.replace (new RegExp('{'+i+'}', 'g'), oVars[i]);
 
     // set selected values

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

@@ -274,6 +274,7 @@
     <string name="_bx_events_cat_repeat_week_of_month_third_week_of_month"><![CDATA[Каждую 3-ю неделю месяца]]></string>
     <string name="_bx_events_cat_repeat_week_of_month_fourth_week_of_month"><![CDATA[Каждую 4-ю неделю месяца]]></string>
     <string name="_bx_events_cat_repeat_week_of_month_fifth_week_of_month"><![CDATA[Каждую 5-ю неделю месяца]]></string>
+    <string name="_bx_events_cat_repeat_week_of_month_sixth_week_of_month"><![CDATA[Каждую 6-ю неделю месяца]]></string>
 
     <string name="_bx_events_pre_lists_repeat_day_of_month"><![CDATA[Повторять: число месяца]]></string>
     <string name="_bx_events_cat_repeat_day_of_month_every_day"><![CDATA[Каждый день]]></string>