402 lines
12 KiB
PHP
402 lines
12 KiB
PHP
<?php
|
|
|
|
/** Customer List
|
|
* Lists all customers
|
|
*
|
|
* @version 1.0.3
|
|
* @since 2007-09-22
|
|
* @author martin lenzelbauer
|
|
*
|
|
* @change 2007-10-13
|
|
* added download()
|
|
*
|
|
* @change 2007-11-15
|
|
* added lastExportDate
|
|
* changed doDownload() - introduced different download types (all, only customers with complete address data)
|
|
*
|
|
* @change 2008-01-15
|
|
* changed doPrintCustomerList() - list each e-mail address only once
|
|
*/
|
|
class CustomerList extends CmsObject{
|
|
|
|
var $sortProperty;
|
|
var $sortOrder;
|
|
var $filterProperty;
|
|
var $filter;
|
|
var $allowedProperties;
|
|
var $propertyLabels;
|
|
|
|
var $lastExportDate;
|
|
|
|
|
|
/** C'tor
|
|
*/
|
|
//-----------------------------------------------
|
|
function CustomerList($id, $parent){
|
|
//-----------------------------------------------
|
|
parent::CmsObject($id, $parent);
|
|
$this->name = "Besucher";
|
|
$this->filterProperty = "name";
|
|
$this->sortProperty = "name";
|
|
$this->sortOrder = "ASC";
|
|
$this->filter = "";
|
|
$this->allowedProperties = array("name", "address", "email", "events");
|
|
$this->propertyLabels = array("Name", "Anschrift", "E-Mail", "Veranstaltungen");
|
|
$this->lastExportDate = "0000-00-00";
|
|
}
|
|
|
|
|
|
/** installs a log table
|
|
*/
|
|
//-----------------------------------------------
|
|
function install(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_customerlist (
|
|
exportDate DATETIME,
|
|
data VARCHAR(32),
|
|
PRIMARY KEY (exportDate)
|
|
)");
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see CmsObject::load()
|
|
*/
|
|
//-----------------------------------------------
|
|
function load($path=array()){
|
|
//-----------------------------------------------
|
|
$query = sprintf("SELECT MAX(exportDate) FROM bruckm_customerlist");
|
|
$result = dbQuery($query);
|
|
if($line = mysqli_fetch_array($result, MYSQLI_ASSOC)){
|
|
$this->lastExportDate = $line['MAX(exportDate)'];
|
|
}
|
|
}
|
|
|
|
|
|
/** @see CmsObject::update()
|
|
*/
|
|
//-----------------------------------------------
|
|
function update(){
|
|
//-----------------------------------------------
|
|
$this->sortProperty = $_POST['sortProperty'];
|
|
$this->sortOrder = $_POST['sortOrder'];
|
|
$this->filterProperty = $_POST['filterProperty'];
|
|
$this->filter = $_POST['filter'];
|
|
}
|
|
|
|
|
|
/** @see CmsObject::printContent()
|
|
*/
|
|
//-----------------------------------------------
|
|
function printContent(){
|
|
//-----------------------------------------------
|
|
$out = $this->doPrintMetadata();
|
|
$out .= $this->doPrintErrors();
|
|
$out .= $this->doPrintCustomerList();
|
|
return $out;
|
|
}
|
|
|
|
|
|
/** @see CmsObject:doPrintMetadata()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doPrintMetadata(){
|
|
//-----------------------------------------------
|
|
$t = new Template(CMS_TEMPLATE_DIR."customerlist1.html");
|
|
$t->setVar("NAME", htmlspecialchars($this->name));
|
|
//sort properties
|
|
$p = "";
|
|
foreach($this->allowedProperties as $i=>$prop){
|
|
if($prop == $this->sortProperty){
|
|
$p .= "<option value=\"$prop\" selected=\"selected\">";
|
|
}
|
|
else{
|
|
$p .= "<option value=\"$prop\">";
|
|
}
|
|
$p .= htmlspecialchars($this->propertyLabels[$i])."</option>";
|
|
}
|
|
$t->setVar("SORT_PROPERTIES", $p);
|
|
if($this->sortOrder == "ASC"){
|
|
$t->setVar("ORDER_ASC", "selected=\"selected\"");
|
|
$t->setVar("ORDER_DESC", "");
|
|
}
|
|
else{
|
|
$t->setVar("ORDER_DESC", "selected=\"selected\"");
|
|
$t->setVar("ORDER_ASC", "");
|
|
}
|
|
//filter properties
|
|
$p = "";
|
|
foreach($this->allowedProperties as $i=>$prop){
|
|
if($prop == $this->filterProperty){
|
|
$p .= "<option value=\"$prop\" selected=\"selected\">";
|
|
}
|
|
else{
|
|
$p .= "<option value=\"$prop\">";
|
|
}
|
|
$p .= htmlspecialchars($this->propertyLabels[$i])."</option>";
|
|
}
|
|
$t->setVar("FILTER_PROPERTIES", $p);
|
|
$t->setVar("FILTER", $this->filter);
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
|
|
/** lists all customers, sorted & filtered
|
|
*/
|
|
//----------------------------------------------
|
|
function doPrintCustomerList(){
|
|
//----------------------------------------------
|
|
$result = $this->doLoadCustomers();
|
|
$email = array();
|
|
$list = "";
|
|
while($customer = mysqli_fetch_array($result, MYSQLI_ASSOC)){
|
|
if(!empty($customer['email']) && !in_array($customer['email'], $email)){
|
|
$email[] = $customer['email'];
|
|
$id = $customer['id'];
|
|
$c = "<tr onClick=\"showCustomer($id, this)\">";
|
|
$c .= "<td><div>".htmlspecialchars($customer['surname']." ".$customer['firstname'])." $customer[acad]</div></td>";
|
|
$c .= "<td><div>$customer[email] $customer[phone]</div></td>";
|
|
$c .= "<td><div>".htmlspecialchars($customer['zip']." ".$customer['city'])." ".htmlspecialchars($customer['address'])."</div></td>";
|
|
$c .= "<td><div>".htmlspecialchars($customer['events'])."</div></td>";
|
|
$c .= "<td width=\"20\"><div onMouseOver=\"showTooltip('Besucher löschen')\" onClick=\"setAction('deleteCustomer', $id); document.forms[0].submit();\"><img src=\"images/delete1.png\" alt=\"Besucher löschen\" onMouseOver=\"swapImage(this,2)\" onMouseOut=\"swapImage(this,1)\" onMouseDown=\"swapImage(this,3)\" onMouseUp=\"swapImage(this,2)\" /></div></td>";
|
|
$c .= "</tr>\n";
|
|
$list .= $c;
|
|
}
|
|
}
|
|
$t = new Template(CMS_TEMPLATE_DIR."customerlist2.html");
|
|
$t->setVar("CUSTOMERS", $list);
|
|
$t->setVar("COUNT", mysql_num_rows($result));
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
/** @see CmsObject::handleAction()
|
|
*/
|
|
//---------------------------------------------
|
|
function handleAction($action, $position=0, $type=NULL){
|
|
//---------------------------------------------
|
|
switch($action){
|
|
case "download_complete_all":
|
|
$this->doUpdateExportDate("complete only (all)");
|
|
$this->doDownload("complete only", false);
|
|
break;
|
|
case "download_complete_new":
|
|
$this->doUpdateExportDate("complete only (new)");
|
|
$this->doDownload("complete only", true);
|
|
break;
|
|
case "download_all":
|
|
$this->doUpdateExportDate("all (all)");
|
|
$this->doDownload("all", false);
|
|
break;
|
|
case "deleteCustomer":
|
|
$this->doDeleteCustomer($position);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
/** @see CmsObject::getCssClass()
|
|
*/
|
|
//-----------------------------------------------
|
|
function getCssClass(){
|
|
//-----------------------------------------------
|
|
return "itemCustomers";
|
|
}
|
|
|
|
|
|
// === ADDITIONAL METHODS ================================================================= //
|
|
|
|
|
|
/** offers an exported csv file for download
|
|
* @param data type of download data
|
|
* @param new true, if only newly added data (since last download)
|
|
* @warning: EXITS THE PHP SCRIPT!
|
|
*/
|
|
//-----------------------------------------------
|
|
function doDownload($data, $new){
|
|
//-----------------------------------------------
|
|
|
|
//download only customers with complete address data
|
|
if($data == "complete only"){
|
|
$customers = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
|
|
$customers .= "<tr>";
|
|
$customers .= "<th><b>Vorname</b></th>";
|
|
$customers .= "<th><b>Nachname</b></th>";
|
|
$customers .= "<th><b>Adresse</b></th>";
|
|
$customers .= "<th><b>PLZ</b></th>";
|
|
$customers .= "<th><b>Ort</b></th>";
|
|
$customers .= "</tr>";
|
|
if($new){
|
|
$query = sprintf("SELECT * FROM bruckm_ticketcustomer
|
|
WHERE creationDate > %s
|
|
AND surname != '' AND firstname != '' AND address != '' AND zip != NULL AND city != ''
|
|
ORDER BY surname ASC, firstname ASC",
|
|
sqlstring($this->lastExportDate));
|
|
}
|
|
else{
|
|
$query = sprintf("SELECT * FROM bruckm_ticketcustomer
|
|
WHERE surname != '' AND firstname != '' AND address != '' AND zip IS NOT NULL AND city != ''
|
|
ORDER BY surname ASC, firstname ASC");
|
|
}
|
|
$result = dbQuery($query);
|
|
while($line = mysqli_fetch_array($result, MYSQLI_ASSOC)){
|
|
$customers .= "<tr>";
|
|
$customers .= "<td>$line[firstname]</td>";
|
|
$customers .= "<td>$line[surname]</td>";
|
|
$customers .= "<td>$line[address]</td>";
|
|
$customers .= "<td>$line[zip]</td>";
|
|
$customers .= "<td>$line[city]</td>";
|
|
$customers .= "</tr>";
|
|
}
|
|
$customers .= "</table>";
|
|
}
|
|
|
|
//download complete customer list
|
|
else{
|
|
$genres = array();
|
|
$customers = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
|
|
$customers .= "<tr>";
|
|
$customers .= "<th><b>Nachname</b></th>";
|
|
$customers .= "<th><b>Vorname</b></th>";
|
|
$customers .= "<th><b>Adresse</b></th>";
|
|
$customers .= "<th><b>PLZ</b></th>";
|
|
$customers .= "<th><b>Ort</b></th>";
|
|
$customers .= "<th><b>E-Mail</b></th>";
|
|
$customers .= "<th><b>Telefon</b></th>";
|
|
//load genres
|
|
$query = sprintf("SELECT id,name FROM bruckm_index WHERE class = 'TicketGenre' ORDER BY name ASC");
|
|
$result = dbQuery($query);
|
|
while($line = mysqli_fetch_array($result, MYSQLI_ASSOC)){
|
|
$genres[] = $line['id'];
|
|
$customers .= "<th>$line[name]</th>";
|
|
}
|
|
$customers .= "</tr>";
|
|
//load customers
|
|
$query = sprintf("SELECT * FROM bruckm_ticketcustomer ORDER BY surname ASC, firstname ASC");
|
|
$result = dbQuery($query);
|
|
while($line = mysqli_fetch_array($result, MYSQLI_ASSOC)){
|
|
$customers .= "<tr>";
|
|
$customers .= "<td>$line[surname]</td>";
|
|
$customers .= "<td>$line[firstname]</td>";
|
|
$customers .= "<td>$line[address]</td>";
|
|
$customers .= "<td>$line[zip]</td>";
|
|
$customers .= "<td>$line[city]</td>";
|
|
$customers .= "<td>$line[email]</td>";
|
|
$customers .= "<td>$line[phone]</td>";
|
|
$cGenres = array_count_values(explode(",", $line['genres']));
|
|
foreach($genres as $g){
|
|
$customers .= "<td>$cGenres[$g]</td>";
|
|
}
|
|
$customers .= "</tr>";
|
|
}
|
|
$customers .= "</table>";
|
|
}
|
|
|
|
header("Content-Type: application/vnd-ms-excel");
|
|
header("Content-Disposition: attachment; filename=\"export.xls\"");
|
|
header('Pragma: no-cache');
|
|
header('Expires: 0');
|
|
echo $customers;
|
|
exit();
|
|
}
|
|
|
|
|
|
/** updates the export date
|
|
* @param data type of download data
|
|
*/
|
|
//-----------------------------------------------
|
|
function doUpdateExportDate($data){
|
|
//-----------------------------------------------
|
|
$query = sprintf("INSERT INTO bruckm_customerlist (exportDate, data) VALUES (NOW(), %s)",
|
|
sqlstring($data));
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
/** sets the id
|
|
* @param id id
|
|
*/
|
|
//-----------------------------------------------
|
|
function setId($id){
|
|
//-----------------------------------------------
|
|
$this->id = $id;
|
|
}
|
|
|
|
|
|
/** loads the list of customers, sorted & filtered
|
|
* @return db result set
|
|
*/
|
|
//-----------------------------------------------
|
|
function doLoadCustomers(){
|
|
//-----------------------------------------------
|
|
//sorting
|
|
switch($this->sortProperty){
|
|
case "name":
|
|
$order = "surname ".$this->sortOrder.", firstname ".$this->sortOrder;
|
|
break;
|
|
case "address":
|
|
$order = "zip ".$this->sortOrder.", city ".$this->sortOrder.", address ".$this->sortOrder;
|
|
break;
|
|
case "email":
|
|
$order = "email ".$this->sortOrder;
|
|
break;
|
|
case "events":
|
|
$order = "eventCount ".$this->sortOrder.", events ".$this->sortOrder;
|
|
break;
|
|
default:
|
|
$order = $this->sortProperty." ".$this->sortOrder;
|
|
}
|
|
//filter
|
|
if(!empty($this->filter)){
|
|
switch($this->filterProperty){
|
|
case "name":
|
|
$filter = "WHERE surname LIKE '%".$this->filter."%'";
|
|
break;
|
|
case "address":
|
|
if(is_numeric($this->filter)){
|
|
$filter = "WHERE zip = ".$this->filter;
|
|
}
|
|
else{
|
|
$filter = "WHERE city = '".$this->filter."' || address LIKE '%".$this->filter."%'";
|
|
}
|
|
break;
|
|
default:
|
|
$filter = "WHERE ".$this->filterProperty." LIKE '%".$this->filter."%'";
|
|
}
|
|
}
|
|
//no filter: remove empty values from sorting
|
|
else{
|
|
switch($this->sortProperty){
|
|
case "name": $filter = "";
|
|
break;
|
|
case "address": $filter = "WHERE zip != 0 || city != ''";
|
|
break;
|
|
default: $filter = "WHERE ".$this->sortProperty." != ''";
|
|
}
|
|
}
|
|
$query = sprintf("SELECT * FROM bruckm_ticketcustomer %s ORDER BY %s", $filter, $order);
|
|
return dbQuery($query);
|
|
}
|
|
|
|
|
|
/** deletes a customer from the list
|
|
* @param id customer id
|
|
*/
|
|
//--------------------------------------------
|
|
function doDeleteCustomer($id){
|
|
//--------------------------------------------
|
|
$query = sprintf("SELECT id FROM bruckm_ticket WHERE customerId = %d LIMIT 1", $id);
|
|
$result = dbQuery($query);
|
|
if(mysql_num_rows($result) > 0){
|
|
$this->addError("Der Besucher kann nicht gelöscht werden (aktuelle Reservierung)!");
|
|
return;
|
|
}
|
|
$query = sprintf("DELETE FROM bruckm_ticketcustomer WHERE id = %d", $id);
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
};
|
|
|
|
?>
|