resource event delettion, start building a useful resource page...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 4 Sep 2009 00:08:49 +0000 (20:08 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 29 Sep 2009 20:53:50 +0000 (16:53 -0400)
Make sure we delete the resource from the 'original' event when
directly deleting the resource's copy of the event.

Fix some variable/method names

Start building a resorce management page.

kronolith/calendars/index.php
kronolith/lib/Driver/Resource.php
kronolith/lib/Driver/Sql.php
kronolith/lib/Event.php
kronolith/lib/Event/Sql.php
kronolith/lib/Kronolith.php
kronolith/lib/Resource/Single.php
kronolith/lib/View/Week.php
kronolith/resources.php

index 88c0863..65425f2 100644 (file)
@@ -61,12 +61,6 @@ $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 051a725..d732dff 100644 (file)
@@ -383,14 +383,36 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
         return $resource;
     }
 
+    /**
+     * Delete an event.
+     *
+     * Since this is the Kronolith_Resource's version of the event, if we
+     * delete it, we must also make sure to remove it from the event that
+     * it is attached to. Not sure if there is a better way to do this...
+     *
+     * @see lib/Driver/Kronolith_Driver_Sql#deleteEvent($eventId, $silent)
+     */
     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.
+        /* Since this is the Kronolith_Resource's version of the event, if we
+         * delete it, we must also make sure to remove it from the event that
+         * it is attached to. Not sure if there is a better way to do this...
          */
+        $delete_event = $this->getEvent($event);
+        $uid = $delete_event->getUID();
+        $driver = Kronolith::getDriver();
+        $events = $driver->getByUID($uid, null, true);
+        foreach ($events as $e) {
+            $resources = $e->getResources();
+            if (count($resources)) {
+                // found the right entry
+                $r = $this->getResource($this->getResourceIdByCalendar($delete_event->getCalendar()));
+                $e->removeResource($r);
+                $e->save();
+            }
+        }
+
+        parent::deleteEvent($event, $silent);
     }
 
     /**
index 2955f7c..37741c6 100644 (file)
@@ -736,7 +736,8 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         }
 
         /* Remove the event from any resources that are attached to it */
-        if (count($resources = $event->getResources())) {
+        $resources = $event->getResources();
+        if (count($resources)) {
             $rd = Kronolith::getDriver('Resource');
             foreach (array_keys($resources) as $uid) {
                 $r = $rd->getResource($uid);
index e17bdeb..0af8f73 100644 (file)
@@ -1568,8 +1568,8 @@ abstract class Kronolith_Event
      */
     function removeResource($resource)
     {
-        if (isset($this->_resources[$resource->uid])) {
-            unset ($this->_resources[$resource->uid]);
+        if (isset($this->_resources[$resource->id])) {
+            unset ($this->_resources[$resource->id]);
         }
     }
 
index bee52b3..93cd981 100644 (file)
@@ -217,7 +217,7 @@ class Kronolith_Event_Sql extends Kronolith_Event
     public function checkResources()
     {
         foreach ($this->_resources as $id => $resource) {
-            $r = $this->getDriver()->getResource($id);
+            $r = Kronolith::getDriver('Resource')->getResource($id);
             if ($r->isFree($this)) {
                 $r->addEvent($this);
                 $this->addResource($r, Kronolith::RESPONSE_ACCEPTED);
index 9824237..eb35057 100644 (file)
@@ -318,6 +318,9 @@ class Kronolith
         /* Resource calendars (this would only be populated if explicitly
          * requested in the request, so include them if this is set regardless
          * of $calendars value).
+         *
+         * @TODO: Should we disallow even *viewing* these if the user is not an
+         *        admin?
          */
         if (!empty($GLOBALS['display_resource_calendars'])) {
             $driver = self::getDriver('Resource');
index f03e5b9..f22da70 100644 (file)
@@ -36,7 +36,7 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base
     public function addEvent($event)
     {
         /* Get a driver for this resource's calendar */
-        $driver = Kronolith::getDriver(null, $this->calendar);
+        $driver = Kronolith::getDriver('Resource', $this->calendar);
         /* Make sure it's not already attached. */
         $uid = $event->getUID();
         $existing = $driver->getByUID($uid, array($this->calendar));
@@ -60,7 +60,9 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base
      */
     public function removeEvent($event)
     {
-
+        $driver = Kronolith::getDriver('Resource', $this->calendar);
+        $re = $driver->getByUID($event->getUID(), array($this->calendar));
+        $driver->deleteEvent($re->getId());
     }
 
     /**
index 23a5710..bd259da 100644 (file)
@@ -69,7 +69,7 @@ class Kronolith_View_Week {
             $day->mday++;
         }
         $endDate = new Horde_Date($day);
-        $allevents = Kronolith::listEvents($this->startDate, $endDate, $GLOBALS['display_calendars']);
+        $allevents = Kronolith::listEvents($this->startDate, $endDate);
         if (is_a($allevents, 'PEAR_Error')) {
             $GLOBALS['notification']->push($allevents, 'horde.error');
             $allevents = array();
index 6a6e828..7dd71a9 100644 (file)
@@ -6,8 +6,60 @@
 require_once dirname(__FILE__) . '/lib/base.php';
 
 $title = _("Edit resources");
+
+$resources = array();
+$resources = Kronolith::listResources();
+$display_url_base = Horde::applicationUrl('month.php', true, -1);
+
+
 require KRONOLITH_TEMPLATES . '/common-header.inc';
 require KRONOLITH_TEMPLATES . '/menu.inc';
+
+?>
+<!-- Break out into template -->
+<div id="page">
+
+<h1 class="header">
+ <?php echo _("Resources") ?>
+</h1>
+<?php if (Horde_Auth::isAdmin()): ?>
+ <form method="get" action="createresource.php">
+  <?php echo Horde_Util::formInput() ?>
+  <input type="submit" class="button" value="<?php echo _("Create a new Resource") ?>" />
+ </form>
+<?php endif; ?>
+<table summary="<?php echo _("Resource List") ?>" cellspacing="0" id="calendar-list" class="striped sortable">
+ <thead>
+  <tr>
+   <th class="sortdown"><?php echo _("Name") ?></th>
+   <th class="calendar-list-url nosort"><?php echo _("Display URL") ?></th>
+  </tr>
+ </thead>
+ <tbody>
+<?php foreach ($resources as $resource): ?>
+ <tr>
+  <td><?php echo htmlspecialchars($resource->name) ?></td>
+  <td><?php $url = Horde_Util::addParameter($display_url_base, 'display_cal', $resource->calendar, false); echo Horde::link($url, _("Click or copy this URL to display this calendar"), '', '_blank') . htmlspecialchars(shorten_url($url)) . '</a>' ?></td>
+ </tr>
+<?php endforeach; ?>
+</tbody>
+</table>
+<?php
+/**
+ * Show just the beginning and end of long URLs.
+ */
+function shorten_url($url, $separator = '...', $first_chunk_length = 35, $last_chunk_length = 15)
+{
+    $url_length = strlen($url);
+    $max_length = $first_chunk_length + strlen($separator) + $last_chunk_length;
+
+    if ($url_length > $max_length) {
+        return substr_replace($url, $separator, $first_chunk_length, -$last_chunk_length);
+    }
+
+    return $url;
+}
+var_dump($resources);
 /* Test creating a new resource */
 //$new = array('name' => _("Another Big Meeting Room"),
 //             'category' => 'conference rooms');
@@ -17,11 +69,11 @@ require KRONOLITH_TEMPLATES . '/menu.inc';
 //var_dump($results);
 
 /* Test adding resource to event */
-$resource = Kronolith::getResource(9);
+$resource = Kronolith::getDriver('Resource')->getResource(9);
 
 /* Any driver/event */
 $driver = Kronolith::getDriver('Sql');
-$event = $driver->getByUID('20090831145736.98833ff01g9m338k@localhost');
+$event = $driver->getByUID('20090903183552.146242hgs864c92c@localhost');
 $event->addResource($resource, Kronolith::RESPONSE_NONE);
 $event->save();
 
@@ -30,4 +82,6 @@ $event->save();
 ////var_dump($resource->getFreeBusy(null, null, true));
 //
 /* Test listing resources */
-//var_dump(Kronolith::listResources());
\ No newline at end of file
+//var_dump(Kronolith::listResources());
+?>
+</div>
\ No newline at end of file