Revize c1bdc1c4
Přidáno uživatelem Michal Kliment před více než 9 roky(ů)
application/controllers/membership_interrupts.php | ||
---|---|---|
*/
|
||
class Membership_interrupts_Controller extends Controller
|
||
{
|
||
/**
|
||
* Only checks whether membership interrupts are enabled
|
||
*
|
||
* @author Jan Dubina
|
||
*/
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
|
||
// membership interrupts are not enabled
|
||
if (!Settings::get('membership_interrupt_enabled'))
|
||
Controller::error (ACCESS);
|
||
}
|
||
|
||
/**
|
||
* Index redirects to show all
|
||
*/
|
||
... | ... | |
$page_word = null, $page = 1)
|
||
{
|
||
// access rights
|
||
if (!$this->acl_check_view('Members_Controller', 'members'))
|
||
if (!$this->acl_check_view('Members_Controller', 'membership_interrupts'))
|
||
Controller::error(ACCESS);
|
||
|
||
$filter_form = new Filter_form('mi');
|
||
... | ... | |
->type('date')
|
||
->table('mf');
|
||
|
||
$filter_form->add('end_after_interrupt_end')
|
||
->type('select')
|
||
->values(arr::bool())
|
||
->label('End membership after end');
|
||
|
||
$filter_form->add('comment');
|
||
|
||
$membership_interrupt_model = new Membership_interrupt_Model();
|
||
... | ... | |
));
|
||
|
||
$grid->order_field('id')
|
||
->label(__('ID'));
|
||
->label('ID');
|
||
|
||
$grid->order_callback_field('member_id')
|
||
->label(__('Member'))
|
||
->label('Member')
|
||
->callback('callback::member_field');
|
||
|
||
$grid->order_field('from')
|
||
->label(__('Date from'));
|
||
->label('Date from');
|
||
|
||
$grid->order_field('to')
|
||
->label(__('Date to'));
|
||
->label('Date to');
|
||
|
||
$grid->order_callback_field('end_after_interrupt_end')
|
||
->callback('callback::boolean')
|
||
->class('center')
|
||
->label('End membership after end');
|
||
|
||
$grid->order_field('comment');
|
||
|
||
... | ... | |
// saving id for callback function
|
||
$this->members_fee_id = NULL;
|
||
|
||
$arr_members[$member->id] = $member->name;
|
||
|
||
$this->form = new Forge('membership_interrupts/add/'.$member->id);
|
||
|
||
$this->form->group('Basic data');
|
||
|
||
$this->form->dropdown('member_id')
|
||
->label('Member')
|
||
->options($arr_members)
|
||
->rules('required')
|
||
->style('width: 350px');
|
||
if ($member->type != Member_Model::TYPE_FORMER)
|
||
{
|
||
$this->form->checkbox('end_after_interrupt_end')
|
||
->label('End membership after end of interrupt')
|
||
->callback(array($this, 'valid_end_after_interrupt_end'));
|
||
}
|
||
|
||
$this->form->date('from')
|
||
->label('Date from')
|
||
... | ... | |
if ($this->form->validate())
|
||
{
|
||
$form_data = $this->form->as_array();
|
||
$saved = true;
|
||
|
||
$from = date('Y-m-d', $form_data['from']);
|
||
$to = date('Y-m-d', $form_data['to']);
|
||
|
||
$fee_model = new Fee_Model();
|
||
$fee = $fee_model->get_by_special_type(Fee_Model::MEMBERSHIP_INTERRUPT);
|
||
|
||
$members_fee = new Members_fee_Model();
|
||
$members_fee->member_id = $form_data['member_id'];
|
||
$members_fee->fee_id = $fee->id;
|
||
$members_fee->activation_date = $from;
|
||
$members_fee->deactivation_date = $to;
|
||
$members_fee->priority = 0;
|
||
|
||
if (!$members_fee->save())
|
||
$saved = false;
|
||
|
||
$mi = new Membership_interrupt_Model();
|
||
$mi->member_id = $form_data['member_id'];
|
||
$mi->members_fee_id = $members_fee->id;
|
||
$mi->comment = $form_data['comment'];
|
||
|
||
if ($mi->save())
|
||
try
|
||
{
|
||
ORM::factory('member')->reactivate_messages($mi->member_id);
|
||
$membership_interrupt = new Membership_interrupt_Model();
|
||
|
||
$membership_interrupt->transaction_start();
|
||
|
||
$from = date('Y-m-d', $form_data['from']);
|
||
$to = date('Y-m-d', $form_data['to']);
|
||
|
||
$fee_model = new Fee_Model();
|
||
$fee = $fee_model->get_by_special_type(Fee_Model::MEMBERSHIP_INTERRUPT);
|
||
|
||
$members_fee = new Members_fee_Model();
|
||
$members_fee->member_id = $member->id;
|
||
$members_fee->fee_id = $fee->id;
|
||
$members_fee->activation_date = $from;
|
||
$members_fee->deactivation_date = $to;
|
||
$members_fee->priority = 0;
|
||
|
||
$members_fee->save_throwable();
|
||
|
||
$membership_interrupt->member_id = $member->id;
|
||
$membership_interrupt->members_fee_id = $members_fee->id;
|
||
$membership_interrupt->comment = $form_data['comment'];
|
||
|
||
if (isset($form_data['end_after_interrupt_end']) &&
|
||
$form_data['end_after_interrupt_end'] == '1' &&
|
||
$member->type != Member_Model::TYPE_FORMER)
|
||
{
|
||
$membership_interrupt->end_after_interrupt_end = 1;
|
||
|
||
// leaving date is validated by validator
|
||
$member->leaving_date = $to;
|
||
|
||
$member->save_throwable();
|
||
}
|
||
else
|
||
{
|
||
$membership_interrupt->end_after_interrupt_end = 0;
|
||
}
|
||
|
||
$membership_interrupt->save_throwable();
|
||
|
||
if (Settings::get('finance_enabled'))
|
||
{
|
||
Accounts_Controller::recalculate_member_fees(
|
||
$member->get_credit_account()->id
|
||
);
|
||
}
|
||
|
||
ORM::factory('member')->reactivate_messages($member->id);
|
||
|
||
// begin of redirection today? => notify member
|
||
if (module::e('notification') &&
|
||
date('Y-m-d') == $members_fee->activation_date)
|
||
{
|
||
// get message
|
||
$message = ORM::factory('message')->get_message_by_type(
|
||
Message_Model::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE
|
||
);
|
||
// create notification object
|
||
$member_notif = array
|
||
(
|
||
'member_id' => $members_fee->member->id,
|
||
'whitelisted' => $members_fee->member->has_whitelist()
|
||
);
|
||
// notify by email
|
||
Notifications_Controller::notify(
|
||
$message, array($member_notif), $this->user_id,
|
||
NULL, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE
|
||
);
|
||
}
|
||
|
||
$membership_interrupt->transaction_commit();
|
||
|
||
status::success('Membership interrupt has been succesfully added');
|
||
}
|
||
else
|
||
catch (Exception $e)
|
||
{
|
||
$saved = false;
|
||
$membership_interrupt->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::success('Membership interrupt has not been succesfully added');
|
||
}
|
||
|
||
if ($saved)
|
||
{
|
||
status::success('Membership interruption has been succesfully added');
|
||
}
|
||
else
|
||
{
|
||
status::success('Membership interruption has not been succesfully added');
|
||
}
|
||
|
||
$this->redirect('members/show/'.$form_data['member_id']);
|
||
$this->redirect('members/show/'.$member_id);
|
||
|
||
}
|
||
else
|
||
... | ... | |
Controller::warning(PARAMETER);
|
||
|
||
// find object with id to edit
|
||
$mi = new Membership_interrupt_Model($membership_interrupt_id);
|
||
$membership_interrupt = new Membership_interrupt_Model($membership_interrupt_id);
|
||
|
||
$member = new Member_Model($membership_interrupt->member_id);
|
||
|
||
// if object with this id doesn't exist
|
||
if ($mi->id == 0)
|
||
if ($membership_interrupt->id == 0)
|
||
Controller::error(RECORD);
|
||
|
||
// saving id for callback function
|
||
$this->members_fee_id = $mi->members_fee_id;
|
||
$this->members_fee_id = $membership_interrupt->members_fee_id;
|
||
// access control
|
||
if (!$this->acl_check_edit(
|
||
'Members_Controller', 'membership_interrupts',
|
||
$mi->member_id
|
||
$membership_interrupt->member_id
|
||
))
|
||
{
|
||
Controller::Error(ACCESS);
|
||
}
|
||
|
||
$arr_members[$mi->member->id] = $mi->member->name;
|
||
|
||
// form
|
||
$this->form = new Forge('membership_interrupts/edit/'.$mi->id);
|
||
$this->form = new Forge();
|
||
|
||
$this->form->group('Basic data');
|
||
|
||
$this->form->dropdown('member_id')
|
||
->label('Member')
|
||
->options($arr_members)
|
||
->rules('required')
|
||
->selected($mi->member_id)
|
||
->style('width:350px');
|
||
if ($member->type != Member_Model::TYPE_FORMER)
|
||
{
|
||
$this->form->checkbox('end_after_interrupt_end')
|
||
->label('End membership after end of interrupt')
|
||
->callback(array($this, 'valid_end_after_interrupt_end'))
|
||
->checked($membership_interrupt->end_after_interrupt_end);
|
||
}
|
||
|
||
$this->form->date('from')
|
||
->label('Date from (first day in month)')
|
||
->years(date('Y')-10, date('Y')+10)
|
||
->rules('required')
|
||
->value(strtotime($mi->members_fee->activation_date));
|
||
->value(strtotime($membership_interrupt->members_fee->activation_date));
|
||
|
||
$this->form->date('to')
|
||
->label('Date to (last day in month)')
|
||
->years(date('Y')-10, date('Y')+10)
|
||
->rules('required')
|
||
->callback(array($this, 'valid_interrupt_interval'))
|
||
->value(strtotime($mi->members_fee->deactivation_date));
|
||
->value(strtotime($membership_interrupt->members_fee->deactivation_date));
|
||
|
||
$this->form->textarea('comment')
|
||
->rules('length[0,250]|required')
|
||
->value($mi->comment)
|
||
->value($membership_interrupt->comment)
|
||
->style('width:350px');
|
||
|
||
$this->form->submit('Save');
|
||
... | ... | |
if ($this->form->validate())
|
||
{
|
||
$form_data = $this->form->as_array();
|
||
$saved = true;
|
||
|
||
$from = date('Y-m-d', $form_data['from']);
|
||
$to = date('Y-m-d', $form_data['to']);
|
||
|
||
$mi->member_id = $form_data['member_id'];
|
||
$mi->comment = $form_data['comment'];
|
||
|
||
if (!$mi->save())
|
||
$saved = false;
|
||
try
|
||
{
|
||
$membership_interrupt->transaction_start();
|
||
|
||
$members_fee = new Members_fee_Model($mi->members_fee_id);
|
||
|
||
$members_fee->member_id = $form_data['member_id'];
|
||
$members_fee->activation_date = $from;
|
||
$members_fee->deactivation_date = $to;
|
||
$from = date('Y-m-d', $form_data['from']);
|
||
$to = date('Y-m-d', $form_data['to']);
|
||
|
||
if ($members_fee->save())
|
||
{
|
||
ORM::factory('member')->reactivate_messages($mi->member_id);
|
||
}
|
||
else
|
||
{
|
||
$saved = false;
|
||
$membership_interrupt->comment = $form_data['comment'];
|
||
|
||
if (isset($form_data['end_after_interrupt_end']) &&
|
||
$form_data['end_after_interrupt_end'] == '1')
|
||
{
|
||
$membership_interrupt->end_after_interrupt_end = 1;
|
||
|
||
if ($member->leaving_date > date('Y-m-d') ||
|
||
!$member->leaving_date ||
|
||
$member->leaving_date == '0000-00-00')
|
||
{
|
||
// leaving date is validated by validator
|
||
$member->leaving_date = $to;
|
||
|
||
$member->save_throwable();
|
||
}
|
||
}
|
||
else if (isset($form_data['end_after_interrupt_end']))
|
||
{
|
||
$membership_interrupt->end_after_interrupt_end = 0;
|
||
|
||
if ($member->leaving_date > date('Y-m-d'))
|
||
{
|
||
$member->leaving_date = NULL;
|
||
|
||
$member->save_throwable();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
$membership_interrupt->end_after_interrupt_end = 0;
|
||
}
|
||
|
||
$membership_interrupt->save_throwable();
|
||
|
||
$members_fee = new Members_fee_Model($membership_interrupt->members_fee_id);
|
||
|
||
$members_fee->member_id = $membership_interrupt->member_id;
|
||
$members_fee->activation_date = $from;
|
||
$members_fee->deactivation_date = $to;
|
||
|
||
$members_fee->save_throwable();
|
||
|
||
if (Settings::get('finance_enabled'))
|
||
{
|
||
Accounts_Controller::recalculate_member_fees(
|
||
$membership_interrupt->member->get_credit_account()->id
|
||
);
|
||
}
|
||
|
||
ORM::factory('member')->reactivate_messages(
|
||
$membership_interrupt->member_id
|
||
);
|
||
|
||
$membership_interrupt->transaction_commit();
|
||
|
||
status::success('Membership interrupt has been succesfully updated');
|
||
$this->redirect('members/show/'.$membership_interrupt->member_id);
|
||
}
|
||
|
||
// success
|
||
if ($saved)
|
||
catch (Exception $e)
|
||
{
|
||
status::success('Membership interruption has been succesfully updated');
|
||
$membership_interrupt->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::error('Error - Cannot update membership interrupt', $e);
|
||
}
|
||
|
||
$this->redirect('members/show/'.$form_data['member_id']);
|
||
}
|
||
else
|
||
{
|
||
$headline = __('Edit interrupt of membership');
|
||
|
||
// breadcrumbs navigation
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('members/show_all', 'Members',
|
||
$this->acl_check_view('Members_Controller', 'members'))
|
||
->disable_translation()
|
||
->link('members/show/'.$mi->member->id,
|
||
'ID ' . $mi->member->id . ' - ' .
|
||
$mi->member->name,
|
||
$this->acl_check_view(
|
||
'Members_Controller', 'members',
|
||
$mi->member->id
|
||
)
|
||
)
|
||
->enable_translation()
|
||
->link('membership_interrupts/show_all', 'Membership interrupts',
|
||
$this->acl_check_view('Members_Controller', 'members'))
|
||
->disable_translation()
|
||
->text($headline);
|
||
|
||
// end of validation
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $breadcrumbs->html();
|
||
$view->content = new View('form');
|
||
$view->content->form = $this->form->html();
|
||
$view->content->headline = $headline;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
$headline = __('Edit interrupt of membership');
|
||
|
||
// breadcrumbs navigation
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('members/show_all', 'Members',
|
||
$this->acl_check_view('Members_Controller', 'members'))
|
||
->disable_translation()
|
||
->link('members/show/'.$membership_interrupt->member->id,
|
||
'ID ' . $membership_interrupt->member->id . ' - ' .
|
||
$membership_interrupt->member->name,
|
||
$this->acl_check_view(
|
||
'Members_Controller', 'membership_interrupts',
|
||
$membership_interrupt->member->id
|
||
)
|
||
)
|
||
->enable_translation()
|
||
->link('membership_interrupts/show_all', 'Membership interrupts',
|
||
$this->acl_check_view('Members_Controller', 'membership_interrupts'))
|
||
->disable_translation()
|
||
->text($headline);
|
||
|
||
// end of validation
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $breadcrumbs->html();
|
||
$view->content = new View('form');
|
||
$view->content->form = $this->form->html();
|
||
$view->content->headline = $headline;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
/**
|
||
... | ... | |
if (!$membership_interrupt->id)
|
||
Controller::error(RECORD);
|
||
|
||
$member_id = $membership_interrupt->member_id;
|
||
$member = new Member_Model($membership_interrupt->member_id);
|
||
|
||
// access control
|
||
if (!$this->acl_check_delete('Members_Controller', 'membership_interrupts', $member_id))
|
||
if (!$this->acl_check_delete('Members_Controller', 'membership_interrupts', $member->id))
|
||
Controller::Error(ACCESS);
|
||
|
||
$members_fee = new Members_fee_Model($membership_interrupt->members_fee_id);
|
||
|
||
// success
|
||
if ($membership_interrupt->delete() && $members_fee->delete())
|
||
|
||
try
|
||
{
|
||
ORM::factory ('member')
|
||
->reactivate_messages($member_id);
|
||
$membership_interrupt->transaction_start();
|
||
|
||
if ($membership_interrupt->end_after_interrupt_end &&
|
||
$member->leaving_date > date('Y-m-d'))
|
||
{
|
||
$member->leaving_date = NULL;
|
||
|
||
$member->save_throwable();
|
||
}
|
||
|
||
$membership_interrupt->delete_throwable();
|
||
$members_fee->delete_throwable();
|
||
|
||
Accounts_Controller::recalculate_member_fees(
|
||
$member->get_credit_account()->id
|
||
);
|
||
|
||
ORM::factory('member')->reactivate_messages($member->id);
|
||
|
||
status::success('Membership interruption has been succesfully deleted');
|
||
$membership_interrupt->transaction_commit();
|
||
|
||
status::success('Membership interrupt has been succesfully deleted');
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$membership_interrupt->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::error('Error - Cannot delete membership interrupt', $e);
|
||
}
|
||
|
||
$this->redirect('members/show/'.$member_id);
|
||
$this->redirect('members/show/'.$member->id);
|
||
}
|
||
|
||
/**
|
||
... | ... | |
|
||
$method = $this->form->from->method;
|
||
$member_id = $this->input->$method('member_id');
|
||
$from = $this->input->$method('from');
|
||
$to = $this->input->$method('to');
|
||
$from = date_parse($this->input->$method('from'));
|
||
$to = date_parse($this->input->$method('to'));
|
||
|
||
$from_date = date::round_month($from['day'], $from['month'], $from['year']);
|
||
$to_date = date::round_month($to['day'], $to['month'], $to['year']);
|
||
... | ... | |
).'.');
|
||
}
|
||
|
||
if ($diff < 1)
|
||
if ($diff < Settings::get('membership_interrupt_minimum'))
|
||
{
|
||
$input->add_error('required', __(
|
||
'Minimal duration of interrupt is one month'
|
||
'Minimal duration of interrupt is %s months',
|
||
Settings::get('membership_interrupt_minimum')
|
||
).'.');
|
||
}
|
||
|
||
$max = intval(Settings::get('membership_interrupt_maximum'));
|
||
if ($max > 0 && $diff > $max)
|
||
{
|
||
$input->add_error('required', __(
|
||
'Maximum duration of interrupt is %s months', $max
|
||
).'.');
|
||
}
|
||
|
||
$fee_model = new Fee_Model();
|
||
|
||
$fee = $fee_model->get_by_special_type(Fee_Model::MEMBERSHIP_INTERRUPT);
|
||
... | ... | |
).'.');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Callback function to valid date to field when checkbox is checked
|
||
*
|
||
* @author David Raška
|
||
* @param object $input
|
||
*/
|
||
public function valid_end_after_interrupt_end($input = NULL)
|
||
{
|
||
// validators cannot be accessed
|
||
if (empty($input) || !is_object($input))
|
||
{
|
||
self::error(PAGE);
|
||
}
|
||
|
||
$method = $this->form->from->method;
|
||
$to_array = date_parse($this->input->$method('to'));
|
||
$checked = $this->input->$method('end_after_interrupt_end');
|
||
|
||
$to = mktime(0, 0, 0, $to_array['month'], $to_array['day'], $to_array['year']);
|
||
|
||
if ($checked == '1' &&
|
||
$to <= mktime(23, 59, 59))
|
||
{
|
||
$this->form->to->add_error('required', __('Date to must be in future if End membership is selected'));
|
||
}
|
||
}
|
||
}
|
Také k dispozici: Unified diff
Release 1.1.0