~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/quotas/DiskQuotaOption.inc

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2005-11-29 15:50:12 UTC
  • Revision ID: james.westby@ubuntu.com-20051129155012-wtophp03lu01kdgl
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * $RCSfile: DiskQuotaOption.inc,v $
 
4
 *
 
5
 * Gallery - a web based photo album viewer and editor
 
6
 * Copyright (C) 2000-2005 Bharat Mediratta
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or (at
 
11
 * your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 
21
 */
 
22
/**
 
23
 * @version $Revision: 1.5 $ $Date: 2005/08/23 03:49:49 $
 
24
 * @package Quotas
 
25
 * @subpackage UserInterface
 
26
 * @author Robert Balousek <volksport@users.sf.net>
 
27
 */
 
28
 
 
29
/**
 
30
 * This ItemAddOption enforces disk quotas
 
31
 *
 
32
 * @package Quotas
 
33
 * @subpackage UserInterface
 
34
 */
 
35
 
 
36
class DiskQuotaOption extends ItemAddOption {
 
37
 
 
38
    /**
 
39
     * @see ItemAddOption::isAppropriate
 
40
     */
 
41
    function isAppropriate() {
 
42
        return array(GalleryStatus::success(), true);
 
43
    }
 
44
 
 
45
    /**
 
46
     * @see ItemAddOption::handleRequestAfterAdd
 
47
     */
 
48
    function handleRequestAfterAdd($form, $items) {
 
49
        GalleryCoreApi::relativeRequireOnce('modules/quotas/classes/GalleryQuotasHelper.class');
 
50
 
 
51
        global $gallery;
 
52
        $warnings = array();
 
53
        $errors = array();
 
54
 
 
55
        /* user's disk quota in KB */
 
56
        list ($ret, $quotaExists, $userDiskQuota) =
 
57
            GalleryQuotasHelper::getUserDiskQuota($gallery->getActiveUserId());
 
58
        if ($ret->isError()) {
 
59
            return array($ret->wrap(__FILE__, __LINE__), null, null);
 
60
        }
 
61
 
 
62
        /* if user has a quota */
 
63
        if ($quotaExists) {
 
64
            $excludedIds = array();
 
65
            for ($j = 0; $j < count($items); $j++) {
 
66
                $excludedIds[] = $items[$j]->getId();
 
67
            }
 
68
            list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'quotas');
 
69
            if ($ret->isError()) {
 
70
                return array($ret->wrap(__FILE__, __LINE__), null, null);
 
71
            }
 
72
            for ($i = 0; $i < count($items); $i++) {
 
73
                $warnings[$i] = array();
 
74
 
 
75
                /* we should come here only if we are adding a data item, not sub-album */
 
76
                $albumId = $items[$i]->getParentId();
 
77
 
 
78
                /* calculate the item ids of the newly added items */
 
79
                array_shift($excludedIds);
 
80
 
 
81
                /* user's disk usage in Bytes */
 
82
                list ($ret, $userDiskUsage) = GalleryQuotasHelper::getUserDiskUsage(
 
83
                    $gallery->getActiveUserId(), $excludedIds);
 
84
                if ($ret->isError()) {
 
85
                    return array($ret->wrap(__FILE__, __LINE__), null, null);
 
86
                }
 
87
 
 
88
                /* make disk usage into KB */
 
89
                $userDiskUsage /= 1024.00;
 
90
 
 
91
                if ($userDiskUsage > $userDiskQuota) {
 
92
                    list ($ret, $userDiskQuotaHumanReadable, $userDiskQuotaHumanReadableUnit) =
 
93
                        GalleryQuotasHelper::humanReadableFromKilobytes($userDiskQuota);
 
94
                    if ($ret->isError()) {
 
95
                        return array($ret->wrap(__FILE__, __LINE__), null, null);
 
96
                    }
 
97
                    $warnings[$i][] = $module->translate(
 
98
                        array('text' => 'Warning: You have reached your disk quota (%s %s), this item will not be added.',
 
99
                              'arg1' => $userDiskQuotaHumanReadable,
 
100
                              'arg2' => $userDiskQuotaHumanReadableUnit));
 
101
 
 
102
                    $gallery->addShutdownAction(
 
103
                        array('GalleryCoreApi', 'deleteEntityById'), array($items[$i]->getId()));
 
104
                } else {
 
105
                    list ($ret, $userDiskQuotaHumanReadable, $userDiskQuotaHumanReadableUnit) = 
 
106
                        GalleryQuotasHelper::humanReadableFromKilobytes($userDiskQuota);
 
107
                    if ($ret->isError()) {
 
108
                        return array($ret->wrap(__FILE__, __LINE__), null, null);
 
109
                    }
 
110
                    list ($ret, $userDiskUsageHumanReadable, $userDiskUsageHumanReadableUnit) = 
 
111
                        GalleryQuotasHelper::humanReadableFromKilobytes($userDiskUsage);
 
112
                    if ($ret->isError()) {
 
113
                        return array($ret->wrap(__FILE__, __LINE__), null, null);
 
114
                    }
 
115
 
 
116
                    $warnings[$i][] = $module->translate(
 
117
                        array('text' => 'You are using %s %s of your allotted %s %s.',
 
118
                              'arg1' => $userDiskUsageHumanReadable,
 
119
                              'arg2' => $userDiskUsageHumanReadableUnit,
 
120
                              'arg3' => $userDiskQuotaHumanReadable,
 
121
                              'arg4' => $userDiskQuotaHumanReadableUnit));
 
122
                }
 
123
            }
 
124
        }
 
125
        return array(GalleryStatus::success(), $errors, $warnings);
 
126
    }
 
127
 
 
128
}
 
129
?>