| Server IP : 180.180.241.3 / Your IP : 216.73.216.35 Web Server : Microsoft-IIS/7.5 System : Windows NT NETWORK-NHRC 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586 User : IUSR ( 0) PHP Version : 5.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /AppServ/www/app/Controller/ |
Upload File : |
<?php
App::uses('AppController', 'Controller');
/**
* Contacts Controller
*
* @property Contact $Contact
* @property PaginatorComponent $Paginator
*/
class ContactsController extends AppController {
private function __getCommonConditions(&$conditions, &$keyword = null) {
if ( isset($this->params->query['keyword']) && !empty($this->params->query['keyword']) ) {
$keyword = $this->params->query['keyword'];
$conditions['OR'] = array(
'Contact.name LIKE' => '%' . $keyword . '%',
'Contact.address LIKE' => '%' . $keyword . '%',
'Contact.postcode LIKE' => '%' . $keyword . '%',
'Contact.phone LIKE' => '%' . $keyword . '%',
'Contact.fax LIKE' => '%' . $keyword . '%',
'Contact.email LIKE' => '%' . $keyword . '%',
'Contact.note LIKE' => '%' . $keyword . '%',
);
}
}
/**
* admin_index method
*
* @return void
*/
public function admin_index() {
$conditions = array();
$keyword = '';
$this->__getCommonConditions($conditions, $keyword);
$this->Paginator->settings['conditions'] = $conditions;
$this->Contact->recursive = 0;
$this->set('contacts', $this->Paginator->paginate());
$this->set('total', $this->Contact->find('count'));
$this->set('topicTitle', __('ผู้ประสานงาน'));
$this->set('keyword', $keyword);
}
/**
* admin_view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_view($id = null) {
if (!$this->Contact->exists($id)) {
throw new NotFoundException(__('Invalid contact'));
}
$options = array('conditions' => array('Contact.' . $this->Contact->primaryKey => $id));
$this->set('contact', $this->Contact->find('first', $options));
}
/**
* add contact information
*
* @return JSON
* @author 3Muksteers <3muksteersteam@gmail.com>
* @since 4 October 2013
*/
public function admin_add() {
$this->autoRender = false;
//We not allow to add `contact` in backend
if ( !$this->request->is('post') ) {
throw new BadRequestException(__('Invalid request'));
}
$this->Contact->create();
//If we successful to add contact will retrun json object.
//Then I will use data in oblect to add to select list.
echo json_encode( $this->Contact->save($this->request->data) );
}
/**
* admin_edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_edit($id = null) {
if (!$this->Contact->exists($id)) {
throw new NotFoundException(__('Invalid contact'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Contact->save($this->request->data)) {
$this->Session->setFlash(__('บันทึกข้อมูลผู้ประสานงานสำเร็จ'), 'flash-success');
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('ไม่สามารถบันทึกข้อมูลผู้ประสานงานได้ กรุณาลองใหม่อีกครั้ง'), 'flash-fail');
}
} else {
$options = array('conditions' => array('Contact.' . $this->Contact->primaryKey => $id));
$this->request->data = $this->Contact->find('first', $options);
}
$this->loadModel('Province');
$provinces = $this->Province->find('list');
$province = $this->Contact->District->find(
'first',
array(
'conditions' => array(
'id' => $this->request->data['Contact']['district_id']
),
'fields' => array('province_id'),
'recursive' => -1
));
$this->set('province_id', $province['District']['province_id']);
$districts = $this->Contact->District->find('list', array(
'conditions' => array(
'province_id' => $province['District']['province_id']
)
));
$organizations = $this->Contact->Organization->find('list');
$this->set(compact('districts', 'organizations', 'provinces'));
$this->set('topicTitle', __('แก้ไขข้อมูล: ') . $this->request->data['Contact']['name']);
}
/**
* admin_delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_delete($id = null) {
$this->Contact->id = $id;
if (!$this->Contact->exists()) {
throw new NotFoundException(__('Invalid contact'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Contact->delete()) {
$this->Session->setFlash(__('The contact has been deleted.'));
} else {
$this->Session->setFlash(__('The contact could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function admin_export() {
Configure::write('debug',0); // just in case
$this->autoRender = false; // thanks, but no.
$modelName = $this->modelClass;
$conditions = array();
$this->__getCommonConditions($conditions);
// Find fields needed without recursing through associated models
$data = $this->{$modelName}->find('all',
array(
'conditions' => $conditions,
'order' => $modelName . '.id ASC',
'recursive' => -1
));
// Define column headers for CSV file, in same array format as the data itself
$headers = array(
$modelName => array(
'id' => __('รหัส'),
'name' => __('ชื่อ'),
'address' => __('ที่อยู่'),
'district' => __('อำเภอ/เขต'),
'postcode' => __('รหัสไปรษณีย์'),
'phone' => __('หมายเลขโทรศัพท์'),
'fax' => __('หมายเลขโทรสาร'),
'email' => __('อีเมล์'),
'note' => __('หมายเหตุ'),
'organization' => __('เครือข่ายองค์กร'),
'created' => __('วันที่เพิ่มข้อมูล'),
'modified' => __('แก้ไขล่าสุด')
)
);
// $roles = array();
foreach ( $data as &$d ) {
$d = array(
$modelName => array(
'id' => $d['Contact']['id'],
'name' => $d['Contact']['name'],
'address' => $d['Contact']['address'],
'district' => $d['District']['name'],
'postcode' => $d['Contact']['postcode'],
'phone' => $d['Contact']['phone'],
'fax' => $d['Contact']['fax'],
'email' => $d['Contact']['email'],
'note' => $d['Contact']['note'],
'organization' => $d['Organization']['name'],
'created' => $d['Contact']['created'],
'modified' => $d['Contact']['modified']
)
);
}
$this->set('model', $modelName);
//print_r($data);
//die();
// Add headers to start of data array
if ( $this->request->params['ext'] == 'csv' ) {
// Add headers to start of data array
array_unshift($data, $headers);
// Make the data available to the view (and the resulting CSV file)
$this->set(compact('data'));
$this->render('/Elements/csv/admin_export', false);
} else {
$this->set('topicTitle', __('ผู้ประสานงาน'));
$this->_renderPdf($data, 'admin_export');
}
}
}