Skip to content
Snippets Groups Projects
Commit 46c42b91 authored by Jared Hancock's avatar Jared Hancock
Browse files

orm: Convert staff directory to ORM

parent 2b422844
No related branches found
No related tags found
No related merge requests found
......@@ -381,6 +381,14 @@ class Dept extends VerySimpleModel {
$query->filter(array(
'manager_id' => is_object($manager)?$manager->getId():$manager));
if (isset($criteria['nonempty'])) {
$query->annotate(array(
'user_count' => SqlAggregate::COUNT('members')
))->filter(array(
'user_count__gt' => 0
));
}
$query->order_by('name')
->values_flat('id', 'name');
......
......@@ -6,59 +6,67 @@ $from='FROM '.STAFF_TABLE.' staff '.
'LEFT JOIN '.DEPT_TABLE.' dept ON(staff.dept_id=dept.dept_id) ';
$where='WHERE staff.isvisible=1 ';
$agents = Staff::objects()
->filter(array('isvisible'=>1))
->select_related('dept');
if($_REQUEST['q']) {
$searchTerm=$_REQUEST['q'];
if($searchTerm){
$query=db_real_escape($searchTerm,false); //escape the term ONLY...no quotes.
if(is_numeric($searchTerm)){
$where.=" AND (staff.phone LIKE '%$query%' OR staff.phone_ext LIKE '%$query%' OR staff.mobile LIKE '%$query%') ";
$agents->filter(Q::any(array(
'phone__contains'=>$searchTerm,
'phone_ext__contains'=>$searchTerm,
'mobile__contains'=>$searchTerm,
)));
}elseif(strpos($searchTerm,'@') && Validator::is_email($searchTerm)){
$where.=" AND staff.email='$query'";
$agents->filter(array('email'=>$searchTerm));
}else{
$where.=" AND ( staff.email LIKE '%$query%'".
" OR staff.lastname LIKE '%$query%'".
" OR staff.firstname LIKE '%$query%'".
' ) ';
$agents->filter(Q::any(array(
'email__contains'=>$searchTerm,
'lastname__contains'=>$searchTerm,
'firstname__contains'=>$searchTerm,
)));
}
}
}
if($_REQUEST['did'] && is_numeric($_REQUEST['did'])) {
$where.=' AND staff.dept_id='.db_input($_REQUEST['did']);
$agents->filter(array('dept'=>$_REQUEST['did']));
$qstr.='&did='.urlencode($_REQUEST['did']);
}
$sortOptions=array('name'=>'staff.firstname,staff.lastname','email'=>'staff.email','dept'=>'dept.name',
'phone'=>'staff.phone','mobile'=>'staff.mobile','ext'=>'phone_ext',
'created'=>'staff.created','login'=>'staff.lastlogin');
$orderWays=array('DESC'=>'DESC','ASC'=>'ASC');
$sortOptions=array('name'=>'firstname,lastname','email'=>'email','dept'=>'dept__name',
'phone'=>'phone','mobile'=>'mobile','ext'=>'phone_ext',
'created'=>'created','login'=>'lastlogin');
$orderWays=array('DESC'=>'-','ASC'=>'');
$sort=($_REQUEST['sort'] && $sortOptions[strtolower($_REQUEST['sort'])])?strtolower($_REQUEST['sort']):'name';
//Sorting options...
if($sort && $sortOptions[$sort]) {
$order_column =$sortOptions[$sort];
}
$order_column=$order_column?$order_column:'staff.firstname,staff.lastname';
$order_column = $order_column ?: 'firstname,lastname';
if($_REQUEST['order'] && $orderWays[strtoupper($_REQUEST['order'])]) {
$order=$orderWays[strtoupper($_REQUEST['order'])];
}
$order=$order?$order:'ASC';
if($order_column && strpos($order_column,',')){
$order_column=str_replace(','," $order,",$order_column);
}
$x=$sort.'_sort';
$$x=' class="'.strtolower($order).'" ';
$order_by="$order_column $order ";
$$x=' class="'.strtolower($_REQUEST['order'] ?: 'desc').'" ';
foreach (explode(',', $order_column) as $C) {
$agents->order_by($order.$C);
}
$total=db_count('SELECT count(DISTINCT staff.staff_id) '.$from.' '.$where);
$total=$agents->count();
$page=($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
$pageNav=new Pagenate($total, $page, PAGE_LIMIT);
$pageNav->setURL('directory.php',$qstr.'&sort='.urlencode($_REQUEST['sort']).'&order='.urlencode($_REQUEST['order']));
$pageNav->paginate($agents);
//Ok..lets roll...create the actual query
$qstr.='&order='.($order=='DESC'?'ASC':'DESC');
$query="$select $from $where GROUP BY staff.staff_id ORDER BY $order_by LIMIT ".$pageNav->getStart().",".$pageNav->getLimit();
//echo $query;
$qstr.='&order='.($order=='-'?'ASC':'DESC');
?>
<h2><?php echo __('Agents');?>
&nbsp;<i class="help-tip icon-question-sign" href="#staff_members"></i></h2>
......@@ -68,16 +76,10 @@ $query="$select $from $where GROUP BY staff.staff_id ORDER BY $order_by LIMIT ".
<select name="did" id="did">
<option value="0">&mdash; <?php echo __('All Departments');?> &mdash;</option>
<?php
$sql='SELECT dept.dept_id, dept.name as dept,count(staff.staff_id) as users '.
'FROM '.DEPT_TABLE.' dept '.
'INNER JOIN '.STAFF_TABLE.' staff ON(staff.dept_id=dept.dept_id AND staff.isvisible=1) '.
'GROUP By dept.dept_id HAVING users>0 ORDER BY dept.name';
if(($res=db_query($sql)) && db_num_rows($res)){
while(list($id,$name, $users)=db_fetch_row($res)){
$sel=($_REQUEST['did'] && $_REQUEST['did']==$id)?'selected="selected"':'';
echo sprintf('<option value="%d" %s>%s (%s)</option>',$id,$sel,$name,$users);
}
}
foreach (Dept::getDepartments(array('nonempty'=>1)) as $id=>$name) {
$sel=($_REQUEST['did'] && $_REQUEST['did']==$id)?'selected="selected"':'';
echo sprintf('<option value="%d" %s>%s</option>',$id,$sel,$name);
}
?>
</select>
&nbsp;&nbsp;
......@@ -87,8 +89,7 @@ $query="$select $from $where GROUP BY staff.staff_id ORDER BY $order_by LIMIT ".
</div>
<div class="clear"></div>
<?php
$res=db_query($query);
if($res && ($num=db_num_rows($res)))
if ($agents->exists(true))
$showing=$pageNav->showing();
else
$showing=__('No agents found!');
......@@ -107,24 +108,23 @@ else
</thead>
<tbody>
<?php
if($res && db_num_rows($res)):
$ids=($errors && is_array($_POST['ids']))?$_POST['ids']:null;
while ($row = db_fetch_array($res)) { ?>
<tr id="<?php echo $row['staff_id']; ?>">
<td>&nbsp;<?php echo Format::htmlchars($row['name']); ?></td>
<td>&nbsp;<?php echo Format::htmlchars($row['dept']); ?></td>
<td>&nbsp;<?php echo Format::htmlchars($row['email']); ?></td>
<td>&nbsp;<?php echo Format::phone($row['phone']); ?></td>
<td>&nbsp;<?php echo $row['phone_ext']; ?></td>
<td>&nbsp;<?php echo Format::phone($row['mobile']); ?></td>
</tr>
$ids=($errors && is_array($_POST['ids']))?$_POST['ids']:null;
foreach ($agents as $A) { ?>
<tr id="<?php echo $A->staff_id; ?>">
<td>&nbsp;<?php echo Format::htmlchars($A->getName()); ?></td>
<td>&nbsp;<?php echo Format::htmlchars((string) $A->dept); ?></td>
<td>&nbsp;<?php echo Format::htmlchars($A->email); ?></td>
<td>&nbsp;<?php echo Format::phone($A->phone); ?></td>
<td>&nbsp;<?php echo $A->phone_ext; ?></td>
<td>&nbsp;<?php echo Format::phone($A->mobile); ?></td>
</tr>
<?php
} //end of while.
endif; ?>
} // end of foreach
?>
<tfoot>
<tr>
<td colspan="6">
<?php if($res && $num) {
<?php if ($agents->exists(true)) {
echo '<div>&nbsp;'.__('Page').':'.$pageNav->getPageLinks().'&nbsp;</div>';
?>
<?php } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment