<?php

/**
 * @copyright (c) Johnny Mast.
 * @version 1.3
 * @author Johnny Mast <j.mast@peoplesplayground.nl>
 * @since Version 1.0
 */

/**
 * Sets the information about the content-type so Drupal can display it 
 * on the create content page.
 *
 * @since 1.0
 * @return array with content-type information.
 */
function paypal_donate_node_info() {
    return array(
        'paypal_donate' => array(
            'name' => t('Paypal donate page'),
            'base' => 'paypal_donate',
            'description' => t('Allow your users to leave you a donation'),
        )
    );
}

/**
 * Returns a list of permissions that can be used for this module.
 * 
 * @since 1.2
 * @return array An array of valid permissions for the onthisdate module
 */
function paypal_donate_permission() {
    return array(
        'create paypal_page' => array(
            'title' => t('Create a new paypal page'),
            'description' => t('Allows users to create paypal donation pages'),
        ),
        'edit own paypal_page' => array(
            'title' => t('Edit own paypal page'),
            'description' => t('Allows users to edit there own paypal donation pages'),
        ),
    );
}

/**
 * Determine if the given user has access to this given object. (System hook)
 *
 * @since 1.0
 * @param $node The node on which the operation is to be performed, or, if it does not yet exist, the type of node to be created.
 * @param $op   The operation to be performed. Possible values: "create", "delete", "update", "view"
 * @param $account A user object representing the user for whom the operation is to be performed.
 * @return bool  user has access true or false.
 */
function paypal_donate_access($node, $op, $account) {

    if ($op == 'create') {
        // Only users with permission to do so may create this node type.
        return user_access('create paypal donate', $account);
    }

    if ($op == 'update' || $op == 'delete') {
        if (user_access('edit own paypal page', $account) && ($account->uid == $node->uid)) {
            return TRUE;
        }
    }
}

/**
 * Slight ateration to make the email field required.
 * 
 * @since 1.4
 * @param $form Nested array of form elements that comprise the form.
 * @param $form_state A keyed array containing the current state of the form. The arguments that drupal_get_form() was originally called with are available in the array $form_state['build_info']['args'].
 * @param $form_id String representing the name of the form itself. Typically this is the name of the function that generated the form.
 * @return void
 */
function paypal_donate_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'paypal_donate_node_form') {
        $form['paypal_donate_email']['und'][0]['value']['#required'] = true;
    }
}

/**
 * Define all themes used in this module. These theme items
 * can be used in the module here using the theme function.
 *
 * @since 1.0
 * @return array with theme information
 */
function paypal_donate_theme() {
    $items = array();
    $items['paypal_form'] = array(
        'template' => 'paypal_form',
        'arguments' => array('node' => NULL)
    );
    return $items;
}

/**
 * This function is called with every node event. In this case we use this function to render 
 * the Paypal form on the user side of the website (So on watching the content). It will precent
 * the Paypal button for users to donate money to the website.
 * 
 * @since 1.0
 */
function paypal_donate_view(&$node, $view_mode) {
    if (user_access('access content')) {
        if ($node->type == 'paypal_donate') {
            foreach ($node as $delta => $item) {
                if (is_array($item)) {
                    if (isset($item['und'])) {
                        $array = current($item['und']);
                        if (isset($array['value'])){
                        $node->fields[$delta] = $array['value'];
                        unset($node->$delta);
                        }   
                    }
                }
            }

            if (isset($node->fields['body']) == false) {
                $node->fields['body'] = '';
            }
            
            $node->content['paypal_donate_view_paypalform'] = array(
                '#markup' => paypal_donate_view_paypalform($node, $view_mode)
            );
        }
    }
    return $node;
}

/**
 * This function is a wrapper to populate the node content by calling the 
 * theme function
 * 
 * @since 1.0
 * @see paypal_donate_theme()
 */
function paypal_donate_view_paypalform(&$node, $view_mode) {
    return theme('paypal_form', array('node' => $node));
}

/**
 * This function returns the form that will displayed on the content creation page.
 *
 * @since 1.0
 * @param $node object to pass the values to the form.
 * @param $form_state The form state array. Changes made to this variable will have no effect.
 * @return array form with node settings.
 */
function paypal_donate_form(&$node, $form_state) {
    return node_content_form($node, $form_state);
}
