From 523b0966d22d0fc149ca64afb32995a0eb29a6d3 Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind1991@gmail.com>
Date: Mon, 15 Aug 2011 21:06:29 +0200
Subject: [PATCH] add option to set user quota

---
 settings/ajax/setquota.php   | 22 ++++++++++++++++++++
 settings/js/users.js         | 39 ++++++++++++++++++++++++++++++++++++
 settings/templates/users.php |  4 ++++
 settings/users.php           |  2 +-
 4 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 settings/ajax/setquota.php

diff --git a/settings/ajax/setquota.php b/settings/ajax/setquota.php
new file mode 100644
index 0000000000..244a85e3d9
--- /dev/null
+++ b/settings/ajax/setquota.php
@@ -0,0 +1,22 @@
+<?php
+
+// Init owncloud
+require_once('../../lib/base.php');
+
+// We send json data
+header( "Content-Type: application/jsonrequest" );
+
+// Check if we are a user
+if( !OC_User::isLoggedIn() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
+	echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
+	exit();
+}
+
+$username = $_POST["username"];
+$quota= OC_Helper::computerFileSize($_POST["quota"]);
+
+// Return Success story
+OC_Preferences::setValue($username,'files','quota',$quota);
+echo json_encode( array( "status" => "success", "data" => array( "username" => $username ,'quota'=>$quota)));
+
+?>
diff --git a/settings/js/users.js b/settings/js/users.js
index bc1b8fc3ed..3122f5614c 100644
--- a/settings/js/users.js
+++ b/settings/js/users.js
@@ -75,6 +75,45 @@ $(document).ready(function(){
 	$('td.password').live('click',function(event){
 		$(this).children('img').click();
 	});
+
+	$('td.quota>img').live('click',function(event){
+		event.stopPropagation();
+		var img=$(this);
+		var uid=img.parent().parent().data('uid');
+		var input=$('<input>');
+		var quota=img.parent().children('span').text();
+		img
+		if(quota=='None'){
+			quota='';
+		}
+		input.val(quota);
+		img.css('display','none');
+		img.parent().children('span').replaceWith(input);
+		input.focus();
+		input.keypress(function(event) {
+			if(event.keyCode == 13) {
+				$(this).parent().attr('data-quota',$(this).val());
+				if($(this).val().length>0){
+					$.post(
+						OC.filePath('settings','ajax','setquota.php'),
+						   {username:uid,quota:$(this).val()},
+						   function(result){}
+					);
+					input.blur();
+				}else{
+					input.blur();
+				}
+			}
+		});
+		input.blur(function(){
+			var quota=$(this).parent().data('quota');
+			$(this).replaceWith($('<span>'+quota+'</span>'));
+			img.css('display','');
+		});
+	});
+	$('td.quota').live('click',function(event){
+		$(this).children('img').click();
+	});
 	
 	$('#newuser').submit(function(event){
 		event.preventDefault();
diff --git a/settings/templates/users.php b/settings/templates/users.php
index 01e2adf4e9..9733c3e9a5 100644
--- a/settings/templates/users.php
+++ b/settings/templates/users.php
@@ -31,6 +31,10 @@ foreach($_["groups"] as $group) {
 					<?php endforeach;?>
 				</select>
 			</td>
+			<td class="quota" data-quota="<?php echo $user['quota']?>">
+				<span><?php echo ($user['quota']>0)?$user['quota']:'None';?></span>
+				<img class="svg action" src="<?php echo image_path('core','actions/rename.svg')?>" alt="set new password" title="set quota" />
+			</td>
 			<td class="remove">
 				<?php if($user['name']!=OC_User::getUser()):?>
 					<img alt="Delete" title="<?php echo $l->t('Delete')?>" class="svg action" src="<?php echo image_path('core','actions/delete.svg') ?>" />
diff --git a/settings/users.php b/settings/users.php
index cd7d04a55e..8bf64e16ff 100644
--- a/settings/users.php
+++ b/settings/users.php
@@ -38,7 +38,7 @@ $users = array();
 $groups = array();
 
 foreach( OC_User::getUsers() as $i ){
-	$users[] = array( "name" => $i, "groups" => join( ", ", OC_Group::getUserGroups( $i ) ));
+	$users[] = array( "name" => $i, "groups" => join( ", ", OC_Group::getUserGroups( $i ) ),'quota'=>OC_Helper::humanFileSize(OC_Preferences::getValue($i,'files','quota',0)));
 }
 
 foreach( OC_Group::getGroups() as $i ){
-- 
GitLab