Ejemplo n.º 1
0
 /**
  * @param Whups_Query $query The query to save.
  * @throws Whups_Exception
  */
 public function save(Whups_Query $query)
 {
     if ($query->id) {
         // Query already exists; get its share and update the name
         // if necessary.
         try {
             $share = $this->_shareManager->getShareById($query->id);
         } catch (Horde_Exception_NotFound $e) {
             // Share has an id but doesn't exist; just throw an
             // error.
             throw new Whups_Exception($e);
         }
         if ($share->get('name') != $query->name || $share->get('slug') != $query->slug) {
             $share->set('name', $query->name);
             $share->set('slug', $query->slug);
             $share->save();
         }
     } else {
         // Create a new share for the query.
         $share = $this->_shareManager->newShare($GLOBALS['registry']->getAuth(), (string) new Horde_Support_Uuid(), $query->name);
         $share->set('slug', $query->slug);
         try {
             $this->_shareManager->addShare($share);
         } catch (Horde_Share_Exception $e) {
             throw new Whups_Exception($e);
         }
         $query->id = $share->getId();
     }
     // Update the queries table.
     $GLOBALS['whups_driver']->saveQuery($query);
 }
Ejemplo n.º 2
0
 /**
  * Create and initialise a new gallery object.
  *
  * @param array $attributes             The gallery attributes.
  * @param Horde_Perms_Permission $perm  The permissions for the gallery if
  *                                      the defaults are not desirable.
  * @param integer $parent               The id of the parent gallery (if any)
  *
  * @return Ansel_Gallery  A new gallery object.
  * @throws Ansel_Exception
  */
 public function createGallery(array $attributes = array(), Horde_Perms_Permission $perm = null, $parent = null)
 {
     // Required values.
     if (empty($attributes['owner'])) {
         $attributes['owner'] = $GLOBALS['registry']->getAuth();
     }
     if (empty($attributes['name'])) {
         $attributes['name'] = _("Unnamed");
     }
     if (empty($attributes['desc'])) {
         $attributes['desc'] = '';
     }
     // Default values
     $attributes['default_type'] = isset($attributes['default_type']) ? $attributes['default_type'] : 'auto';
     $attributes['default'] = isset($attributes['default']) ? (int) $attributes['default'] : 0;
     $attributes['default_prettythumb'] = isset($attributes['default_prettythumb']) ? $attributes['default_prettythumb'] : '';
     // No value for style now means to use the 'default_ansel' style as
     // defined in styles.php
     $attributes['style'] = isset($attributes['style']) ? $attributes['style'] : '';
     $attributes['date_created'] = time();
     $attributes['last_modified'] = $attributes['date_created'];
     $attributes['images'] = isset($attributes['images']) ? (int) $attributes['images'] : 0;
     $attributes['slug'] = isset($attributes['slug']) ? $attributes['slug'] : '';
     $attributes['age'] = isset($attributes['age']) ? (int) $attributes['age'] : 0;
     $attributes['download'] = isset($attributes['download']) ? $attributes['download'] : $GLOBALS['prefs']->getValue('default_download');
     $attributes['view_mode'] = isset($attributes['view_mode']) ? $attributes['view_mode'] : 'Normal';
     $attributes['passwd'] = isset($attributes['passwd']) ? $attributes['passwd'] : '';
     // Don't pass tags to the share creation method.
     if (isset($attributes['tags'])) {
         $tags = $attributes['tags'];
         unset($attributes['tags']);
     } else {
         $tags = array();
     }
     // Check for slug uniqueness
     if (!empty($attributes['slug']) && $this->galleryExists(null, $attributes['slug'])) {
         throw new Ansel_Exception(sprintf(_("The slug \"%s\" already exists."), $attributes['slug']));
     }
     // Create the gallery's share, and then the gallery.
     try {
         $gallery_share = $this->_shares->newShare($GLOBALS['registry']->getAuth(), strval(new Horde_Support_Randomid()), $attributes['name']);
     } catch (Horde_Share_Exception $e) {
         Horde::log($e->getMessage, 'ERR');
         throw new Ansel_Exception($e);
     }
     $gallery = $this->buildGallery($gallery_share);
     // Set the gallery's parent if needed, and clear the parent's cache
     if (!is_null($parent)) {
         $gallery->setParent($parent);
         if ($GLOBALS['conf']['ansel_cache']['usecache']) {
             $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $parent);
         }
     }
     // Fill up the new gallery
     foreach ($attributes as $key => $value) {
         if ($key != 'name') {
             $gallery->set($key, $value);
         }
     }
     // Save it to storage
     try {
         $result = $this->_shares->addShare($gallery_share);
     } catch (Horde_Share_Exception $e) {
         $error = sprintf(_("The gallery \"%s\" could not be created: %s"), $attributes['name'], $e->getMessage());
         Horde::log($error, 'ERR');
         throw new Ansel_Exception($error);
     }
     // Add default permissions.
     if (empty($perm)) {
         $perm = $gallery->getPermission();
         // Default permissions for logged in users
         switch ($GLOBALS['prefs']->getValue('default_permissions')) {
             case 'read':
                 $perms = Horde_Perms::SHOW | Horde_Perms::READ;
                 break;
             case 'edit':
                 $perms = Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::EDIT;
                 break;
             case 'none':
                 $perms = 0;
                 break;
         }
         $perm->addDefaultPermission($perms, false);
         // Default guest permissions
         switch ($GLOBALS['prefs']->getValue('guest_permissions')) {
             case 'read':
                 $perms = Horde_Perms::SHOW | Horde_Perms::READ;
                 break;
             case 'none':
             default:
                 $perms = 0;
                 break;
         }
         $perm->addGuestPermission($perms, false);
         // Default user groups permissions
         switch ($GLOBALS['prefs']->getValue('group_permissions')) {
             case 'read':
                 $perms = Horde_Perms::SHOW | Horde_Perms::READ;
                 break;
             case 'edit':
                 $perms = Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::EDIT;
                 break;
             case 'delete':
                 $perms = Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::EDIT | Horde_Perms::DELETE;
                 break;
             case 'none':
             default:
                 $perms = 0;
                 break;
         }
         if ($perms) {
             $group_list = $GLOBALS['injector']->getInstance('Horde_Group')->listGroups($GLOBALS['registry']->getAuth());
             if (count($group_list)) {
                 foreach ($group_list as $group_id => $group_name) {
                     $perm->addGroupPermission($group_id, $perms, false);
                 }
             }
         }
     }
     $gallery->setPermission($perm);
     // Initial tags
     if (count($tags)) {
         $gallery->setTags($tags);
     }
     return $gallery;
 }