'doForm', 'report'=>'doReport', 'download'=>'doDownload', ); function exec(&$Doc) { $this->Doc = $Doc; parent::exec(); $Doc->addContent(array('tpl' => 'statistic.tpl')); } function getForm() { $form = $this->_getBaseForm(); $this->setFormTitle($form, 'Login Counts'); $form->addElement('hidden', 'action', 'report') ; $form->tpl = 'tpl/Statistic/form.tpl'; $form->setRendType(FORM_RENDERER_ARRAY_SMARTY); $form->addElement('calendar', 'date_start', '', array('minYear' => 2008, 'maxYear' => 2012)); $form->addElement('calendar', 'date_end', '', array('minYear' => 2008, 'maxYear' => 2012)); $form->addElement('submit', 'submit', 'Generate Report', array('class' => 'btn')); $form->addElement('submit', 'download', 'Download Report', array('class' => 'btn', 'onclick'=>'this.form.action.value="download"')); return $form; } function doForm() { $form = $this->getForm(); $form->setDefaults(array( 'date_start'=>mktime(0, 0, 0, date('m'), date('d'), date('Y')-1), 'date_end'=>mktime(), )); $this->renderForm($form); } function doReport() { $form = $this->getForm(); $this->renderForm($form); $this->Doc->addContent(array( 'tpl' => 'Statistic/report.tpl', 'report'=>$this->getReportData($form->exportValues()) )); } function doDownload() { $form = $this->getForm(); $repotData = $this->getReportData($form->exportValues()); header('Content-Type: text/comma-separated-values'); header('Content-Disposition: attachment; filename=login-count.csv'); echo ("Individual ID, First Name, Last Name, Login Count\r\n"); foreach ($repotData as $aRow) { echo ('"' . $aRow['id'] . '","' . $aRow['first_name'] . '","' . $aRow['last_name'] . '","' . $aRow['login_count'] . "\"\r\n"); } die(); } function getReportData($condition) { require_once('class/DB/S_db2.php'); $db = S_db2::getInstance(); $sql = "SELECT m.id, m.first_name, m.last_name, COUNT(mh.id) login_count FROM {$db->tblMember} m INNER JOIN {$db->tblMemberHistory} mh ON m.id=mh.id_individual WHERE login_date>='" . sprintf("%04d-%02d-%02d 00:00:00", $condition['date_start']['Y'], $condition['date_start']['M'], $condition['date_start']['d']) . "' AND login_date<='" . sprintf("%04d-%02d-%02d 23:59:59", $condition['date_end']['Y'], $condition['date_end']['M'], $condition['date_end']['d']) . "' GROUP BY m.id, m.first_name, m.last_name"; return $db->queryAll($sql); } } ?>