Commits

Anonymous committed 8406a37

Added 2 actions to Admin controller, which lists members then render a special view which is designed for ajax calls ( doesn't extend main )

Also added action sendMail, and it's view
but it is not completed yet , now it only list members by calling admin.ListMembers via ajax

Comments (0)

Files changed (6)

app/controllers/Admin.java

 package controllers;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import models.Member;
 			if (Account.connectedMember() == null) {
 				Auth.login(Account.request.action);
 			}
-			if ((secure.admin() && ! Account.connectedMember().isAdmin())) {
+			if ((secure.admin() && !Account.connectedMember().isAdmin())) {
 				forbidden();
 			}
 		}
 	}
-	
+
 	public static void index() {
 		renderText("Test");
 	}
-	
-	@Secure(admin=true)
+
+	@Secure(admin = true)
 	public static void list(int page) {
 		List<Member> members = Member.all().fetch(page, Account.pageSize);
 		render(members);
 	}
+
+	public static void membersList(String q, int page) {
+		StringBuilder query = new StringBuilder("");
+		for (String field : Member.getQueryableFields())
+			query.append(field + " like ?1 OR ");
+		// removes the last "OR"
+		query.delete(query.length() - 4, query.length() - 1); 
+		renderArgs.put("members", Member.find(query.toString(), "%" + q + "%")
+				.fetch(page, Account.pageSize));
+		renderArgs.put("count", Member.count(query.toString(), "%" + q + "%"));
+		renderTemplate("Admin/membersList.html");
+	}
+
+	public static void membersSearch(int page) {
+		StringBuilder query = new StringBuilder("");
+		ArrayList<String> qParams = new ArrayList<String>();
+		int i = 1;
+		for (String field : Member.getQueryableFields()) {
+			String value = params.get(field);
+			if (value == null || value.isEmpty())
+				continue;
+			query.append(field + " like ?" + i + " AND ");
+			qParams.add("%" + value + "%");
+			i++;
+		}
+		// to avoid StringIndexOutOfBoundsException, in case of no valid args were sent
+		if (i > 1) 
+			// removes the last "AND"
+			query.delete(query.length() - 5, query.length() - 1);
+		renderArgs.put(
+				"members",
+				Member.find(query.toString(), qParams.toArray()).fetch(page,
+						Account.pageSize));
+		renderArgs.put("count",
+				Member.count(query.toString(), qParams.toArray()));
+		renderTemplate("Admin/membersList.html");
+	}
+
+	public static void sendMail() {
+		render();
+	}
 }

app/models/Member.java

 package models;
 
+import java.lang.reflect.Field;
 import java.util.*;
 
 import javax.persistence.*;
 	public static List<Member> findAll(int page, int pageSize) {
 		return Member.all().fetch(page, pageSize);
 	}
+	
+	public static boolean isInvaildField(String f) {
+		try {Member.class.getField(f);} 
+    	catch (SecurityException e) 	{return true;} 
+		catch (NoSuchFieldException e) 	{return true;}
+		return false ;
+	}
+	
+	public static List<String> getQueryableFields() {
+		List<String> queryableFields = new ArrayList() ;
+		List<String> forbiddenFields = Arrays.asList(new String[]{"password", "passwordConfirmation", "willBeSaved"}) ;
+		for (Field field : Member.class.getFields())
+			if (!forbiddenFields.contains(field.getName())) 
+				queryableFields.add(field.getName()) ;
+		return queryableFields;
+	}
 }

app/views/Admin/membersList.html

+#{set title:messages.get('website.title') /}
+<script type="text/javascript" version="1.0">
+	*{ Data about the result, for javascript manipulation }*
+	var membersCount = ${count};
+</script>
+%{reqFields = ['firstName','lastName','email'] }%
+<table id="members_table" border="3" class="left text_align">
+	<thead>
+		<th></th>
+		#{list items: reqFields, as: 'field'}
+			<th>
+				&{"registration."+field}
+			</th>
+		#{/list}
+	</thead>
+	%{ i = 0 ;}%
+	#{list items: members, as: 'member'}
+	<tr class="${member_parity}" id="memberRow${i}">
+		<td>
+			<input type="checkbox" value="${i}">
+		</td>
+		#{list items: reqFields, as: 'field'}
+			<td id="${field+i}">
+				${member[field]}
+			</td>
+		#{/list}
+	</tr>
+	%{ i++ ;}%
+	#{/list}
+</table>
+<form>
+	%{ i = 0 ;}%
+	#{list items: members, as: 'member'}
+	<input type="hidden" id="idValue${i}" value="${member.id}" />
+	<input type="hidden" id="emailValue${i}" value="${member.email}" />
+	%{ i++ ;}%
+	#{/list}
+</form>

app/views/Admin/sendMail.html

+#{extends 'main.html' /}
+#{set title:messages.get('website.title') /}
+<script src="@{'/public/javascripts/jquery-1.4.3.min.js'}" type="text/javascript" charset="utf-8"></script>
+<script type="text/javascript">
+	var emails = new Array() ;
+	var p = 0 ;  // page -- TODO: think how to get this value ??
+	
+	function quickSearch(query,p) { 
+	   var listAction = #{jsAction @membersList(':q', ':page') /} ;
+	   $('#memTable').empty().load(listAction({q: query, page: 0}), function() {
+	   		// Add Select/UnSelect behaviour 
+			$("input:checkbox").each(function(){
+				$(this).click(function(){
+					alert(this.value)
+				})
+			});
+	   })
+	}
+</script>
+
+<style>
+	div.form, .usersList { display: inline-block ;  border:1px solid black ; width:39%}
+	div.memList { display: inline-block ;  border:1px solid black ; width:55% ; padding:10px}
+</style>
+<h1>&{'admin.mailer.head'}</h1>
+<div class="wrapper">
+	<div class="form">
+		#{form @sendMail(), id:'adminMailForm', method:"POST", enctype:'multipart/form-data'}
+			<label for="subject">&{'admin.mail.subject'}</label>
+			<input type="text" name="subject" value="" />
+			<br>
+			
+			<label for="to">&{'admin.mail.to'}</label>
+			<input type="hidden" name="subject" value="" />
+			<label id="mailsCount">&{'admin.mail.to.user'}</label>
+			<br>
+			
+			<label for="content">&{'admin.mail.content'}</label><br>
+			<textarea style="width:90%"></textarea><br>
+			
+			<input type="submit" value="&{'admin.mail.send'}"/>
+		#{/form}
+	</div>
+	<div class="memList">
+		<!-- Users Advanced Searching and Filtering Panel -->
+		<div class="memAdvancedSearch">
+			<span>Advanced Search</span><br>
+			First Name <input type="text"/><br>
+			Last Name <input type="text"/><br>
+			Email <input type="text"/><br>
+			<input value="Filter" type="button" onClick="$('#result').load(listAction({page: '0', query: document.getElementById('filterBy'), value: document.getElementById('value')})" />
+		</div>
+		
+		<!-- Users Searching and Filtering Panel -->
+		<div class="memSearch">
+			Search <input id="q" type="text" onkeyup="quickSearch(this.value)"/> 
+			<a href="#advanced">(Advanced Search)</a>
+			<!--input value="Filter" type="button" onClick="$('#result').load(listAction({page: '0', query: document.getElementById('filterBy'), value: document.getElementById('value')})" /-->
+		</div>
+		
+		<!-- Showing results (members table) -->
+		<div id="memTable"></div>
+		
+		<!-- results paging -->
+		<div id="memPageing"></div>
+	</div>
+</div>
 mail.verification.accountVerified = تم تفعيل حسابك
 mail.verification.wrongToken =  عفواً ! خطأ في التفعيل
 
+# Admininstration
+admin.mail.Previous = السابق
+admin.mail.Next = التالي
+ 
+
 # General
 Ok = موافق
 Cancel = الغاء
 Required = لا يمكنك ترك هذا
 Incorrect Value = قيمة خطأ
+
+members.list.noResults = لا توجد نتائج
 GET		/admin									Admin.index
 GET		/admin/									Admin.index
 GET		/admin/members/list						Admin.list
+GET		/admin/members/mail						Admin.sendMail
+GET		/admin/members/mail/list/{q}			Admin.membersList
+GET		/admin/members/mail/search				Admin.membersSearch
 
 *		/uploadphoto							Account.uploadPhoto
 GET		/getcountrieslist						Account.getCountryCitiesList