Projekt

Obecné

Profil

Stáhnout (18.7 KB) Statistiky
| Větev: | Tag: | Revize:
8baed187 Michal Kliment
<?php defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreenetIS
* and it is released under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/

/**
* Handles interrupt of member's membership to associations.
*
* @package Controller
*/
class Membership_interrupts_Controller extends Controller
{
c1bdc1c4 Michal Kliment
/**
* 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);
}
8baed187 Michal Kliment
/**
* Index redirects to show all
*/
public function index()
{
url::redirect('membership_interrupts/show_all');
}
/**
* Shows all membership interrupts
*
* @author Michal Kliment
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
* @param string $page_word
* @param integer $page
*/
public function show_all (
$limit_results = 50, $order_by = 'id', $order_by_direction = 'ASC',
$page_word = null, $page = 1)
{
// access rights
c1bdc1c4 Michal Kliment
if (!$this->acl_check_view('Members_Controller', 'membership_interrupts'))
8baed187 Michal Kliment
Controller::error(ACCESS);
$filter_form = new Filter_form('mi');
$filter_form->add('id')
->type('number');
a29b0d63 Michal Kliment
$filter_form->add('member_id')
->type('number');
8baed187 Michal Kliment
$filter_form->add('name')
->label(__('Member name'))
->callback('json/member_name')
->table('m');
$filter_form->add('activation_date')
->type('date')
->table('mf');
$filter_form->add('deactivation_date')
->type('date')
->table('mf');
c1bdc1c4 Michal Kliment
$filter_form->add('end_after_interrupt_end')
->type('select')
->values(arr::bool())
->label('End membership after end');
8baed187 Michal Kliment
$filter_form->add('comment');
$membership_interrupt_model = new Membership_interrupt_Model();
$total = $membership_interrupt_model->count_all_membership_interrupts(
$filter_form->as_sql()
);
if (($sql_offset = ($page - 1) * $limit_results) > $total)
$sql_offset = 0;
$interupts = $membership_interrupt_model->get_all_membership_interrupts(
$sql_offset, (int)$limit_results, $order_by,
$order_by_direction, $filter_form->as_sql()
);
$grid = new Grid('members', null, array
(
'current' => $limit_results,
'selector_increace' => 50,
'selector_min' => 50,
'selector_max_multiplier' => 20,
'base_url' => Config::get('lang').'/membership_interrupts/show_all/'
. $limit_results.'/'.$order_by.'/'.$order_by_direction ,
'uri_segment' => 'page',
'total_items' => $total,
'items_per_page' => $limit_results,
'style' => 'classic',
'order_by' => $order_by,
'order_by_direction' => $order_by_direction,
'limit_results' => $limit_results,
'filter' => $filter_form
));
$grid->order_field('id')
c1bdc1c4 Michal Kliment
->label('ID');
8baed187 Michal Kliment
$grid->order_callback_field('member_id')
c1bdc1c4 Michal Kliment
->label('Member')
2a205303 Michal Kliment
->callback('callback::member_field_with_id');
8baed187 Michal Kliment
$grid->order_field('from')
c1bdc1c4 Michal Kliment
->label('Date from');
8baed187 Michal Kliment
$grid->order_field('to')
c1bdc1c4 Michal Kliment
->label('Date to');
$grid->order_callback_field('end_after_interrupt_end')
->callback('callback::boolean')
->class('center')
->label('End membership after end');
8baed187 Michal Kliment
$grid->order_field('comment');
$actions = $grid->grouped_action_field();
if ($this->acl_check_edit('Members_Controller', 'membership_interrupts'))
{
$actions->add_action('id')
->icon_action('edit')
->url('membership_interrupts/edit');
}
if ($this->acl_check_delete('Members_Controller', 'membership_interrupts'))
{
$actions->add_action('id')
->icon_action('delete')
->url('membership_interrupts/delete')
->class('delete_link');
}
$grid->datasource($interupts);
$title = __('List of all membership interrupts');
$view = new View('main');
$view->breadcrumbs = __('Membership interrupts');
$view->title = $title;
$view->content = new View('show_all');
$view->content->headline = $title;
$view->content->table = $grid;
$view->render(TRUE);
}

/**
* Adding new membership interrupt
*
* @param integer $member_id id of member to add new membership interrupt
*/
public function add($member_id = NULL)
{
if (!isset($member_id))
Controller::warning(PARAMETER);
$member = new Member_Model($member_id);
if ($member->id == 0)
Controller::error(RECORD);
// access control
if (!$this->acl_check_new('Members_Controller', 'membership_interrupts', $member->id))
Controller::Error(ACCESS);
// saving id for callback function
$this->members_fee_id = NULL;

$this->form = new Forge('membership_interrupts/add/'.$member->id);
$this->form->group('Basic data');
74a7dbca Michal Kliment
$this->form->hidden('member_id')
->value($member->id);
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
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'));
}
8baed187 Michal Kliment
$this->form->date('from')
->label('Date from')
->years(date('Y')-10, date('Y')+10)
->rules('required');
74a7dbca Michal Kliment
$to_date = $this->form->date('to')
8baed187 Michal Kliment
->label('Date to')
->years(date('Y')-10, date('Y')+10)
->rules('required')
->callback(array($this, 'valid_interrupt_interval'));
74a7dbca Michal Kliment
if (intval(Settings::get('membership_interrupt_maximum')) == 0)
{
$to_date->infinity($this->form);
}
8baed187 Michal Kliment
$this->form->textarea('comment')
->rules('length[0,250]|required')
->style('width: 350px');
$this->form->submit('Save');
74a7dbca Michal Kliment
8baed187 Michal Kliment
// form validation
if ($this->form->validate())
{
$form_data = $this->form->as_array();
74a7dbca Michal Kliment
c1bdc1c4 Michal Kliment
try
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$membership_interrupt = new Membership_interrupt_Model();

$membership_interrupt->transaction_start();

74a7dbca Michal Kliment
$from = $this->form->from->get_string_value_with_infinite();
$to = $this->form->to->get_string_value_with_infinite();
c1bdc1c4 Michal Kliment
$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');
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
catch (Exception $e)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$membership_interrupt->transaction_rollback();
Log::add_exception($e);
status::success('Membership interrupt has not been succesfully added');
8baed187 Michal Kliment
}

c1bdc1c4 Michal Kliment
$this->redirect('members/show/'.$member_id);
8baed187 Michal Kliment
}
else
{
// end of form validation

$headline = __('Add new 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/'.$member_id,
"ID $member->id - $member->name",
$this->acl_check_view(
'Members_Controller', 'members', $member_id
)
)
->disable_translation()
->text($headline);

// view
$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);
}
}

/**
* Editing of membership interruption
*
* @param integer $membership_interrupt_id id of membership interrupt to delete
*/
public function edit($membership_interrupt_id = NULL)
{
if (!isset($membership_interrupt_id))
Controller::warning(PARAMETER);
// find object with id to edit
c1bdc1c4 Michal Kliment
$membership_interrupt = new Membership_interrupt_Model($membership_interrupt_id);
$member = new Member_Model($membership_interrupt->member_id);
8baed187 Michal Kliment
// if object with this id doesn't exist
c1bdc1c4 Michal Kliment
if ($membership_interrupt->id == 0)
8baed187 Michal Kliment
Controller::error(RECORD);
// saving id for callback function
c1bdc1c4 Michal Kliment
$this->members_fee_id = $membership_interrupt->members_fee_id;
8baed187 Michal Kliment
// access control
if (!$this->acl_check_edit(
'Members_Controller', 'membership_interrupts',
c1bdc1c4 Michal Kliment
$membership_interrupt->member_id
8baed187 Michal Kliment
))
{
Controller::Error(ACCESS);
}

// form
c1bdc1c4 Michal Kliment
$this->form = new Forge();
8baed187 Michal Kliment
$this->form->group('Basic data');
74a7dbca Michal Kliment
$this->form->hidden('member_id')
->value($member->id);
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
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);
}
8baed187 Michal Kliment
$this->form->date('from')
->label('Date from (first day in month)')
->years(date('Y')-10, date('Y')+10)
->rules('required')
c1bdc1c4 Michal Kliment
->value(strtotime($membership_interrupt->members_fee->activation_date));
74a7dbca Michal Kliment
$to_date = $this->form->date('to')
8baed187 Michal Kliment
->label('Date to (last day in month)')
->years(date('Y')-10, date('Y')+10)
->rules('required')
->callback(array($this, 'valid_interrupt_interval'))
c1bdc1c4 Michal Kliment
->value(strtotime($membership_interrupt->members_fee->deactivation_date));
74a7dbca Michal Kliment
if (intval(Settings::get('membership_interrupt_maximum')) == 0)
{
$to_date->infinity($this->form);

if ($membership_interrupt->members_fee->deactivation_date == '9999-12-31')
{
$this->form->inputs['to_infinity']->checked(TRUE);
$this->form->inputs['to']->disabled('disabled');
}
}
8baed187 Michal Kliment
$this->form->textarea('comment')
->rules('length[0,250]|required')
c1bdc1c4 Michal Kliment
->value($membership_interrupt->comment)
8baed187 Michal Kliment
->style('width:350px');
$this->form->submit('Save');
// end of form validation
if ($this->form->validate())
{
$form_data = $this->form->as_array();

c1bdc1c4 Michal Kliment
try
{
$membership_interrupt->transaction_start();
8baed187 Michal Kliment
74a7dbca Michal Kliment
$from = $this->form->from->get_string_value_with_infinite();
$to = $this->form->to->get_string_value_with_infinite();
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
$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);
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
catch (Exception $e)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$membership_interrupt->transaction_rollback();
Log::add_exception($e);
status::error('Error - Cannot update membership interrupt', $e);
8baed187 Michal Kliment
}
}
c1bdc1c4 Michal Kliment
$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);
8baed187 Michal Kliment
}

/**
* Deleting of membership interruption
*
* @param integer $membership_interrupt_id id of membership interruption to delete
*/
public function delete($membership_interrupt_id = NULL)
{
// parameter is wrong?
if (!$membership_interrupt_id || !is_numeric($membership_interrupt_id))
Controller::warning(PARAMETER);

$membership_interrupt = new Membership_interrupt_Model($membership_interrupt_id);

// membership interrupt doesn't exist
if (!$membership_interrupt->id)
Controller::error(RECORD);
c1bdc1c4 Michal Kliment
$member = new Member_Model($membership_interrupt->member_id);
8baed187 Michal Kliment
// access control
c1bdc1c4 Michal Kliment
if (!$this->acl_check_delete('Members_Controller', 'membership_interrupts', $member->id))
8baed187 Michal Kliment
Controller::Error(ACCESS);
$members_fee = new Members_fee_Model($membership_interrupt->members_fee_id);
c1bdc1c4 Michal Kliment
try
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$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);
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
$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);
8baed187 Michal Kliment
}

c1bdc1c4 Michal Kliment
$this->redirect('members/show/'.$member->id);
8baed187 Michal Kliment
}

/**
* Callback function to valid interval of membership interrupt
*
* @author Michal Kliment
* @param object $input
*/
public function valid_interrupt_interval($input = NULL)
{
// validators cannot be accessed
if (empty($input) || !is_object($input))
{
self::error(PAGE);
}
74a7dbca Michal Kliment
8baed187 Michal Kliment
$method = $this->form->from->method;
$member_id = $this->input->$method('member_id');
c1bdc1c4 Michal Kliment
$from = date_parse($this->input->$method('from'));
$to = date_parse($this->input->$method('to'));
74a7dbca Michal Kliment
8baed187 Michal Kliment
$from_date = date::round_month($from['day'], $from['month'], $from['year']);
$to_date = date::round_month($to['day'], $to['month'], $to['year']);
$diff = date::diff_month($to_date, $from_date);
74a7dbca Michal Kliment
8baed187 Michal Kliment
if ($diff < 0)
{
$input->add_error('required', __(
'Date from must be smaller then date to'
).'.');
}

c1bdc1c4 Michal Kliment
if ($diff < Settings::get('membership_interrupt_minimum'))
8baed187 Michal Kliment
{
$input->add_error('required', __(
c1bdc1c4 Michal Kliment
'Minimal duration of interrupt is %s months',
Settings::get('membership_interrupt_minimum')
8baed187 Michal Kliment
).'.');
}

c1bdc1c4 Michal Kliment
$max = intval(Settings::get('membership_interrupt_maximum'));
if ($max > 0 && $diff > $max)
{
$input->add_error('required', __(
'Maximum duration of interrupt is %s months', $max
).'.');
}
8baed187 Michal Kliment
$fee_model = new Fee_Model();
$fee = $fee_model->get_by_special_type(Fee_Model::MEMBERSHIP_INTERRUPT);

// tests collides
$members_fee_model = new Members_fee_Model();
$members_fees = $members_fee_model->exists(
$member_id, $fee->type_id,
date::create($from['day'], $from['month'], $from['year']),
date::create($to['day'], $to['month'], $to['year']),
$this->members_fee_id, 0
);

// interval of interruption collides with another interruption of this member
if (count($members_fees))
{
$input->add_error('required', __('Interval of interruption collides '.
'with another interruption of this member'
).'.');
}
}
c1bdc1c4 Michal Kliment
/**
* 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'));
}
}
8baed187 Michal Kliment
}