Example #1
0
/**
 * Get a single data structure from the database, and allow all modules
 * to extend it.  This function will call hook_data() to get the data and
 * hook_data_alter() to allow modules to alter the data.
 * @param $type The type of data.
 * @param $opts An associative array of options.
 * @return An array of data structures.
 */
function crm_get_one($type, $opts = array())
{
    $opts['limit'] = 1;
    $data = crm_get_data($type, $opts);
    if (count($data) > 0) {
        return $data[0];
    }
    return array();
}
Example #2
0
/**
 * Delete membership data for a contact.
 * @param $cid - The contact id.
 */
function member_delete($cid)
{
    // Store name
    $contact_data = crm_get_data('contact', array('cid' => $cid));
    $contact = $contact_data[0];
    $name = theme('contact_name', $contact);
    // Delete member
    $esc_cid = mysql_real_escape_string($cid);
    $sql = "DELETE FROM `member` WHERE `cid`='{$esc_cid}'";
    $res = mysql_query($sql);
    if (!$res) {
        crm_error(mysql_error());
    }
    $sql = "DELETE FROM `membership` WHERE `cid`='{$esc_cid}'";
    $res = mysql_query($sql);
    if (!$res) {
        crm_error(mysql_error());
    }
    message_register("Deleted membership info for: {$name}");
}
function secrets_table($opts)
{
    // Determine settings
    $export = false;
    $show_ops = isset($opts['ops']) ? $opts['ops'] : true;
    foreach ($opts as $option => $value) {
        switch ($option) {
            case 'export':
                $export = $value;
                break;
        }
    }
    // Get secrets data
    $secrets = crm_get_data('secrets');
    if (count($secrets) < 1) {
        return array();
    }
    // Initialize table
    $table = array('columns' => array(array('title' => 'Name'), array('title' => 'Value')), 'rows' => array());
    // Add ops column
    if (user_access('secrets_edit') || user_access('secrets_delete')) {
        $table['columns'][] = array('title' => 'Ops', 'class' => '');
    }
    // Add rows
    foreach ($secrets as $key) {
        // Add secrets data
        $row = array();
        if (user_access('secrets_view')) {
            // Add cells
            $row[] = $key['name'];
            $row[] = $key['value'];
        }
        // Construct ops array
        $ops = array();
        // Add edit op
        if (user_access('secrets_edit')) {
            $ops[] = '<a href=' . crm_url('secrets_edit&name=' . $key['name']) . '>edit</a> ';
            // $ops[] = '<a href=' . crm_url('secrets_edit&name=' . $key['name'] . '#tab-edit') . '>edit</a> ';
        }
        // Add delete op
        if (user_access('secrets_delete')) {
            $ops[] = '<a href=' . crm_url('delete&type=secrets&id=' . $key['name']) . '>delete</a>';
        }
        // Add ops row
        $row[] = join(' ', $ops);
        $table['rows'][] = $row;
    }
    return $table;
}
Example #4
0
/**
 * Run billings
 */
function command_billing()
{
    // Get current date and last bill date
    $today = date('Y-m-d');
    $last_billed = variable_get('billing_last_date', '');
    // Find memberships that start before today and end after the last bill date
    $filter = array();
    if (!empty($last_billed)) {
        $filter['ends_after'] = $last_billed;
    }
    $membership_data = crm_get_data('member_membership', array('filter' => $filter));
    // Bill each membership
    foreach ($membership_data as $membership) {
        if (!empty($membership['end']) && strtotime($membership['end']) < strtotime($today)) {
            // Bill until end of membership
            _billing_bill_membership($membership, $membership['end'], $last_billed);
        } else {
            // Bill until today
            _billing_bill_membership($membership, $today, $last_billed);
        }
    }
    // Set last billed date to today
    variable_set('billing_last_date', $today);
    $begin = empty($last_billed) ? 'the beginning of time' : $last_billed;
    message_register("Billings processed from {$begin} through {$today}.");
    return crm_url('payments');
}
Example #5
0
/**
 * Return the delete key assigment form structure.
 *
 * @param $kid The kid of the key assignment to delete.
 * @return The form structure.
*/
function key_delete_form($kid)
{
    // Ensure user is allowed to delete keys
    if (!user_access('key_delete')) {
        return NULL;
    }
    // Get key data
    $data = crm_get_data('key', array('kid' => $kid));
    $key = $data[0];
    // Construct key name
    $key_name = "key:{$key['kid']} serial:{$key['serial']} slot:{$key['slot']} {$key['start']} -- {$key['end']}";
    // Create form structure
    $form = array('type' => 'form', 'method' => 'post', 'command' => 'key_delete', 'hidden' => array('kid' => $key['kid']), 'fields' => array(array('type' => 'fieldset', 'label' => 'Delete Key', 'fields' => array(array('type' => 'message', 'value' => '<p>Are you sure you want to delete the key assignment "' . $key_name . '"? This cannot be undone.'), array('type' => 'submit', 'value' => 'Delete')))));
    return $form;
}
Example #6
0
function mentor_data_alter($type, $data = array(), $opts = array())
{
    switch ($type) {
        case 'member':
            //Get cids of all members passed into $data
            $cids = array();
            foreach ($data as $member) {
                $cids[] = $member['cid'];
            }
            // Add the cids to the options
            $mentor_opts = $opts;
            $mentor_opts['cid'] = $cids;
            // Get an array of member structures for each cid
            $mentor_data = crm_get_data('mentor', $mentor_opts);
            // Add mentorship data to member array
            foreach ($data as $i => $member) {
                $data[$i]['mentorships'] = $mentor_data[$member['cid']];
            }
            break;
    }
    return $data;
}
Example #7
0
/**
 * Implementation of hook_data_alter().
 * @param $type The type of the data being altered.
 * @param $data An array of structures of the given $type.
 * @param $opts An associative array of options.
 * @return An array of modified structures.
 */
function user_data_alter($type, $data = array(), $opts = array())
{
    switch ($type) {
        case 'contact':
            // Get cids of all contacts passed into $data
            $cids = array();
            foreach ($data as $contact) {
                $cids[] = $contact['cid'];
            }
            // Add the cids to the options
            $user_opts = $opts;
            $user_opts['cid'] = $cids;
            // Get an array of user structures for each cid
            $user_data = crm_get_data('user', $user_opts);
            // Create a map from cid to user structure
            $cid_to_user = array();
            foreach ($user_data as $user) {
                $cid_to_user[$user['cid']] = $user;
            }
            // Add user structures to the contact structures
            foreach ($data as $i => $contact) {
                $user = $cid_to_user[$contact['cid']];
                if ($user) {
                    $data[$i]['user'] = $user;
                }
            }
            break;
    }
    return $data;
}
/**
 * Send emails to any members with a positive balance.
 */
function command_amazon_payment_email()
{
    global $config_email_from;
    global $config_site_title;
    // Get balances and contacts
    $cids = payment_contact_filter(array('balance_due' => true));
    $balances = payment_accounts(array('cid' => $cids));
    $contacts = crm_get_data('contact', array('cid' => $cids));
    $cidToContact = crm_map($contacts, 'cid');
    // Email each contact with a balance
    foreach ($balances as $cid => $balance) {
        // Construct button
        $params = array('referenceId' => $cid, 'amount' => $balance['code'] . ' ' . payment_format_currency($balance, false), 'description' => 'CRM Dues Payment');
        $amount = payment_format_currency($balance);
        $button = theme('amazon_payment_button', $cid, $params);
        // Send email
        $to = $cidToContact[$cid]['email'];
        $subject = "[{$config_site_title}] Payment Due";
        $from = $config_email_from;
        $headers = "Content-type: text/html\r\nFrom: {$from}\r\n";
        $message = "<p>Hello,<br/>Your current account balance is {$amount}.  To pay this balance using Amazon Payments, please click the button below.</p>{$button}";
        $res = mail($to, $subject, $message, $headers);
    }
    message_register('E-mails have been sent');
    variable_set('amazon_payment_last_email', date('Y-m-d'));
    return crm_url('payments', array('query' => array('tab' => 'billing')));
}
Example #9
0
/**
 * Return the payment form structure.
 *
 * @param $pmtid The id of the key assignment to delete.
 * @return The form structure.
*/
function payment_delete_form($pmtid)
{
    // Ensure user is allowed to delete keys
    if (!user_access('payment_delete')) {
        return NULL;
    }
    // Get data
    $data = crm_get_data('payment', array('pmtid' => $pmtid));
    $payment = $data[0];
    // Construct key name
    $amount = payment_format_currency($payment);
    $payment_name = "Payment:{$payment['pmtid']} - {$amount}";
    if ($payment['credit_cid']) {
        $name = theme('contact_name', $payment['credit_cid']);
        $payment_name .= " - Credit: {$name}";
    }
    if ($payment['debit_cid']) {
        $name = theme('contact_name', $payment['debit_cid']);
        $payment_name .= " - Debit: {$name}";
    }
    // Create form structure
    $form = array('type' => 'form', 'method' => 'post', 'command' => 'payment_delete', 'hidden' => array('pmtid' => $payment['pmtid']), 'fields' => array(array('type' => 'fieldset', 'label' => 'Delete Payment', 'fields' => array(array('type' => 'message', 'value' => '<p>Are you sure you want to delete the payment "' . $payment_name . '"? This cannot be undone.'), array('type' => 'submit', 'value' => 'Delete')))));
    return $form;
}
Example #10
0
// Generate the request parameters
$q = 'Action=VerifySignature' . '&UrlEndPoint=' . rawurlencode('http://' . $config_host . base_path() . 'modules/amazon_payment/ipn.php') . '&HttpParameters=' . rawurlencode($http_params) . '&Version=2008-09-17';
// Send the request
$method = 'GET';
$url = 'https://fps.amazonaws.com/?' . $q;
$options = array('https' => array('method' => $method));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
// Check for success
if (strpos($result, '<VerifySignatureResult><VerificationStatus>Success</VerificationStatus></VerifySignatureResult>') === false) {
    die;
}
// Check if the payment already exists
// Skip transactions that have already been imported
$payment_opts = array('filter' => array('confirmation' => $_POST['transactionId']));
$data = crm_get_data('payment', $payment_opts);
if (count($data) > 0) {
    die;
}
// Parse the data and insert into the database
// 'USD 12.34' goes to ['USD', '1234']
$parts = explode(' ', $_POST['transactionAmount']);
file_put_contents($debug, print_r($parts, true) . "\n", FILE_APPEND);
$payment_amount = payment_parse_currency($parts[1], $parts[0]);
// Determine cid
$cid = $_POST['referenceId'];
if (empty($cid)) {
    // Check if the amazon name is linked to a contact
    $opts = array('filter' => array('amazon_name' => $_POST['buyerName']));
    $contact_data = amazon_payment_contact_data($opts);
    if (count($contact_data) > 0) {
/**
 * Return the delete paypal contact form structure.
 *
 * @param $cid The cid of the paypal contact to delete.
 * @return The form structure.
*/
function paypal_payment_contact_delete_form($cid)
{
    // Ensure user is allowed to delete paypal contacts
    if (!user_access('payment_edit')) {
        return crm_url('paypal-admin');
    }
    // Get paypal contact data
    $data = crm_get_data('paypal_payment_contact', array('cid' => $cid));
    $paypal_payment_contact = $data[0];
    // Construct paypal contact name
    $paypal_payment_contact_name = "paypal contact:{$paypal_payment_contact['cid']} email:{$paypal_payment_contact['paypal_email']}";
    // Create form structure
    $form = array('type' => 'form', 'method' => 'post', 'command' => 'paypal_payment_contact_delete', 'hidden' => array('cid' => $paypal_payment_contact['cid']), 'fields' => array(array('type' => 'fieldset', 'label' => 'Delete Paypal Contact', 'fields' => array(array('type' => 'message', 'value' => '<p>Are you sure you want to delete the paypal contact "' . $paypal_payment_contact_name . '"? This cannot be undone.'), array('type' => 'submit', 'value' => 'Delete')))));
    return $form;
}
Example #12
0
/**
 * Page hook.  Adds contact module content to a page before it is rendered.
 *
 * @param &$page_data Reference to data about the page being rendered.
 * @param $page_name The name of the page being rendered.
*/
function contact_page(&$page_data, $page_name)
{
    switch ($page_name) {
        case 'contacts':
            // Set page title
            page_set_title($page_data, 'Contacts');
            // Add view tab
            if (user_access('contact_view')) {
                $opts = array('show_export' => true, 'exclude' => array('emergencyName', 'emergencyPhone'));
                $view = theme('table', 'contact', $opts);
                page_add_content_top($page_data, $view, 'View');
            }
            // Add add tab
            if (user_access('contact_add')) {
                page_add_content_top($page_data, theme('form', crm_get_form('contact')), 'Add');
            }
            break;
        case 'contact':
            // Capture contact id
            $cid = $_GET['cid'];
            if (empty($cid)) {
                return;
            }
            if (!user_access('contact_view') && $cid !== user_id()) {
                error_register('Permission denied: contact_view');
                return;
            }
            $contact_data = crm_get_data('contact', array('cid' => $cid));
            $contact = $contact_data[0];
            // Set page title
            page_set_title($page_data, theme('contact_name', $contact));
            // Add view tab
            $view_content = '';
            if (user_access('contact_view')) {
                $view_content .= '<h3>Contact Info</h3>';
                $opts = array('cid' => $cid, 'ops' => false);
                $view_content .= theme('table_vertical', 'contact', array('cid' => $cid));
            }
            if (!empty($view_content)) {
                page_add_content_top($page_data, $view_content, 'View');
            }
            // Add edit tab
            if (user_access('contact_edit') || $cid == user_id()) {
                $opts = array('cid' => $cid);
                $form = crm_get_form('contact', $opts);
                page_add_content_top($page_data, theme('form', $form), 'Edit');
            }
            break;
    }
}