Ejemplo n.º 1
0
/**
 * Generate description for a membership plan.
 *
 * @param $sid The sid of the membership.
 * @return The description string.
 */
function member_membership_description($sid)
{
    // Get membership data
    $data = member_membership_data(array('sid' => $sid));
    $membership = $data[0];
    // Get member contact info
    $data = member_contact_data(array('cid' => $membership['cid']));
    $contact = $data[0];
    // Construct description
    $description = 'Membership : ';
    $description .= theme_contact_name($contact['cid']);
    $description .= ' : ' . $membership['plan']['name'];
    $description .= ' : Starting ' . $membership['start'];
    return $description;
}
Ejemplo n.º 2
0
/**
 * Handle member import request.
 *
 * @return The url to display on completion.
 */
function command_member_import()
{
    global $config_org_name;
    if (!user_access('contact_edit')) {
        error_register('User does not have permission: contact_edit');
        return crm_url('members');
    }
    if (!user_access('member_edit')) {
        error_register('User does not have permission: member_edit');
        return crm_url('members');
    }
    if (!array_key_exists('member-file', $_FILES)) {
        error_register('No member file uploaded');
        return crm_url('members&tab=import');
    }
    $csv = file_get_contents($_FILES['member-file']['tmp_name']);
    $data = csv_parse($csv);
    foreach ($data as $row) {
        // Convert row keys to lowercase and remove spaces
        foreach ($row as $key => $value) {
            $new_key = str_replace(' ', '', strtolower($key));
            unset($row[$key]);
            $row[$new_key] = $value;
        }
        // Add contact
        $firstName = mysql_real_escape_string($row['firstname']);
        $middleName = mysql_real_escape_string($row['middlename']);
        $lastName = mysql_real_escape_string($row['lastname']);
        $email = mysql_real_escape_string($row['email']);
        $phone = mysql_real_escape_string($row['phone']);
        $emergencyName = mysql_real_escape_string($row['emergencyname']);
        $emergencyPhone = mysql_real_escape_string($row['emergencyphone']);
        $sql = "\n            INSERT INTO `contact`\n            (`firstName`,`middleName`,`lastName`,`email`,`phone`,`emergencyName`,`emergencyPhone`)\n            VALUES\n            ('{$firstName}','{$middleName}','{$lastName}','{$email}','{$phone}','{$emergencyName}','{$emergencyPhone}')";
        $res = mysql_query($sql);
        if (!$res) {
            crm_error(mysql_error());
        }
        $cid = mysql_insert_id();
        $esc_cid = mysql_real_escape_string($cid);
        // Add member
        $sql = "\n            INSERT INTO `member`\n            (`cid`)\n            VALUES\n            ('{$esc_cid}')";
        $res = mysql_query($sql);
        if (!$res) {
            crm_error(mysql_error());
        }
        // Find Username
        $username = $row['username'];
        $n = 0;
        while (empty($username) && $n < 100) {
            // Contruct test username
            $test_username = strtolower($row['firstname'][0] . $row['lastName']);
            if ($n > 0) {
                $test_username .= $n;
            }
            // Check whether username is taken
            $esc_test_name = mysql_real_escape_string($test_username);
            $sql = "SELECT * FROM `user` WHERE `username`='{$esc_test_name}'";
            $res = mysql_query($sql);
            if (!$res) {
                crm_error(mysql_error());
            }
            $user_row = mysql_fetch_assoc($res);
            if (!$user_row) {
                $username = $test_username;
            }
            $n++;
        }
        if (empty($username)) {
            error_register('Please specify a username');
            return crm_url('members&tab=import');
        }
        // Add user
        $user = array();
        $user['username'] = $username;
        $user['cid'] = $cid;
        user_save($user);
        // Add role entry
        $sql = "SELECT `rid` FROM `role` WHERE `name`='member'";
        $res = mysql_query($sql);
        if (!$res) {
            crm_error(mysql_error());
        }
        $role_row = mysql_fetch_assoc($res);
        $esc_rid = mysql_real_escape_string($role_row['rid']);
        if ($role_row) {
            $sql = "\n                INSERT INTO `user_role`\n                (`cid`, `rid`)\n                VALUES\n                ('{$esc_cid}', '{$esc_rid}')";
            $res = mysql_query($sql);
            if (!$res) {
                crm_error(mysql_error());
            }
        }
        // Add plan if necessary
        $esc_plan_name = mysql_real_escape_string($row['plan']);
        $sql = "SELECT `pid` FROM `plan` WHERE `name`='{$esc_plan_name}'";
        $res = mysql_query($sql);
        if (!$res) {
            crm_error(mysql_error());
        }
        if (mysql_num_rows($res) < 1) {
            $sql = "\n                INSERT INTO `plan`\n                (`name`, `active`, `price`, `voting`)\n                VALUES\n                ('{$esc_plan_name}', '1', '0', '0' )\n            ";
            $res = mysql_query($sql);
            if (!$res) {
                crm_error(mysql_error());
            }
            $pid = mysql_insert_id();
        } else {
            $plan_row = mysql_fetch_assoc($res);
            $pid = $plan_row['pid'];
        }
        // Add membership
        $esc_start = mysql_real_escape_string($row['startdate']);
        $esc_pid = mysql_real_escape_string($pid);
        $sql = "\n            INSERT INTO `membership`\n            (`cid`, `pid`, `start`)\n            VALUES\n            ('{$esc_cid}', '{$esc_pid}', '{$esc_start}')\n        ";
        $res = mysql_query($sql);
        if (!$res) {
            crm_error(mysql_error());
        }
        // Notify admins
        $from = "\"{$config_org_name}\" <{$config_email_from}>";
        $headers = "From: {$from}\r\nContent-Type: text/html; charset=ISO-8859-1\r\n";
        if (!empty($config_email_to)) {
            $name = theme_contact_name($_POST['cid']);
            $content = theme('member_created_email', $user['cid']);
            mail($config_email_to, "New Member: {$name}", $content, $headers);
        }
        // Notify user
        $confirm_url = user_reset_password_url($user['username']);
        $content = theme('member_welcome_email', $user['cid'], $confirm_url);
        mail($email, "Welcome to {$config_org_name}", $content, $headers);
    }
    return crm_url('members');
}
Ejemplo n.º 3
0
/**
 * Return the form structure for editing a membership.
 *
 * @param $sid id of the membership to edit.
 * @return The form structure.
*/
function member_membership_edit_form($sid)
{
    // Ensure user is allowed to edit memberships
    if (!user_access('member_membership_edit')) {
        return NULL;
    }
    // Get membership data
    $data = member_membership_data(array('sid' => $sid));
    $membership = $data[0];
    if (empty($membership) || count($membership) < 1) {
        return array();
    }
    // Construct contact name
    $data = member_contact_data(array('cid' => $membership['cid']));
    $contact = $data[0];
    $name = theme_contact_name($contact['cid']);
    // Create form structure
    $form = array('type' => 'form', 'method' => 'post', 'command' => 'member_membership_update', 'hidden' => array('sid' => $sid, 'cid' => $membership['cid']), 'fields' => array(array('type' => 'fieldset', 'label' => 'Edit Membership Info', 'fields' => array(array('type' => 'readonly', 'label' => 'Name', 'value' => $name), array('type' => 'select', 'label' => 'Plan', 'name' => 'pid', 'options' => member_plan_options(), 'selected' => $membership['pid']), array('type' => 'text', 'label' => 'Start', 'name' => 'start', 'class' => 'date', 'value' => $membership['start']), array('type' => 'text', 'label' => 'End', 'name' => 'end', 'class' => 'date', 'value' => $membership['end']), array('type' => 'submit', 'value' => 'Update')))));
    return $form;
}
Ejemplo n.º 4
0
/**
 * Return a table showing a contacts payment history.
 * @param $opts An associative array of options.  Possible keys are:
 *   cid - The id of the contact to display
 * @return A table object.
 */
function payment_history_table($opts)
{
    $cid = $opts['cid'];
    if (!$cid) {
        error_register('Unable to create payment history: no contact specified');
        return array();
    }
    // Show oldest to newest unless otherwise specified
    if (!isset($opts['order'])) {
        $opts['order'] = array('date' => 'asc', 'created' => 'asc');
    }
    $payments = crm_get_data('payment', $opts);
    $balance = null;
    $table = array('columns' => array(array('title' => 'Date'), array('title' => 'Description'), array('title' => 'Amount'), array('title' => 'Method'), array('title' => 'To/From'), array('title' => 'Balance Owed')), 'rows' => array());
    if (user_access('payment_edit')) {
        $table['columns'][] = array('title' => 'Ops');
    }
    foreach ($payments as $payment) {
        $contact = '';
        if ($payment['credit_cid'] === $cid) {
            $payment = payment_invert_currency($payment);
            $contact = $payment['debit'];
        } else {
            $contact = $payment['credit'];
        }
        $contactName = '';
        if (!empty($contact)) {
            $contactName = theme_contact_name($contact['cid']);
        }
        if (isset($balance)) {
            $balance = payment_add_currency($balance, $payment);
        } else {
            $balance = $payment;
        }
        $row = array();
        $row[] = $payment['date'];
        $row[] = $payment['description'];
        $row[] = payment_format_currency($payment);
        $row[] = $payment['method'];
        $row[] = $contactName;
        $row[] = payment_format_currency($balance);
        $ops = '';
        if (user_access('payment_edit')) {
            $ops .= '<a href=' . crm_url('payment&pmtid=' . $payment[pmtid]) . '>edit</a> ';
        }
        if (user_access('payment_delete')) {
            $ops .= '<a href=' . crm_url('delete&type=payment&id=' . $payment['pmtid']) . '>delete</a>';
        }
        $row[] = $ops;
        $table['rows'][] = $row;
    }
    return $table;
}
Ejemplo n.º 5
0
/**
 * Return the text of an email notifying administrators that a user has been created.
 * @param $cid The contact id of the new member.
 */
function theme_member_created_email($cid)
{
    // Get info on the logged in user
    $data = member_contact_data(array('cid' => user_id()));
    $admin = $data[0];
    $adminName = theme_contact_name($admin['cid']);
    // Get info on member
    $data = member_data(array('cid' => $cid));
    $member = $data[0];
    $contact = $member['contact'];
    $name = theme_contact_name($contact['cid']);
    // Get info on member's plan
    $data = member_membership_data(array('cid' => $cid, $filter => array('active' => true)));
    $date = $data[0]['start'];
    $plan = $data[0]['plan']['name'];
    $output = "<p>Contact info:<br/>\n";
    $output .= "Name: {$name}<br/>\n";
    $output .= "Email: {$contact['email']}<br/>\n";
    $output .= "Phone: {$contact['phone']}\n</p>\n";
    $output .= "<p>Membership info:<br/>\n";
    $output .= "Plan: {$plan}<br/>\n";
    $output .= "Start date: {$date}\n</p>\n";
    $output .= "<p>Entered by: {$adminName}</p>\n";
    return $output;
}