Skip to content

Instantly share code, notes, and snippets.

@ScullWM
Created January 20, 2014 20:54
Show Gist options
  • Save ScullWM/8528942 to your computer and use it in GitHub Desktop.
Save ScullWM/8528942 to your computer and use it in GitHub Desktop.
<?php
class crm_info extends CoreController {
public $ItemPerPage = '10';
public $ItemPerGroup = '10';
/**
* @param int $_GET['page'] Displayed page
* @global type $app
* @return render Listing template
*/
public function index()
{
global $app;
// load pagination model and set a default $_GET['page']
$this->loadmodel('Pagination');
if(empty($_GET['page']) || !isset($_GET['page']) || $_GET['page']==0 || is_numeric($_GET['page'])===false) $_GET['page'] = 1;
$pagination_param = $this->Pagination_model->getPagination((int)$_GET['page'], $this->ItemPerPage);
// Launch query
$data['rows'] = Crm_info_ar::find('all', array('conditions'=>array(), 'order'=>'', 'limit'=> $pagination_param['limit'], 'offset' => $pagination_param['offset']));
$data['NbreTotal'] = Crm_info_ar::count();
// Generate array for pagination list
$data['pagination'] = $this->Pagination_model->getListPagination((int)$_GET['page'], $this->ItemPerPage, $data['NbreTotal'], $this->ItemPerGroup, false);
$app->template->render('index', $data);
}
/**
* @global type $app
* @return render template with add/modification form
*/
public function form()
{
global $app;
$data = array();
if(isset($_GET['iid']) && !empty($_GET['iid'])):
$data['r'] = Crm_info_ar::find((int)$_GET['iid']);
endif;
$data['actions'] = Crm_action_ar::find('all', array('conditions'=>array('etablissement_id = ?', $_GET['id']), 'order'=>''));
echo $app->template->renderPartial('form', $data);
}
/**
* Save element with a try catch
* If error, catch it to the next notification page
* @return header Location to listing index
*/
public function save()
{
global $app;
if(isset($_POST['idform']) && !empty($_POST['idform'])):
$crm_info = Crm_info_ar::find((int)$_POST['idform']);
else:
$crm_info = new Crm_info_ar();
endif;
$crm_info->etablissement_id = $_GET['id'];
$crm_info->info = $_POST['infoform'];
$crm_info->value = $_POST['valueform'];
// Keep for debug mode
//print_r($_POST);exit();
try {
$crm_info->save();
} catch (ActiveRecord\DatabaseException $e) {
//echo $e->getMessage();
$app->notify->setnotify($e->getMessage(), 'error', $crm_info.'error');
}
$histo = new crm_histo_ar();
$histo->etablissement_id = $crm_info->id;
$histo->user_id = $app->user->user_infos->id;
$histo->what = $_GET['c'].'::'.$_GET['a'];
$histo->save();
header('location:index.php?c=etablissements&a=infos&id='.$_GET['id']);
}
/**
* Delete record
*/
public function delete()
{
if(isset($_GET['iid']) && !empty($_GET['iid'])):
$crm_info = Crm_info_ar::find((int)$_GET['iid']);
$crm_info->delete();
endif;
$histo = new crm_histo_ar();
$histo->etablissement_id = $_GET['iid'];
$histo->user_id = $app->user->user_infos->id;
$histo->what = $_GET['c'].'::'.$_GET['a'];
$histo->save();
header('location:index.php?c=etablissements&a=infos&id='.$_GET['id']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment