<commit_before><?php
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

function wikiplugin_memberpayment_info() {
	global $prefs;

	return array(
		'name' => tra('Member Payment'),
		'documentation' => 'PluginMemberPayment',
		'description' => tra('Receive payments from a member to extend membership to a group.'),
		'validate' => 'all',
		'prefs' => array( 'wikiplugin_memberpayment', 'payment_feature' ),
		'icon' => 'pics/icons/money.png',
		'params' => array(
			'group' => array(
				'required' => true,
				'name' => tra('Group'),
				'description' => tra('Name of the group for which the subscription should be added or extended.'),
				'filter' => 'groupname',
				'default' => '',
			),
			'price' => array(
				'required' => true,
				'name' => tra('Price'),
				'description' => tr('Price per period (%0).', $prefs['payment_currency']),
				'filter' => 'text',
			),
			'currentuser' => array(
				'required' => false,
				'name' => tra('Current User Member'),
				'description' => tra('Membership only for the current user'),
				'filter' => 'alpha',
				'default' => 'n',
				'options' => array(
					array('text' => '', 'value' => ''), 
					array('text' => tra('Yes'), 'value' => 'y'), 
					array('text' => tra('No'), 'value' => 'n')
				)
			),
			'inputtitle' => array(
				'required' => false,
				'name' => tra('Input Title'),
				'description' => tra('Title of the input form.').' '. tra('Use %0 for the group name.').' '.tra('Supports wiki syntax'),
				'filter' => 'text',
				'default' => 'Membership to %0 for %1 (x%2)',
			),
			'howtitle' => array(
				'required' => false,
				'name' => tra('Tiki Payments Invoice Title'),
				'description' => tra('Title of the input form.').' '. tra('Use %0 for the group name, %4 for the number of days or %5 for the number of years').' '.tra('Supports wiki syntax'),
				'filter' => 'text',
				'default' => 'Membership to %0 for %1 (x%2)',
			),
			'preventdoublerequest' => array(
				'required' => false,
				'name' => tra('Prevent double request'),
				'description' => tra('Prevent user from extended if there is already a pending request'),
				'filter' => 'alpha',
				'default' => 'n',
				'options' => array(
					array('text' => '', 'value' => ''), 
					array('text' => tra('Yes'), 'value' => 'y'), 
					array('text' => tra('No'), 'value' => 'n')
				)
			),
			'freeperiods' => array(
				'required' => false,
				'name' => tra('Give specified numbers of free periods'),
				'description' => tra('Give specified numbers of free periods, the first one could be prorated, in addition to those bought'),
				'filter' => 'int',
				'default' => 0,
			),
		),
	);
}

function wikiplugin_memberpayment( $data, $params, $offset ) {
	global $smarty, $userlib, $prefs, $user, $headerlib;
	global $paymentlib; require_once 'lib/payment/paymentlib.php';
	static $iPluginMemberpayment = 0;
	$attributelib = TikiLib::lib('attribute');

	$iPluginMemberpayment++;
	$smarty->assign('iPluginMemberpayment', $iPluginMemberpayment);
	$params['price'] = floatval( $params['price'] );
	$default = array( 'currentuser'=>'n', 'inputtitle'=>'', 'howtitle' => '');
	$params = array_merge( $default, $params );

	if ( ( $info = $userlib->get_group_info( $params['group'] ) ) && ( $info['expireAfter'] > 0 || $info['anniversary'] > '') ) {
		$smarty->assign( 'wp_member_offset', $offset );
		$smarty->assign( 'wp_member_price', $params['price'] );
		if (($info['expireAfter']/365)*365 == $info['expireAfter'] && $info['expireAfter'] >= 365) {
			$info['expireAfterYear'] = $info['expireAfter']/365;
		}
		$smarty->assign( 'wp_member_group', $info );
		$smarty->assign( 'wp_member_currentuser', $params['currentuser'] );
		$smarty->assign( 'wp_member_prorated', 0); // default
		
		if ($info['anniversary'] > '') {
			if (strlen($info['anniversary']) == 4) {
				$ann_month = substr($info['anniversary'],0,2);
				$ann_day = substr($info['anniversary'],2,2);
			} elseif (strlen($info['anniversary']) == 2) {
				$ann_month = 0;
				$ann_day = $info['anniversary'];
			}
			$smarty->assign( 'wp_member_anniversary_month', $ann_month );
			$smarty->assign( 'wp_member_anniversary_day', $ann_day );
			if ($params['currentuser'] == 'y') {
				$extend_until_info = $userlib->get_extend_until_info($user, $params['group']);
				$smarty->assign( 'wp_member_prorated', round($extend_until_info['ratio_prorated_first_period'] * $params['price'], 2) );	
			}
		}
		
		// setup free period display
		if (!empty($params['freeperiods'])) {
			if (isset($extend_until_info['ratio_prorated_first_period']) && $extend_until_info['ratio_prorated_first_period'] < 1) {
				$smarty->assign('wp_member_freeperiods', $params['freeperiods'] - 1);
				$smarty->assign('wp_member_freeprorated', 1);
			} else {
				$smarty->assign('wp_member_freeperiods', $params['freeperiods']);
			}
		} else {
			$smarty->assign('wp_member_freeperiods', 0);
		}
		
		$smarty->assign('wp_member_requestpending', 'n');
        $smarty->assign('wp_member_paymentid', 0);
		if (isset($params['currentuser']) && $params['currentuser'] == 'y' && !empty($params['preventdoublerequest']) && $params['preventdoublerequest'] == 'y') {
			$attname = 'tiki.memberextend.' . $info['id'];
			$attributes = $attributelib->get_attributes('user', $user);
			if (isset($attributes[$attname])) {
				$smarty->assign('wp_member_requestpending', 'y');
				$smarty->assign('wp_member_paymentid', $attributes[$attname]);
			}
		}
		
		if ( isset($_POST['wp_member_offset']) && $_POST['wp_member_offset'] == $offset ) {
			$users = $params['currentuser'] == 'y'? array($user): explode( '|', $_POST['wp_member_users'] );
			$users = array_map( 'trim', $users );
			$users = array_filter( $users, array( $userlib, 'user_exists' ) );
			$users = array_filter( $users );

			if (!empty($params['preventdoublerequest']) && $params['preventdoublerequest'] == 'y') {
				foreach ($users as $u) {
					$attname = 'tiki.memberextend.' . $info['id']; 
					$attributes = $attributelib->get_attributes('user', $u);
					if (isset($attributes[$attname])) {
						return '{REMARKSBOX(type=warning, title=Plugin Memberpayment Error)}' . tra('The user ') . $u 
					. tra(' already has a pending extension request payment invoice ') . $attributes[$attname] . '{REMARKSBOX}';
					}
				}
			}
			$periods = max( 1, (int) $_POST['wp_member_periods'] );

			if ( count($users) == 1 ) {
				$desc = tr('Membership to %0 for %1 (x%2)', $params['group'], reset( $users ), $periods );
			} else {
				$desc = tr('Membership to %0 for %1 users (x%2)', $params['group'], count( $users ), $periods );
			}<commit_msg>[MOD] When entered number of periods is zero, to not do anything instead of treating is as if it was 1 period.<commit_after><?php
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

function wikiplugin_memberpayment_info() {
	global $prefs;

	return array(
		'name' => tra('Member Payment'),
		'documentation' => 'PluginMemberPayment',
		'description' => tra('Receive payments from a member to extend membership to a group.'),
		'validate' => 'all',
		'prefs' => array( 'wikiplugin_memberpayment', 'payment_feature' ),
		'icon' => 'pics/icons/money.png',
		'params' => array(
			'group' => array(
				'required' => true,
				'name' => tra('Group'),
				'description' => tra('Name of the group for which the subscription should be added or extended.'),
				'filter' => 'groupname',
				'default' => '',
			),
			'price' => array(
				'required' => true,
				'name' => tra('Price'),
				'description' => tr('Price per period (%0).', $prefs['payment_currency']),
				'filter' => 'text',
			),
			'currentuser' => array(
				'required' => false,
				'name' => tra('Current User Member'),
				'description' => tra('Membership only for the current user'),
				'filter' => 'alpha',
				'default' => 'n',
				'options' => array(
					array('text' => '', 'value' => ''), 
					array('text' => tra('Yes'), 'value' => 'y'), 
					array('text' => tra('No'), 'value' => 'n')
				)
			),
			'inputtitle' => array(
				'required' => false,
				'name' => tra('Input Title'),
				'description' => tra('Title of the input form.').' '. tra('Use %0 for the group name.').' '.tra('Supports wiki syntax'),
				'filter' => 'text',
				'default' => 'Membership to %0 for %1 (x%2)',
			),
			'howtitle' => array(
				'required' => false,
				'name' => tra('Tiki Payments Invoice Title'),
				'description' => tra('Title of the input form.').' '. tra('Use %0 for the group name, %4 for the number of days or %5 for the number of years').' '.tra('Supports wiki syntax'),
				'filter' => 'text',
				'default' => 'Membership to %0 for %1 (x%2)',
			),
			'preventdoublerequest' => array(
				'required' => false,
				'name' => tra('Prevent double request'),
				'description' => tra('Prevent user from extended if there is already a pending request'),
				'filter' => 'alpha',
				'default' => 'n',
				'options' => array(
					array('text' => '', 'value' => ''), 
					array('text' => tra('Yes'), 'value' => 'y'), 
					array('text' => tra('No'), 'value' => 'n')
				)
			),
			'freeperiods' => array(
				'required' => false,
				'name' => tra('Give specified numbers of free periods'),
				'description' => tra('Give specified numbers of free periods, the first one could be prorated, in addition to those bought'),
				'filter' => 'int',
				'default' => 0,
			),
		),
	);
}

function wikiplugin_memberpayment( $data, $params, $offset ) {
	global $smarty, $userlib, $prefs, $user, $headerlib;
	global $paymentlib; require_once 'lib/payment/paymentlib.php';
	static $iPluginMemberpayment = 0;
	$attributelib = TikiLib::lib('attribute');

	$iPluginMemberpayment++;
	$smarty->assign('iPluginMemberpayment', $iPluginMemberpayment);
	$params['price'] = floatval( $params['price'] );
	$default = array( 'currentuser'=>'n', 'inputtitle'=>'', 'howtitle' => '');
	$params = array_merge( $default, $params );

	if ( ( $info = $userlib->get_group_info( $params['group'] ) ) && ( $info['expireAfter'] > 0 || $info['anniversary'] > '') ) {
		$smarty->assign( 'wp_member_offset', $offset );
		$smarty->assign( 'wp_member_price', $params['price'] );
		if (($info['expireAfter']/365)*365 == $info['expireAfter'] && $info['expireAfter'] >= 365) {
			$info['expireAfterYear'] = $info['expireAfter']/365;
		}
		$smarty->assign( 'wp_member_group', $info );
		$smarty->assign( 'wp_member_currentuser', $params['currentuser'] );
		$smarty->assign( 'wp_member_prorated', 0); // default
		
		if ($info['anniversary'] > '') {
			if (strlen($info['anniversary']) == 4) {
				$ann_month = substr($info['anniversary'],0,2);
				$ann_day = substr($info['anniversary'],2,2);
			} elseif (strlen($info['anniversary']) == 2) {
				$ann_month = 0;
				$ann_day = $info['anniversary'];
			}
			$smarty->assign( 'wp_member_anniversary_month', $ann_month );
			$smarty->assign( 'wp_member_anniversary_day', $ann_day );
			if ($params['currentuser'] == 'y') {
				$extend_until_info = $userlib->get_extend_until_info($user, $params['group']);
				$smarty->assign( 'wp_member_prorated', round($extend_until_info['ratio_prorated_first_period'] * $params['price'], 2) );	
			}
		}
		
		// setup free period display
		if (!empty($params['freeperiods'])) {
			if (isset($extend_until_info['ratio_prorated_first_period']) && $extend_until_info['ratio_prorated_first_period'] < 1) {
				$smarty->assign('wp_member_freeperiods', $params['freeperiods'] - 1);
				$smarty->assign('wp_member_freeprorated', 1);
			} else {
				$smarty->assign('wp_member_freeperiods', $params['freeperiods']);
			}
		} else {
			$smarty->assign('wp_member_freeperiods', 0);
		}
		
		$smarty->assign('wp_member_requestpending', 'n');
        $smarty->assign('wp_member_paymentid', 0);
		if (isset($params['currentuser']) && $params['currentuser'] == 'y' && !empty($params['preventdoublerequest']) && $params['preventdoublerequest'] == 'y') {
			$attname = 'tiki.memberextend.' . $info['id'];
			$attributes = $attributelib->get_attributes('user', $user);
			if (isset($attributes[$attname])) {
				$smarty->assign('wp_member_requestpending', 'y');
				$smarty->assign('wp_member_paymentid', $attributes[$attname]);
			}
		}
		
		if ( isset($_POST['wp_member_offset']) && $_POST['wp_member_offset'] == $offset && !empty($_POST['wp_member_periods'])) {
			$users = $params['currentuser'] == 'y'? array($user): explode( '|', $_POST['wp_member_users'] );
			$users = array_map( 'trim', $users );
			$users = array_filter( $users, array( $userlib, 'user_exists' ) );
			$users = array_filter( $users );

			if (!empty($params['preventdoublerequest']) && $params['preventdoublerequest'] == 'y') {
				foreach ($users as $u) {
					$attname = 'tiki.memberextend.' . $info['id']; 
					$attributes = $attributelib->get_attributes('user', $u);
					if (isset($attributes[$attname])) {
						return '{REMARKSBOX(type=warning, title=Plugin Memberpayment Error)}' . tra('The user ') . $u 
					. tra(' already has a pending extension request payment invoice ') . $attributes[$attname] . '{REMARKSBOX}';
					}
				}
			}
			$periods = (int) $_POST['wp_member_periods'];

			if ( count($users) == 1 ) {
				$desc = tr('Membership to %0 for %1 (x%2)', $params['group'], reset( $users ), $periods );
			} else {
				$desc = tr('Membership to %0 for %1 users (x%2)', $params['group'], count( $users ), $periods );
			}
{'fraction_non_alphanumeric': 0.2054814490852103, 'fraction_numerical': 0.008163860339674903, 'mean_word_length': 4.226666666666667, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7549060', 'n_tokens_mistral': 5320, 'n_tokens_neox': 4589, 'n_words': 1056}