More tweaks/fleshing out:
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 3 Sep 2009 20:34:20 +0000 (16:34 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 29 Sep 2009 20:53:50 +0000 (16:53 -0400)
- Perms checks
- Remove Kronolith::getResource() - use the driver's getResource() method directly.
- have K_Driver_Resource::listResources() method return resource objects directly.

12 files changed:
kronolith/attendees.php
kronolith/calendars/index.php
kronolith/delete.php
kronolith/lib/Driver/Resource.php
kronolith/lib/Driver/Sql.php
kronolith/lib/Event/Kolab.php
kronolith/lib/Event/Sql.php
kronolith/lib/Kronolith.php
kronolith/lib/Resource.php [new file with mode: 0644]
kronolith/lib/Resource/Base.php
kronolith/templates/calendar_list.php
kronolith/templates/calendar_titles.inc

index e36a37d..f4ef39b 100644 (file)
@@ -249,12 +249,14 @@ foreach ($attendees as $email => $status) {
 }
 
 // Add Free/Busy for resources
-foreach ($resources as $r_id => $resource) {
-    $r = Kronolith::getResource($r_id);
-    $vfb = $r->getFreeBusy(null, null, true);
-    $attendee_view->addResourceMember($vfb);
+if (count($resources)) {
+    $driver = Kronolith::getDriver('Resource');
+    foreach ($resources as $r_id => $resource) {
+        $r = $driver->getResource($r_id);
+        $vfb = $r->getFreeBusy(null, null, true);
+        $attendee_view->addResourceMember($vfb);
+    }
 }
-
 $date = Horde_Util::getFormData('date', date('Ymd')) . '000000';
 $date = new Horde_Date($date);
 $vfb_html = $attendee_view->render($date);
index 65425f2..88c0863 100644 (file)
@@ -61,6 +61,12 @@ $edit_img = Horde::img('edit.png', _("Edit"), null, $registry->getImageDir('hord
 $perms_img = Horde::img('perms.png', _("Change Permissions"), null, $registry->getImageDir('horde'));
 $delete_img = Horde::img('delete.png', _("Delete"), null, $registry->getImageDir('horde'));
 
+/* @TODO: Show resources? */
+$resources = array();
+//$resources = Kronolith::listResources();
+//var_dump($resources);
+
+
 Horde::addScriptFile('tables.js', 'horde', true);
 $title = _("Manage Calendars");
 require KRONOLITH_TEMPLATES . '/common-header.inc';
index ff98fff..37ffe6b 100644 (file)
 
 require_once dirname(__FILE__) . '/lib/base.php';
 
-$kronolith_driver = Kronolith::getDriver(null, Horde_Util::getFormData('calendar'));
+if (Kronolith::isResourceCalendar($c = Horde_Util::getFormData('calendar'))) {
+    $driver = 'Resource';
+} else {
+    $driver = null;
+}
+
+$kronolith_driver = Kronolith::getDriver($driver, $c);
 if ($eventID = Horde_Util::getFormData('eventID')) {
     $event = $kronolith_driver->getEvent($eventID);
     if (is_a($event, 'PEAR_Error')) {
@@ -21,10 +27,22 @@ if ($eventID = Horde_Util::getFormData('eventID')) {
         header('Location: ' . $url);
         exit;
     }
-    $share = &$kronolith_shares->getShare($event->getCalendar());
-    if (!$share->hasPermission(Horde_Auth::getAuth(), PERMS_DELETE, $event->getCreatorID())) {
-        $notification->push(_("You do not have permission to delete this event."), 'horde.warning');
+    if ($driver != 'Resource') {
+        $share = &$kronolith_shares->getShare($event->getCalendar());
+        if (!$share->hasPermission(Horde_Auth::getAuth(), PERMS_DELETE, $event->getCreatorID())) {
+            $notification->push(_("You do not have permission to delete this event."), 'horde.warning');
+        } else {
+            $have_perms = true;
+        }
     } else {
+        if (!Horde_Auth::isAdmin()) {
+            $notification->push(_("You do not have permission to delete this event."), 'horde.warning');
+        } else {
+            $have_perms = true;
+        }
+    }
+
+    if (!empty($have_perms)) {
         $notification_type = Kronolith::ITIP_CANCEL;
         $instance = null;
         if (Horde_Util::getFormData('future')) {
index 977e9d4..051a725 100644 (file)
@@ -383,6 +383,16 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
         return $resource;
     }
 
+    public function deleteEvent($event, $silent = false)
+    {
+        parent::deleteEvent($event, $silent);
+
+        /* @TODO: Since this is being removed from a resource calendar, need to
+         * make sure we remove any acceptance status from the event it's
+         * attached to.
+         */
+    }
+
     /**
      * Obtain a Kronolith_Resource by the resource's id
      *
@@ -402,12 +412,8 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
         if (empty($results)) {
             throw new Horde_Exception('Resource not found');
         }
-        $return = array();
-        foreach ($results as $field => $value) {
-            $return[str_replace('resource_', '', $field)] = $this->convertFromDriver($value);
-        }
 
-        return $return;
+        return new Kronolith_Resource_Single($this->_fromDriver($results));
     }
 
     /**
@@ -439,7 +445,7 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
      * fleshed out.
      *
      */
-    function listResources($params = array())
+    public function listResources($params = array())
     {
         $query = 'SELECT resource_id, resource_name, resource_calendar, resource_category FROM kronolith_resources';
         $results = $this->_db->getAll($query, null, DB_FETCHMODE_ASSOC);
@@ -447,7 +453,22 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
             throw new Horde_Exception($results->getMessage());
         }
 
-        return $results;
+        $return = array();
+        foreach ($results as $result) {
+            $return[] = new Kronolith_Resource_Single($this->_fromDriver($result));
+        }
+
+        return $return;
+    }
+
+    protected function _fromDriver($params)
+    {
+        $return = array();
+        foreach ($params as $field => $value) {
+            $return[str_replace('resource_', '', $field)] = $this->convertFromDriver($value);
+        }
+
+        return $return;
     }
 
     /**
index f356a8d..2955f7c 100644 (file)
@@ -735,6 +735,15 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
             $history->log('kronolith:' . $this->_calendar . ':' . $event->getUID(), array('action' => 'delete'), true);
         }
 
+        /* Remove the event from any resources that are attached to it */
+        if (count($resources = $event->getResources())) {
+            $rd = Kronolith::getDriver('Resource');
+            foreach (array_keys($resources) as $uid) {
+                $r = $rd->getResource($uid);
+                $r->removeEvent($event);
+            }
+        }
+
         /* Remove any pending alarms. */
         if (@include_once 'Horde/Alarm.php') {
             $alarm = Horde_Alarm::factory();
index 0994122..6e5a35f 100644 (file)
@@ -269,24 +269,4 @@ class Kronolith_Event_Kolab extends Kronolith_Event
         return $event;
     }
 
-    /**
-     * Function to check availability and auto accept/decline for each resource
-     * attached to this event. Needed here instead of in Kronolith_Driver::saveEvent
-     * since the _properties array is already built at that point.
-     *
-     * @return unknown_type
-     */
-    public function checkResources()
-    {
-        foreach ($this->_resources as $id => $resource) {
-            $r = Kronolith::getResource($id);
-            if ($r->isFree($this)) {
-                $r->addEvent($this);
-                $this->addResource($r, Kronolith::RESPONSE_ACCEPTED);
-            } else {
-                $this->addResource($r, Kronolith::RESPONSE_DECLINED);
-            }
-        }
-    }
-
 }
index 7937e04..bee52b3 100644 (file)
@@ -217,7 +217,7 @@ class Kronolith_Event_Sql extends Kronolith_Event
     public function checkResources()
     {
         foreach ($this->_resources as $id => $resource) {
-            $r = Kronolith::getResource($id);
+            $r = $this->getDriver()->getResource($id);
             if ($r->isFree($this)) {
                 $r->addEvent($this);
                 $this->addResource($r, Kronolith::RESPONSE_ACCEPTED);
index 727ea9a..2754042 100644 (file)
@@ -1825,6 +1825,8 @@ class Kronolith
             if (Horde_Util::getFormData('calendar') == '**remote') {
                 $event = self::getDriver('Ical', Horde_Util::getFormData('remoteCal'))
                     ->getEvent(Horde_Util::getFormData('eventID'));
+            } elseif (strncmp(Horde_Util::getFormData('calendar'), 'resource_', 9) === 0) {
+                $event = self::getDriver('Resource', Horde_Util::getFormData('calendar'))->getEvent(Horde_Util::getFormData('eventID'));
             } else {
                 $event = self::getDriver(null, Horde_Util::getFormData('calendar'))
                     ->getEvent(Horde_Util::getFormData('eventID'));
@@ -1837,8 +1839,12 @@ class Kronolith
             return new Kronolith_View_EditEvent($event);
 
         case 'DeleteEvent':
-            $event = self::getDriver(null, Horde_Util::getFormData('calendar'))
-                ->getEvent(Horde_Util::getFormData('eventID'));
+            if (strncmp(Horde_Util::getFormData('calendar'), 'resource_', 9) === 0) {
+                $event = self::getDriver('Resource', Horde_Util::getFormData('calendar'))->getEvent(Horde_Util::getFormData('eventID'));
+            } else {
+                $event = self::getDriver(null, Horde_Util::getFormData('calendar'))
+                    ->getEvent(Horde_Util::getFormData('eventID'));
+            }
             if (!is_a($event, 'PEAR_Error') &&
                 !$event->hasPermission(PERMS_DELETE)) {
                 $event = PEAR::raiseError(_("Permission Denied"));
@@ -2007,28 +2013,29 @@ class Kronolith
     }
 
     /**
+     * Return a list of resources that the current user has access to administer.
      *
      * @return array of Kronolith_Resource objects
      */
     static public function listResources($params = array())
     {
+        // For now, keep this check here. Maybe move this to the resource
+        // driver object?
+        if (!Horde_Auth::isAdmin()) {
+            return array();
+        }
+
         // Query kronolith_resource table for all(?) available resources?
         // maybe by 'type' or 'name'? type would be arbitrary?
         $driver = Kronolith::getDriver('Resource');
-        $resources = $driver->listResources($params);
-        $return = array();
-        foreach ($resources as $resource) {
-            $return[] = new Kronolith_Resource_Single($resource);
-        }
-
-        return $return;
+        return $driver->listResources($params);
     }
 
-    static public function getResource($id)
-    {
-        $driver = Kronolith::getDriver('Resource');
-        return new Kronolith_Resource_Single($driver->getResource($id));
-    }
+//    static public function getResource($id)
+//    {
+//        $driver = Kronolith::getDriver('Resource');
+//        return new Kronolith_Resource_Single($driver->getResource($id));
+//    }
 
     static public function isResourceCalendar($calendar)
     {
diff --git a/kronolith/lib/Resource.php b/kronolith/lib/Resource.php
new file mode 100644 (file)
index 0000000..db84dda
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+/**
+ * Base class for dealing with Kronolith_Resource objects. Handles basic
+ * creation/deletion/listing by delegating to the underlying Kronolith_Driver
+ * object.
+ *
+ * For now, assume SQL driver only. Could probably easily extend this to use
+ * different backend drivers if/when support is added to those drivers for
+ * resources.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Kronolith
+ */
+class Kronolith_Resource
+{
+    static protected $_driver;
+
+    /**
+     * Removes a resource from storage
+     *
+     * @param Kronolith_Resource $resource
+     * @return boolean
+     * @throws Horde_Exception
+     */
+    static public function removeResource($resource)
+    {
+
+    }
+
+    static public function isResourceCalendar($calendar)
+    {
+        if (strncmp($calendar, 'resource_', 9) === 0) {
+            return true;
+        }
+    }
+
+}
\ No newline at end of file
index 1c874da..35dde64 100644 (file)
@@ -37,6 +37,7 @@ abstract class Kronolith_Resource_Base
             return $this->_id;
         }
 
+        $property = str_replace('resource_', '', $property);
         if (isset($this->_params[$property])) {
             return $this->_params[$property];
         } else {
index 4cb611c..5ba7267 100644 (file)
  </tbody>
 </table>
 
+<?php if (count($resources)): ?>
+<h1 class="smallheader">Resources</div>
+<table summary="<?php echo _("Resource Calendars") ?>" cellspacing="0"  class="striped sortable">
+ <thead>
+   <th class="sortdown"><?php echo _("Resource Name") ?></th>
+   <th class="calendar-list-url nosort"><?php echo _("Display URL") ?></th>
+   <th class="calendar-list-icon nosort" colspan="<?php echo empty($conf['share']['no_sharing']) ? 3 : 2 ?>">&nbsp;</th>
+  </thead>
+<?php foreach ($resources as $resource): ?>
+<?php var_dump($resource);?>
+  <tr>
+   <td><?php echo $resource->name ?></td>
+   <td><?php //echo $resource->url ?></td>
+   <td><?php echo Horde::link(Horde_Util::addParameter($edit_url_base, 'c', $resource->get('calendar')), _("Edit")) . $edit_img . '</a>' ?></td>
+  </tr>
+<?php endforeach; ?>
+</table>
+<?php endif; ?>
+
 </div>
index afefc0f..79495cc 100644 (file)
@@ -4,8 +4,9 @@ foreach ($GLOBALS['display_calendars'] as $calendarId) {
     $calendar_names[] = htmlspecialchars($GLOBALS['all_calendars'][$calendarId]->get('name'));
 }
 if (!empty($GLOBALS['display_resource_calendars'])) {
+    $driver = Kronolith::getDriver('Resource');
     foreach ($GLOBALS['display_resource_calendars'] as $c) {
-        $rc = Kronolith::getResource(Kronolith::getDriver('Resource')->getResourceIdByCalendar($c));
+        $rc = $driver->getResource($driver->getResourceIdByCalendar($c));
         $calendar_names[] = htmlspecialchars($rc->name);
     }
 }