6.x-2.x branch
Main module file for the Storm module. Basic Storm module provides a dashboard and some API functions. 1: Hooks (help, perm, init, menu, theme) 2: Dashboard 3: Admin Settings 4: Date / time manipulation 5: Taxation functions 6: Storm Icons 7: SQL Functions 8: Node form theming
- <?php
-
- /**
- * @file
- * Main module file for the Storm module.
- * Basic Storm module provides a dashboard and some API functions.
- * 1: Hooks (help, perm, init, menu, theme)
- * 2: Dashboard
- * 3: Admin Settings
- * 4: Date / time manipulation
- * 5: Taxation functions
- * 6: Storm Icons
- * 7: SQL Functions
- * 8: Node form theming
- */
-
- // HOOKS
- /**
- * @function
- * Implementation of hook_help().
- */
- function storm_help($path, $arg) {
- $output = '';
-
- switch ($path) {
- case "admin/help#storm":
- $output = '<p>'. t("Provides a complete project management environment") .'</p>';
- break;
- case "admin/help#stormattribute":
- $output = '<p>'. t("Provides attributes support for Storm") .'</p>';
- $output .= '<p>'. t("Price Modes - Price modes are used calculate the price of the Storm node when added to an invoice. A price mode can be added to any Storm node type and any type can be added to an invoice. When a node is added to an invoice it looks for a price mode in the following order:") .'</p>';
- $output .= '<p>'. t("Ticket, Task, Project, Organization.") .'</p>';
- $output .= '<p>'. t("It will take the price mode for it's current node or the first valid node type above in it's tree. e.g. For a task node type, if that node doesn't have a price mode it will look at the project and then the organization and take the first one it finds. This means you can define a price mode for the organization and it will be used for all nodes under that organization unless it's given a different one.") .'</p>';
- $output .= '<p>'. t("The following price modes keys are defined:") .'<br />';
- $output .= '<ul>';
- $output .= '<li>'. t("not applicable - This is ignored by the system. This means that no price mode is defined.") .'</li>';
- $output .= '<li>'. t("fixed - Price of 0 is used (so the invoice would need to be manually updated.") .'</li>';
- $output .= '<li>'. t("hourly - Price taken from node and multiplied by billing duration.") .'</li>';
- $output .= '<li>'. t("daily - Price given is for daily rate. This price is divided by 8 hours and then multiplied by the billing duration.") .'</li>';
- $output .= '<li>'. t("fixed_price - Will use the price given as the fixed price for that invoice item.") .'</li>';
- $output .= '</ul>';
- break;
- }
-
- return $output;
- }
-
- /**
- * @function
- * Implementation of hook_perm().
- */
- function storm_perm() {
- return array(
- 'Storm: access dashboard',
- 'Storm: access administration pages',
- );
- }
-
- // Note #370120. It is intended to move these calls to pages which specifically need them rather than on hook_init.
- function storm_init() {
- drupal_add_js(drupal_get_path('module', 'storm') .'/storm.js', 'module', 'header', FALSE);
- drupal_add_css(drupal_get_path('module', 'storm') .'/storm.css', 'module');
- }
-
- /**
- * @function
- * Implementation of hook_menu().
- */
- function storm_menu() {
- $items = array();
-
- $dashboard_types = module_invoke_all('storm_dashboard_types');
- foreach ($dashboard_types as $type => $type_info) {
- if (isset($type_info['url'])) {
- $title = isset($type_info['title']) ? $type_info['title'] : 'Storm Dashboard';
- $items[$type_info['url']] = array(
- 'title' => $title,
- 'description' => $title,
- 'page callback' => 'storm_dashboard',
- 'page arguments' => array($type),
- 'access arguments' => array('Storm: access dashboard'),
- 'parent' => '',
- 'type' => MENU_NORMAL_ITEM,
- );
- if (isset($type_info['menu_options'])) {
- $items[$type_info['url']] = array_merge($items[$type_info['url']], $type_info['menu_options']);
- }
- }
- }
-
- $items['admin/settings/storm'] = array(
- 'title' => 'Storm',
- 'description' => 'Storm administration page',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('storm_admin_settings'),
- 'access arguments' => array('Storm: access administration pages'),
- 'type' => MENU_NORMAL_ITEM,
- );
-
- $items['admin/settings/storm/storm'] = array(
- 'title' => 'Storm',
- 'description' => 'Storm administration page',
- 'access arguments' => array('Storm: access administration pages'),
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => -100,
- );
-
- $items['storm/attributes'] = array(
- 'title' => 'Attributes',
- 'description' => 'Storm attributes',
- 'page callback' => 'storm_attribute_list',
- 'access arguments' => array('Storm: access administration pages'),
- 'file' => 'storm.admin.inc',
- 'type' => MENU_NORMAL_ITEM,
- 'weight' => 11,
- );
-
- $items['storm/attributes/add'] = array(
- 'title' => 'Add a new attribute',
- 'description' => 'Storm attributes',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('storm_attribute_add'),
- 'access arguments' => array('Storm: access administration pages'),
- 'file' => 'storm.admin.inc',
- 'type' => MENU_CALLBACK);
-
- $items['storm/attributes/edit/%'] = array(
- 'title' => 'Edit an attribute',
- 'description' => 'Storm attributes',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('storm_attribute_edit', 3),
- 'access arguments' => array('Storm: access administration pages'),
- 'file' => 'storm.admin.inc',
- 'type' => MENU_CALLBACK);
-
- $items['storm/attributes/delete/%'] = array(
- 'title' => 'Delete an attribute',
- 'description' => 'Storm attributes',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('storm_attribute_delete', 3),
- 'access arguments' => array('Storm: access administration pages'),
- 'file' => 'storm.admin.inc',
- 'type' => MENU_CALLBACK);
-
- $items['storm/attributes/domain/autocomplete'] = array(
- 'title' => 'List of attributes',
- 'description' => 'Storm attributes',
- 'page callback' => '_storm_attribute_domain_autocomplete',
- 'page arguments' => array(4),
- 'access arguments' => array('Storm: access administration pages'),
- 'file' => 'storm.admin.inc',
- 'type' => MENU_CALLBACK);
-
- return $items;
- }
-
- function storm_theme() {
- return array(
- 'storm_form_group' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('header', 'form'),
- ),
- 'datetime' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('element'),
- ),
- 'dateext' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('element'),
- ),
- 'storm_link' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('source_module', 'destination_module', 'node_nid', 'weight'),
- ),
- 'storm_list_report' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('header', 'rows', 'title', 'footer', 'headtitle'),
- ),
- 'storm_report' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('header', 'content', 'title', 'footer'),
- ),
- 'storm_view_item' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('label', 'value'),
- ),
- 'storm_dashboard' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('links' => array()),
- ),
- 'storm_dashboard_block' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('links' => array()),
- ),
- 'storm_dashboard_link' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('link_blocks' => array()),
- ),
- 'storm_dashboard_links_weight_table' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('form' => array()),
- ),
- 'storm_number_items' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('number' => ''),
- ),
- 'storm_attribute_list' => array(
- 'file' => 'storm.theme.inc',
- 'arguments' => array('form'),
- ),
- );
- }
-
-
- /**
- * Function to create a dashboard (call to theme function)
- *
- * @param string $type dashboard type, used for storm_get_dashboard_links
- * @param null|string $theme Theme function to use, if omitted it uses storm_dashboard
- * @return string themed string
- */
- function storm_dashboard($type = 'page') {
- $types = module_invoke_all('storm_dashboard_types');
- // only set Page Title if it has a url, so there is a menu entry for that
- // The block type has no url and shouldn't change the title!
- if (isset($types[$type]['url']) && isset($types[$type]['title'])) {
- drupal_set_title($types[$type]['title']);
- }
- drupal_add_css(drupal_get_path('module', 'storm') .'/storm-dashboard.css');
-
- $link_blocks = storm_dashboard_get_links(TRUE, $type);
- if (!empty($link_blocks)) {
- // DIVIDE LINKS INTO TWO BLOCKS
- $count = ceil(count($link_blocks) / 2);
- $link_blocks = array_chunk($link_blocks, $count);
- }
- $theme = isset($types[$type]['theme']) ? $types[$type]['theme'] : 'storm_dashboard';
- $content = theme($theme, $link_blocks);
-
- return $content;
- }
-
- /**
- * Return links array for the storm dashboard.
- *
- * @param bool $check_active when FALSE, returns all links, no matter if activated or not (needed for admin settings)
- * @param string $type
- * @return array dashboard links
- */
- function storm_dashboard_get_links($check_active = TRUE, $type = 'page') {
-
- $links = module_invoke_all('storm_dashboard_links', $type);
-
- if (!empty($links)) {
-
- $default_dashboard_settings = variable_get('storm_'. $type .'dashboard_settings', array());
-
- $weight = 0;
- foreach ($links as $key => &$link_array) {
-
- // ACTIVE CHECK
- if ($check_active && isset($default_dashboard_settings[$link_array['path']]['active']) && $default_dashboard_settings[$link_array['path']]['active'] == FALSE) {
- unset($links[$key]);
- continue;
- }
-
- // MODULE EXIST CHECK
- if (isset($link_array['destination_module']) && !module_exists($link_array['destination_module'])) {
- unset($links[$key]);
- continue;
- }
-
- // ACCESS CHECK
- if (!empty($link_array['access_callback'])) {
- if (function_exists($link_array['access_callback'])) {
- if (!isset($link_array['access_arguments'])) {
- $link_array['access_arguments'] = "";
- }
- // USE THE ACCESS CHECK OF THE MENU API
- _menu_check_access($link_array, $link_array['map']);
- }
- }
- elseif (isset($link_array['access_arguments'])) {
- $link_array['access'] = user_access($link_array['access_arguments']);
- }
- else {
- $link_array['access'] = TRUE;
- }
-
- if (!$link_array['access']) {
- unset($links[$key]);
- }
-
- if (isset($default_dashboard_settings[$link_array['path']]['weight'])) {
- $link_array['weight'] = $default_dashboard_settings[$link_array['path']]['weight'];
- }
- elseif (!isset($link_array['weight'])) {
- $link_array['weight'] = $weight;
- $weight++;
- }
-
- }
-
- // HOOK FOR ALTERING LINKS
- drupal_alter('storm_dashboard_links', $links, $type);
-
- // SORT LINKS BY WEIGHT
- uasort($links, '_storm_dashboard_sort_links');
-
- }
- return $links;
- }
-
- /**
- * Helper function for storm_dashboard_get_links().
- * Order dashboard links by their weight.
- * This is a uasort callback function.
- *
- * @see uasort()
- * @param array $a dashboard link array
- * @param array $b dashboard link array
- * @return int
- */
- function _storm_dashboard_sort_links($a, $b) {
- if (intval($a['weight']) == intval($b['weight'])) {
- return 0;
- }
- elseif (intval($a['weight']) < intval($b['weight'])) {
- return -1;
- }
- else {
- return 1;
- }
- }
-
- function storm_storm_dashboard_links($type) {
- $links = array();
- if ($type == 'page' || $type == 'block') {
- $links[] = array(
- 'title' => t('Configuration'),
- 'icon' => 'stormconfiguration',
- 'path' => 'admin/settings/storm',
- 'params' => array(),
- 'access_arguments' => 'Storm: access administration pages',
- 'node_type' => '',
- 'add_type' => '',
- 'map' => array(),
- 'weight' => 15,
- );
- $links[] = array(
- 'theme' => 'storm_dashboard_link',
- 'title' => t('Attributes'),
- 'icon' => 'stormattributes',
- 'path' => 'storm/attributes',
- 'params' => array(),
- 'access_arguments' => 'Storm: access administration pages',
- 'map' => array(),
- 'weight' => 14,
- );
- }
- return $links;
- }
-
- /**
- * Implementation of hook_storm_dashboard_types().
- * Defines dashboard types page and block.
- * This can be used as an example for other types
- * @return array
- */
- function storm_storm_dashboard_types() {
- return array(
- 'page' => array(
- // URL: menu path which should be used, if omitted no menu item will be created
- 'url' => 'storm',
- // title: Required. Title of menu item, page title and title for the storm settings
- 'title' => 'Storm dashboard',
- // description: Required. Description for the fieldset in the storm settings
- 'description' => t('You can disable or reorder the links from the !dashboard here', array('!dashboard' => l(t('dashboard'), 'storm'))),
- // theme: theme which should be used to display this dashboard. If omitted uses standard theme storm_dashboard
- 'theme' => 'storm_dashboard',
- // menu_options: This array will be merged into the default menu item array. If the menu should have special access arguments, title, it can be set here.
- // page callback and page argument shouldn't been overwritten.
- 'menu_options' => array(
- 'title' => 'Storm',
- 'description' => 'Storm dashboard',
- ),
- ),
- 'block' => array(
- 'title' => 'Dashboard block',
- 'description' => t('You can disable or reorder the links from the dashboard !block here', array('!block' => l(t('block'), 'admin/build/block'))),
- 'theme' => 'storm_dashboard_block',
- ),
- );
- }
-
- /**
- * Implementation of hook_block().
- */
- function storm_block($op = 'list', $delta = 0) {
- switch ($op) {
- case 'list' :
- // DASHBOARD AS TOP NAVIGATION
- $blocks['storm_dashboard_block']['info'] = t('Storm dashboard block');
- return $blocks;
- break;
- case 'view':
- switch ($delta) {
- // DASHBOARD AS BLOCK
- case 'storm_dashboard_block':
- $block = array(
- 'subject' => '',
- 'content' => storm_dashboard('block'),
- );
- return $block;
- }
- break;
- }
- }
-
- /**
- * @function
- * Defines the administration settings form for the Storm module
- */
- function storm_admin_settings() {
- $form = array();
- $w = -10;
-
- $form['icons'] = array(
- '#type' => 'fieldset',
- '#title' => t('Icons'),
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#weight' => $w++,
- );
-
- $form['icons']['storm_icons_display'] = array(
- '#type' => 'checkbox',
- '#title' => t('Display Storm icons'),
- '#default_value' => variable_get('storm_icons_display', TRUE),
- '#description' => t('The icons that ship with Storm may not fit well with some themes. If this box is unchecked, icons will be hidden.'),
- '#weight' => $w++,
- );
-
- $form['icons']['storm_icons_path'] = array(
- '#type' => 'textfield',
- '#title' => t('Icons directory'),
- '#default_value' => variable_get('storm_icons_path', drupal_get_path('module', 'storm') .'/icons'),
- '#description' => t("The directory that contains Storm's icons."),
- '#weight' => $w++,
- '#element_validate' => array('storm_admin_settings_icons_path_validate'),
- );
-
- $form['lists'] = array(
- '#type' => 'fieldset',
- '#title' => t('Lists'),
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#weight' => $w++,
- );
-
- $form['lists']['storm_default_items_per_page'] = array(
- '#type' => 'textfield',
- '#title' => t('Default Items per Page'),
- '#default_value' => variable_get('storm_default_items_per_page', 10),
- '#description' => t('Default items per page when viewing lists'),
- '#size' => 5,
- '#weight' => $w++
- );
-
- $form['reports'] = array(
- '#type' => 'fieldset',
- '#title' => t('Reports'),
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#weight' => $w++,
- );
-
- $form['reports']['storm_report_header'] = array(
- '#type' => 'textarea',
- '#title' => t('Report header'),
- '#default_value' => variable_get('storm_report_header', ''),
- '#description' => t('The header that will appear on Storm reports'),
- '#weight' => $w++,
- );
-
- $form['yearsrange'] = array(
- '#type' => 'fieldset',
- '#title' => t('Years range in dates'),
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#weight' => $w++,
- );
-
- $form['yearsrange']['group0'] = array(
- '#type' => 'markup',
- '#theme' => 'storm_form_group',
- '#weight' => $w++,
- );
-
- $form['yearsrange']['group0']['storm_yearsrangebegin'] = array(
- '#type' => 'select',
- '#title' => t('Begin'),
- '#options' => drupal_map_assoc(range(1970, 2037)),
- '#default_value' => variable_get('storm_yearsrangebegin', 2001),
- );
-
- $form['yearsrange']['group0']['storm_yearsrangeend'] = array(
- '#type' => 'select',
- '#title' => t('End'),
- '#options' => drupal_map_assoc(range(1970, 2037)),
- '#default_value' => variable_get('storm_yearsrangeend', 2012),
- );
-
- $form['taxation'] = array(
- '#type' => 'fieldset',
- '#title' => t('Taxation defaults'),
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#weight' => $w++,
- );
-
- $form['taxation']['storm_tax1_app'] = array(
- '#type' => 'select',
- '#title' => t('Tax 1: Application'),
- '#default_value' => variable_get('storm_tax1_app', 1),
- '#description' => t('The method of application to use for Tax 1'),
- '#options' => array(
- 1 => t('Apply to item amount'),
- 0 => t('Do not apply tax'),
- ),
- '#weight' => $w++,
- );
-
- $form['taxation']['group0'] = array(
- '#type' => 'markup',
- '#theme' => 'storm_form_group',
- '#weight' => $w++,
- );
-
- $form['taxation']['group0']['storm_tax1_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Tax 1: Name'),
- '#default_value' => variable_get('storm_tax1_name', 'VAT'),
- '#description' => t('The name to use for Tax 1'),
- '#weight' => $w++,
- );
-
- $form['taxation']['group0']['storm_tax1_percent'] = array(
- '#type' => 'textfield',
- '#title' => t('Tax 1: Default percentage'),
- '#default_value' => variable_get('storm_tax1_percent', 20),
- '#description' => t('Default percentage for Tax 1'),
- '#size' => 20,
- '#weight' => $w++
- );
-
- $form['taxation']['storm_tax2_app'] = array(
- '#type' => 'select',
- '#title' => t('Tax 2: Application'),
- '#default_value' => variable_get('storm_tax2_app', 0),
- '#description' => t('The method of application to use for Tax 2'),
- '#options' => array(
- 2 => t('Apply to total of item amount plus previous tax'),
- 1 => t('Apply to item amount'),
- 0 => t('Do not apply tax'),
- ),
- '#weight' => $w++,
- );
-
- $form['taxation']['group1'] = array(
- '#type' => 'markup',
- '#theme' => 'storm_form_group',
- '#weight' => $w++,
- );
-
- $form['taxation']['group1']['storm_tax2_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Tax 2: Name'),
- '#default_value' => variable_get('storm_tax2_name', 'Tax 2 Name'),
- '#description' => t('The name to use for Tax 2'),
- '#weight' => $w++,
- );
-
- $form['taxation']['group1']['storm_tax2_percent'] = array(
- '#type' => 'textfield',
- '#title' => t('Tax 2: Default percentage'),
- '#default_value' => variable_get('storm_tax2_percent', 20),
- '#description' => t('Default percentage for Tax 2'),
- '#size' => 20,
- '#weight' => $w++,
- );
-
- $form['taxation']['storm_tax_display'] = array(
- '#type' => 'checkbox',
- '#title' => t('Display tax edit fields'),
- '#default_value' => variable_get('storm_tax_display', TRUE),
- '#description' => t('If disabled, all tax fields will use the default values and you will not be able to override them for any nodes until this setting is enabled again.'),
- '#weight' => $w++,
- );
-
- $form['taxation']['storm_tax2_display'] = array(
- '#type' => 'checkbox',
- '#title' => t('Display tax 2 edit fields'),
- '#default_value' => variable_get('storm_tax2_display', TRUE),
- '#description' => t('If disabled, tax 2 fields will use the default values and you will not be able to override them for any nodes until this setting is enabled again. This setting will be ignored unless the general "Display tax edit fields" setting is enabled above.'),
- '#weight' => $w++,
- );
-
- // DASHBOARD SETTINGS
- $form['storm_dashboard_settings'] = array(
- '#type' => 'fieldset',
- '#title' => t('Dashboard settings'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- );
-
- $types = module_invoke_all('storm_dashboard_types');
- foreach ($types as $type => $type_data) {
- $all_links_options = array();
- $all_links = storm_dashboard_get_links(FALSE, $type);
- foreach ($all_links as $link) {
- $all_links_options[$link['path']] = l($link['title'], $link['path']);
- }
-
- $default_dashboard_settings = variable_get('storm_'. $type .'dashboard_settings', array());
-
- $form['storm_dashboard_settings'][$type] = array(
- '#type' => 'fieldset',
- '#title' => t($type_data['title']),
- '#description' => $type_data['description'],
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- );
- $form['storm_dashboard_settings'][$type]['dashboard_links'] = array(
- '#theme' => 'storm_dashboard_links_weight_table',
- '#infix' => $type,
- );
-
- $weight = 0;
- foreach ($all_links_options as $path => $title) {
- $form['storm_dashboard_settings'][$type]['dashboard_links'][$path][$type .'_storm_dashboard_link_active_'. $path] = array(
- '#type' => 'checkbox',
- '#default_value' => isset($default_dashboard_settings[$path]['active']) ? $default_dashboard_settings[$path]['active'] : TRUE,
- );
- $form['storm_dashboard_settings'][$type]['dashboard_links'][$path][$type .'_storm_dashboard_link_weight_'. $path] = array(
- '#type' => 'weight',
- '#default_value' => isset($default_dashboard_settings[$path]['weight']) ? $default_dashboard_settings[$path]['weight'] : $weight,
- '#delta' => 30,
- );
- $form['storm_dashboard_settings'][$type]['dashboard_links'][$path]['#value'] = $title;
- $weight++;
- }
- }
-
- if (empty($form['#submit'])) {
- $form['#submit'] = array();
- }
- $form['#submit'] = array('storm_admin_settings_form_submit');
-
- return system_settings_form($form);
- }
-
- /**
- * Validate Icon Path form element.
- *
- * @param $form
- * @param $form_values
- */
- function storm_admin_settings_icons_path_validate($form, $form_values) {
- $icon_path_old = variable_get('storm_icons_path', drupal_get_path('module', 'storm') .'/icons');
- $icon_path = $form_values['values']['storm_icons_path'];
- // check if directory exists
- if (!is_dir($icon_path)) {
- form_set_error('storm_icons_path', t('Icon path %path does not exist', array('%path' => $icon_path)));
- }
- // if it exists and changed, delete cache, so there are no old values in the cache
- elseif ($icon_path != $icon_path_old) {
- cache_clear_all('storm:icons', 'cache', FALSE);
- }
- }
-
- function storm_admin_settings_form_submit($form, $form_state) {
- $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
- // RESET
- if ($op == t('Reset to defaults')) {
- $types = module_invoke_all('storm_dashboard_types');
- foreach ($types as $type => $type_data) {
- variable_del('storm_'. $type .'dashboard_settings');
- };
- return;
- }
-
- // GET OPTIONS
- $types = module_invoke_all('storm_dashboard_types');
- foreach ($types as $type => $type_data) {
- $all_links = storm_dashboard_get_links(FALSE, $type);
- $settings = array();
- foreach ($all_links as $link) {
- $path = $link['path'];
- $settings[$path]['active'] = $form_state['values'][$type .'_storm_dashboard_link_active_'. $path];
- $settings[$path]['weight'] = $form_state['values'][$type .'_storm_dashboard_link_weight_'. $path];
- }
- variable_set('storm_'. $type .'dashboard_settings', $settings);
- }
-
- }
-
- // DATE / TIME MANIPULATION
- function storm_elements() {
- $type['datetime'] = array(
- '#input' => TRUE,
- '#process' => array('storm_datetime_expand'),
- '#element_validate' => array('storm_datetime_validate'),
- '#default_value' => array(
- 'day' => format_date(time(), 'custom', 'j'),
- 'month' => format_date(time(), 'custom', 'n'),
- 'year' => format_date(time(), 'custom', 'Y'),
- 'hour' => format_date(time(), 'custom', 'H'),
- 'minute' => format_date(time(), 'custom', 'i'),
- ),
- );
- $type['dateext'] = array(
- '#input' => TRUE,
- '#process' => array('storm_dateext_expand'),
- '#element_validate' => array('storm_dateext_validate'),
- '#default_value' => time(),
- '#withnull' => FALSE,
- '#disable_date_popup' => FALSE,
- );
- return $type;
- }
-
- function storm_datetime_expand($element) {
- if (empty($element['#value'])) {
- $element['#value'] = array(
- 'day' => format_date(time(), 'custom', 'j'),
- 'month' => format_date(time(), 'custom', 'n'),
- 'year' => format_date(time(), 'custom', 'Y'),
- 'hour' => format_date(time(), 'custom', 'H'),
- 'minute' => format_date(time(), 'custom', 'i'),
- );
- }
-
- $element['#tree'] = TRUE;
-
- // Determine the order of day, month, year in the site's chosen date format.
- $format = variable_get('date_format_short', 'm/d/Y - H:i');
- $sort = array();
- $sort['day'] = max(strpos($format, 'd'), strpos($format, 'j'));
- $sort['month'] = max(strpos($format, 'm'), strpos($format, 'M'));
- $sort['year'] = strpos($format, 'Y');
- $sort['hour'] = strpos($format, 'H');
- $sort['minute'] = strpos($format, 'i');
- asort($sort);
- $order = array_keys($sort);
-
- // Output multi-selector for date.
- foreach ($order as $type) {
- switch ($type) {
- case 'year':
- $options = drupal_map_assoc(range(variable_get('storm_yearsrangebegin', 2001), variable_get('storm_yearsrangeend', 2012)));
- break;
- case 'month':
- $options = drupal_map_assoc(range(1, 12), 'map_month');
- break;
- case 'day':
- $options = drupal_map_assoc(range(1, 31));
- break;
- case 'hour':
- $options = drupal_map_assoc(range(0, 23));
- break;
- case 'minute':
- $options = drupal_map_assoc(range(0, 59));
- break;
- }
- $parents = $element['#parents'];
- $parents[] = $type;
- $element[$type] = array(
- '#type' => 'select',
- '#value' => $element['#value'][$type],
- '#attributes' => $element['#attributes'],
- '#options' => $options,
- );
- }
- return $element;
- }
-
- function storm_datetime_validate($form) {
- if (!checkdate($form['#value']['month'], $form['#value']['day'], $form['#value']['year'])) {
- form_error($form, t('The specified date is invalid.'));
- }
- }
-
- function storm_dateext_expand($element, $edit, $form_state, $form) {
- if (empty($element['#value'])) {
- if (!$element['#withnull']) {
- $element['#value'] = array(
- 'day' => format_date(time(), 'custom', 'j'),
- 'month' => format_date(time(), 'custom', 'n'),
- 'year' => format_date(time(), 'custom', 'Y'),
- );
- }
- else {
- $element['#value'] = array('day' => -1, 'month' => -1, 'year' => -1);
- }
- }
-
- $element['#tree'] = TRUE;
-
- // If date popup exists, we should use date popup,
- // but you can force to disable it by set disable_date_popup to true
- if (module_exists('date_popup') && (!isset($element['disable_date_popup']) || !$element['disable_date_popup'])) {
-
- // value is timestamp
- if (is_numeric($element['#value']) && $element['#value'] != 0) {
- $element['#value'] = array('popup' => format_date($element['#value'], 'custom', 'Y-m-d'));
- }
- // value is date array
- elseif (is_array($element['#value']) && !isset($element['#value']['popup']) && $element['#value']['day'] != -1) {
- $element['#value'] = array('popup' => sprintf("%04d-%02d-%02d", $element['#value']['year'], $element['#value']['month'], $element['#value']['day']));
- }
- elseif (is_numeric($element['#value']) || !isset($element['#value']['popup']) || (isset($element['#value']['day']) && $element['#value']['day'] == -1)) {
- $element['#value'] = array('popup' => '');
- }
-
- if (is_array($element['#value']['popup']) && isset($element['#value']['popup']['date'])) {
- $value = $element['#value']['popup']['date'];
- }
- else {
- $value = $element['#value']['popup'];
- }
-
- $current_year = date('Y');
- $begin = variable_get('storm_yearsrangebegin', 2001) - $current_year;
- if ($begin > 0) {
- $begin = '+'. $begin;
- }
- $end = variable_get('storm_yearsrangeend', 2012) - $current_year;
- if ($end > 0) {
- $end = '+'. $end;
- }
-
- $parents = $element['#parents'];
- $parents[] = 'popup';
- $element['popup'] = array(
- '#type' => 'date_popup',
- '#date_timezone' => date_default_timezone_name(),
- '#date_format' => "Y-m-d",
- '#date_year_range' => $begin .':'. $end,
- '#default_value' => $value,
- '#attributes' => $element['#attributes'],
- '#withnull' => $element['#withnull'],
- );
- }
- // fallback - use select boxes to choose the date
- else {
-
- // its a timestamp, convert it to a date format
- if (is_numeric($element['#value']) && $element['#value'] != 0) {
- $element['#value'] = _storm_gmtimestamp_to_date($element['#value']);
- }
-
- // Determine the order of day, month, year in the site's chosen date format.
- $format = variable_get('date_format_short', 'm/d/Y - H:i');
- $sort = array();
- $sort['day'] = max(strpos($format, 'd'), strpos($format, 'j'));
- $sort['month'] = max(strpos($format, 'm'), strpos($format, 'M'));
- $sort['year'] = strpos($format, 'Y');
- asort($sort);
- $order = array_keys($sort);
-
- // Output multi-selector for date.
- foreach ($order as $type) {
- switch ($type) {
- case 'year':
- $options = drupal_map_assoc(range(variable_get('storm_yearsrangebegin', 2001), variable_get('storm_yearsrangeend', 2012)));
- break;
- case 'month':
- $options = drupal_map_assoc(range(1, 12), 'map_month');
- break;
- case 'day':
- $options = drupal_map_assoc(range(1, 31));
- break;
- }
- if ($element['#withnull']) {
- $options = array('-1' => '-') + $options;
- }
-
- if (empty($element['#attributes'])) {
- $element['#attributes'] = array();
- }
-
- $parents = $element['#parents'];
- $parents[] = $type;
- $element[$type] = array(
- '#type' => 'select',
- '#value' => isset($element['#value'][$type]) ? $element['#value'][$type] : NULL,
- '#options' => $options,
- '#attributes' => array_merge(array('onchange' => "storm_datext_tonull(this, '". $element['#id'] ."')"), $element['#attributes']),
- );
- }
- }
- return $element;
- }
-
- function storm_dateext_validate($element, &$form_state) {
- // value is a string, convert it back to array
- if (is_array($element['#value']) && isset($element['#value']['popup'])) {
- if (!$element['#withnull'] && empty($element['#value']['popup']['date'])) {
- form_set_error($element, t('Field %field is required.', array('%field' => !empty($element['#title']) ? $element['#title'] : '')));
- }
- if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $element['#value']['popup']['date'], $matches)) {
- $element['#value'] = array();
- $element['#value']['day'] = (int) $matches[3];
- $element['#value']['month'] = (int) $matches[2];
- $element['#value']['year'] = (int) $matches[1];
- }
- else {
- if (!$element['#withnull'] && !empty($element['#value']['popup']['date'])) {
- form_set_error($element, t('Wrong Format for Field %field. Format should be YYYY-MM-DD.', array('%field' => !empty($element['#title']) ? $element['#title'] : '')));
- }
- $element['#value'] = array('day' => -1, 'month' => -1, 'year' => -1);
- }
- form_set_value($element, $element['#value'], $form_state);
- }
-
- if ($element['#value']['day'] == -1 && !$element['#withnull']) {
- form_set_error($element, t('Field %field is required.', array('%field' => !empty($element['#title']) ? $element['#title'] : '')));
- }
- if ($element['#value']['day'] != -1 && !checkdate($element['#value']['month'], $element['#value']['day'], $element['#value']['year'])) {
- form_error($element, t('The specified date is invalid.'));
- }
- return $element;
- }
-
- function storm_dependent_select_process($form, $edit, $form_state, $complete_form) {
- unset($form['#needs_validation']);
- return $form;
- }
-
- function _timestamp_to_gm($timestamp, $timezone=NULL) {
- if (!isset($timezone)) {
- global $user;
- if (variable_get('configurable_timezones', 1) && $user->uid && drupal_strlen($user->timezone)) {
- $timezone = $user->timezone;
- }
- else {
- $timezone = variable_get('date_default_timezone', 0);
- }
- }
- $timestamp -= $timezone;
- return $timestamp;
- }
-
- function _storm_date_to_gmtimestamp($date, $timezone=NULL) {
- if ($date['month'] == -1 || $date['year'] == -1 || $date['day'] == -1) {
- return NULL;
- }
- else {
- $gmttimestamp = gmmktime(0, 0, 0, intval($date['month']), intval($date['day']), intval($date['year']));
- return _timestamp_to_gm($gmttimestamp, $timezone);
- }
- }
-
- function _storm_datetime_to_gmtimestamp($datetime, $timezone=NULL) {
- $gmttimestamp = gmmktime(intval($datetime['hour']), intval($datetime['minute']), 0, intval($datetime['month']),
- intval($datetime['day']), intval($datetime['year']));
- return _timestamp_to_gm($gmttimestamp, $timezone);
- }
-
- function _storm_gmtimestamp_to_datetime($timestamp, $timezone=NULL) {
- $datetime = array(
- 'day' => format_date($timestamp, 'custom', 'j', $timezone),
- 'month' => format_date($timestamp, 'custom', 'n', $timezone),
- 'year' => format_date($timestamp, 'custom', 'Y', $timezone),
- 'hour' => (int)format_date($timestamp, 'custom', 'H', $timezone),
- 'minute' => (int)format_date($timestamp, 'custom', 'i', $timezone),
- );
- return $datetime;
- }
-
- function _storm_gmtimestamp_to_date($timestamp, $timezone=NULL) {
- if ($timestamp) {
- $date = array(
- 'day' => format_date($timestamp, 'custom', 'j', $timezone),
- 'month' => format_date($timestamp, 'custom', 'n', $timezone),
- 'year' => format_date($timestamp, 'custom', 'Y', $timezone),
- );
- }
- else {
- $date = array(
- 'day' => -1,
- 'month' => -1,
- 'year' => -1,
- );
- }
-
- return $date;
- }
-
- function _storm_gmtimestamp_without_time($timestamp, $timezone=NULL) {
- $date = _storm_gmtimestamp_to_date($timestamp, $timezone);
- $gmttimestamp = gmmktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
- return _timestamp_to_gm($gmttimestamp, $timezone);
- }
-
- function _storm_strtotime($timestr='') {
- $timestr = drupal_substr($timestr, 0, 5);
- $time = array();
- $time['hour'] = 0;
- $time['minute'] = 0;
-
- $ar = explode(':', $timestr);
- if (is_array($ar)) {
- if (array_key_exists(0, $ar)) $time['hour'] = $ar[0];
- if (array_key_exists(1, $ar)) $time['minute'] = $ar[1];
- }
- return $time;
- }
-
- function _timetostr($time=array()) {
- $timestr = str_pad($time['hour'], 2, "0", STR_PAD_LEFT) .':'. str_pad($time['minute'], 2, "0", STR_PAD_RIGHT);
- return $timestr;
- }
-
-
- /**
- * @function
- * Calculates taxation for Storm nodes
- */
- function storm_taxation(&$node) {
- // If values are not set, then use default values
- if (!isset($node->tax1app)) {
- $node->tax1app = variable_get('storm_tax1_app', 'none');
- }
- if (!isset($node->tax1percent)) {
- $node->tax1percent = variable_get('storm_tax1_percent', '20');
- }
- if (!isset($node->tax2app)) {
- $node->tax2app = variable_get('storm_tax2_app', 'none');
- }
- if (!isset($node->tax2percent)) {
- $node->tax2percent = variable_get('storm_tax2_percent', '20');
- }
-
- switch ($node->tax1app) {
- case 0:
- $node->tax1 = 0;
- break;
- case 1:
- $node->tax1 = $node->amount * $node->tax1percent / 100;
- break;
- default:
- // ERROR
- drupal_set_message(t('Error during tax calculations (Storm module)'), 'error');
- }
-
- $node->subtotal = $node->amount + $node->tax1;
-
- switch ($node->tax2app) {
- case 0:
- $node->tax2 = 0;
- break;
- case 1:
- $node->tax2 = $node->amount * $node->tax2percent / 100;
- break;
- case 2:
- $node->tax2 = $node->subtotal * $node->tax2percent / 100;
- break;
- default:
- // ERROR
- drupal_set_message(t('Error during tax calculations (Storm module)'), 'error');
- }
-
- $node->total = $node->subtotal + $node->tax2;
- }
-
- // STORM ICON ADD / EDIT / LINK FUNCTIONS
- function storm_icon_add_node($node, $params=array()) {
- return storm_icon_add('node/add/'. $node->type, $node, $params);
- }
-
- function storm_icon_edit_node($node, $params=array()) {
- return storm_icon_edit('node/'. $node->nid .'/edit', $node, $params);
- }
-
- function storm_icon_delete_node($node, $params=array()) {
- return storm_icon_delete('node/'. $node->nid .'/delete', $node, $params);
- }
-
- function storm_icon_add($path, $item, $params=array()) {
- global $user;
- if (!node_access('create', $item, $user)) return '';
- $attributes = array('class' => 'popups-form');
- return storm_icon_l('application_add', $path, t('Add'), '', $params, $attributes);
- }
-
- function storm_icon_edit($path, $item, $params=array()) {
- global $user;
- if (!node_access('update', $item, $user)) return '';
- $attributes = array('class' => 'popups-form');
- return storm_icon_l('application_edit', $path, t('Edit'), '', $params, $attributes);
- }
-
- function storm_icon_delete($path, $item, $params=array()) {
- global $user;
- if (!node_access('delete', $item, $user)) return '';
- $attributes = array('class' => 'popups-form');
- return storm_icon_l('application_delete', $path, t('Delete'), '', $params, $attributes);
- }
-
- function storm_icon_l($icon, $path, $title, $permission='', $params=array(), $attributes=array()) {
- if ($permission && !user_access($permission)) return '';
- $icon = storm_icon($icon, $title);
- $attributes ['title'] = $title;
-
- $query = '';
-
- if (array_key_exists('q', $params)) {
- $destination = $params['q'];
- unset($params['q']);
- $c = 0;
- if (array_key_exists('page', $params)) {
- $destination .= '?page='. $params['page'];
- unset($params['page']);
- $c++;
- }
- if (array_key_exists('sort', $params)) {
- if ($c) {
- $destination .= '&';
- }
- else {
- $destination .= '?';
- }
- $destination .= 'sort='. $params['sort'];
- unset($params['sort']);
- $c++;
- }
- if (array_key_exists('order', $params)) {
- if ($c) {
- $destination .= '&';
- }
- else {
- $destination .= '?';
- }
- $destination .= 'order='. $params['order'];
- unset($params['order']);
- $c++;
- }
- $query .= 'destination='. urlencode($destination);
- unset($params['destination']);
- }
-
- $params = _storm_icon_urlencode_helper($params);
- foreach ($params as $key => $value) {
- if ($query) $query .= '&';
- $query .= $key .'='. urlencode($value);
- }
-
- $o = l($icon, $path, array('attributes' => $attributes, 'query' => $query, 'html' => TRUE));
- return $o;
- }
-
- function _storm_icon_urlencode_helper($params, $org_key = "") {
- $new_params = array();
- foreach ($params as $key => $value) {
- if (!empty($org_key)) {
- $new_key = $org_key ."[". $key ."]";
- }
- else {
- $new_key = $key;
- }
- if (is_array($value)) {
- $new_params = array_merge(_storm_icon_urlencode_helper($value, $new_key), $new_params);
- }
- else {
- $new_params[$new_key] = $value;
- }
- }
- return $new_params;
- }
-
- function storm_icon($icon, $title) {
- global $base_path;
-
- // Running checkplain on these variables means that we can call storm_icon without further sanitising
- $icon = check_plain($icon);
- $title = check_plain($title);
-
- $icon = str_replace(' ', '_', $icon);
-
- if (variable_get('storm_icons_display', TRUE) == TRUE) {
- $available = cache_get('storm:icons');
- if (!is_object($available)) {
- // Cache miss
- $available = storm_icon_recache();
- }
-
- if (in_array($icon .'.png', $available->data)) {
- // Standard route - display normal image
- $img_src = $base_path . variable_get('storm_icons_path', drupal_get_path('module', 'storm') .'/icons') .'/'. $icon .'.png';
- $o = '<img src="'. $img_src .'" alt="'. $title .'" title="'. $title .'" />';
- }
- else {
- // Icon not found
- $o = storm_icon_default($icon, $title);
- }
- }
- else {
- // Icons set to not display
- $o = $title;
- }
- return $o;
- }
-
- function storm_icon_recache() {
- $available = array();
-
- // For PHP5, replace with scandir() function
- $dir = variable_get('storm_icons_path', drupal_get_path('module', 'storm') . '/icons');
-
- $files = file_scan_directory($dir, '.*');
- foreach ($files as $file) {
- $available[] = $file->basename;
- }
-
- cache_set('storm:icons', $available, 'cache', CACHE_TEMPORARY);
- $available = cache_get('storm:icons');
- return $available;
- }
-
- function storm_icon_default($icon, $title) {
- // For now, just return $title.
- // A future extension could be more intelligent using $icon.
- return $title;
- }
-
-
- // SQL FUNCTIONS
- function storm_rewrite_sql($sql, $where=array(), $join=array()) {
- $where = empty($where) ? '' : '('. implode(') AND (', $where) .')';
- $join = empty($join) ? '' : implode(' ', $join);
-
- if (!empty($where) || !empty($join)) {
- if (!empty($where)) {
- $new = "WHERE $where ";
- }
- $new = " $join $new";
- if (strpos($sql, 'WHERE')) {
- $sql = str_replace('WHERE', $new .'AND (', $sql);
- $insert = ') ';
- }
- else {
- $insert = $new;
- }
- if (strpos($sql, 'GROUP')) {
- $replace = 'GROUP';
- }
- elseif (strpos($sql, 'HAVING')) {
- $replace = 'HAVING';
- }
- elseif (strpos($sql, 'ORDER')) {
- $replace = 'ORDER';
- }
- elseif (strpos($sql, 'LIMIT')) {
- $replace = 'LIMIT';
- }
- else {
- $sql .= $insert;
- }
- if (isset($replace)) {
- $sql = str_replace($replace, $insert . $replace, $sql);
- }
- }
-
- return $sql;
- }
-
- function storm_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
- if (($primary_table == 'n' || $primary_table == 'node') && $primary_field == 'nid') {
-
- if (preg_match("/'storm_access'='storm_access'/", $query)) {
- return array();
- }
-
- global $user;
- $conditions = array();
- foreach (module_invoke_all('storm_rewrite_where_sql', $query, $primary_table, $user) as $condition) {
- if ($condition) {
- $conditions[] = $condition;
- }
- }
-
- $return = array();
- $where = '';
- if ($conditions) {
- switch ($GLOBALS['db_type']) {
- case 'mysql':
- case 'mysqli':
- $where = '(';
- $where .= " CASE ${primary_table}.type ";
- foreach ($conditions as $condition) {
- $where .= $condition .' ';
- }
- $where .= ' ELSE 1 END ';
- $where .= ' )=1 ';
- $return['where'] = $where;
- break;
- case 'pgsql':
- break;
- }
- }
- return $return;
- }
- }
-
- // FUNCTION TO ADD STORM CLASSES TO NODE FORM
- function storm_form_alter(&$form, $form_state, $form_id) {
- if (!isset($form['type'])) {
- return;
- }
-
- if ($form_id == $form['type']['#value'] .'_node_form') {
- $class = NULL;
- if (isset($form['#attributes']['class'])) $class = $form['#attributes']['class'];
- if ($class) $class .= ' ';
- $class .= $form_id;
- $form['#attributes']['class'] = $class;
- foreach ($form as $key => $elem) {
-
- if (is_array($elem) && (isset($elem['#type']) ? $elem['#type'] : NULL)=='fieldset') {
- $class = (isset($elem['#attributes']['class'])) ? $elem['#attributes']['class'] : NULL;
- if ($class) $class .= ' ';
- $class .= $key;
- $form[$key]['#attributes']['class'] = $class;
- }
- }
- }
- }
-
- /**
- * Get a list of people and teams for select boxes
- *
- * Params:
- * $organization_nid
- * Leave blank to get a list of all teams and persons, otherwise also provide
- * $project_nid
- * to get a limited list of teams and persons following the following logic:
- * - If the project is assigned to a person, only that person is listed as an option
- * - If the project is assigned to a team, all team members are listed as options
- * - If the project is neither assigned to a person nor a team, all people that are
- * assigned to the given origanization are listed as options
- * - In addition, if the project is assigned to a manager, that person is also listed
- * - Finally, look into all existing teams and list those teams that exclusively
- * contain members that are already selected
- * If $organization_nid is provided but $project_nid is omitted, then the logic is as
- * above, just for all projects of the given organization.
- */
- function storm_get_assignment_options($organization_nid = 0, $project_nid = 0) {
- $teams = t('Teams:');
- $people = t('People:');
- $options = array();
- if (!$organization_nid) {
- $options['all'] = t('- no filter -');
- $options['mine'] = t('- mine -');
- }
- $options['none'] = t('- unassigned -');
- if (module_exists('stormteam')) {
- $options[$teams] = array();
- }
- if (module_exists('stormperson')) {
- $options[$people] = array();
- }
- $add_org_people = TRUE;
- if ($organization_nid) {
- $add_org_people = FALSE;
- $manager = array();
- $projects = array();
- if ($project_nid) {
- $projects[] = $project_nid;
- }
- else {
- $sql = "SELECT n.nid FROM {node} AS n
- INNER JOIN {stormproject} AS spr
- ON n.vid=spr.vid
- WHERE n.status=1
- AND n.type='stormproject'
- AND spr.organization_nid=%d";
- $sql = stormproject_access_sql($sql);
- $result = db_query(db_rewrite_sql($sql), $organization_nid);
- while ($project = db_fetch_object($result)) {
- $projects[] = $project->nid;
- }
- $add_org_people = TRUE;
- }
- foreach ($projects as $pid) {
- $project = node_load($pid);
- if ($project->manager_nid) {
- $manager[] = node_load($project->manager_nid);
- }
- if ($project->assigned_nid) {
- $node = node_load($project->assigned_nid);
- if ($node->type == 'stormperson') {
- if (module_exists('stormperson')) {
- $options[$people][$node->nid] = check_plain($node->title);
- }
- }
- else { // ($node->type == 'stormteam')
- if (module_exists('stormteam')) {
- $options[$teams][$node->nid] = check_plain($node->title);
- foreach ($node->members_array as $nid => $name) {
- // do not add deactivated persons
- if (!array_key_exists($nid, $node->members_deactivated_array)) {
- $options[$people][$nid] = check_plain($name);
- }
- }
- }
- }
- }
- else {
- $add_org_people = TRUE;
- }
- }
- }
- if ($add_org_people) {
- if (module_exists('stormperson')) {
- $where = (isset($organization_nid) && $organization_nid != 0) ? 'WHERE spe.organization_nid = %d' : '';
- $sql = "SELECT spe.nid,
- spe.fullname,
- n.title
- FROM {node} n
- INNER JOIN {stormperson} spe
- ON n.vid = spe.vid
- $where
- ORDER BY spe.fullname ASC";
- $sql = stormperson_access_sql($sql);
- $sql = db_rewrite_sql($sql);
- $result = db_query($sql, array($organization_nid));
- while ($person = db_fetch_object($result)) {
- $options[$people][$person->nid] = ($person->fullname) ? check_plain($person->fullname) : check_plain($person->title);
- if (empty($options[$people][$person->nid])) {
- $options[$people][$person->nid] = t('Person !nid', array('!nid' => $person->nid));
- }
- }
- }
- }
- else {
- if (isset($manager) && module_exists('stormperson')) {
- foreach ($manager as $manager_node) {
- if (!array_key_exists($manager_node->nid, $options[$people])) {
- $options[$people][$manager_node->nid] = check_plain($manager_node->title);
- }
- }
- }
- }
- if (module_exists('stormteam')) {
- $where = array();
- $args = array();
- $having = "";
- // do not add teams, which we already added
- if (!empty($options[$teams])) {
- $where[] = "ste.nid NOT IN (". db_placeholders($options[$teams]) .")";
- $args = array_merge($args, array_keys($options[$teams]));
- }
- // only add teams, which have at least the same members as persons we added so far
- if (!empty($organization_nid) && !empty($options[$people])) {
- // add all persons which should be in the team
- $where[] = "ste.mnid IN (". db_placeholders($options[$people]) .")";
- $args = array_merge($args, array_keys($options[$people]));
- // add a count, how many of the given persons should be in the other teams
- $having = " HAVING COUNT(ste.nid) = %d ";
- $args[] = count($options[$people]);
- }
- $sql = "SELECT n.nid, n.vid, n.title
- FROM {node} n
- INNER JOIN {stormteam} ste
- ON n.vid = ste.vid
- WHERE n.type = 'stormteam'
- GROUP BY ste.nid". $having ."
- ORDER BY n.title ASC";
- $sql = stormteam_access_sql($sql, $where);
- $sql = db_rewrite_sql($sql);
- $result = db_query($sql, $args);
- while ($team = db_fetch_object($result)) {
- $options[$teams][$team->nid] = check_plain($team->title);
- }
- }
- if (isset($options[$people]) && array_key_exists(0, $options[$people])) {
- unset($options[$people][0]);
- }
- if (isset($options[$teams]) && array_key_exists(0, $options[$teams])) {
- unset($options[$teams][0]);
- }
- if (isset($options[$people]) && !sizeof($options[$people])) {
- unset($options[$people]);
- }
- if (isset($options[$teams]) && !sizeof($options[$teams])) {
- unset($options[$teams]);
- }
-
- // SORT OPTIONS
- if (!empty($options[$people]) && is_array($options[$people])) {
- asort($options[$people], SORT_LOCALE_STRING);
- }
- if (!empty($options[$teams]) && is_array($options[$teams])) {
- asort($options[$teams], SORT_LOCALE_STRING);
- }
-
- return $options;
- }
-
- /**
- * Helper function to count nodes of type destination module with parent nid of type source module.
- *
- * @param string $source_module A string of module where the request is comming from
- * @param string $destination_module A string with name of targeted module
- * @param int $nid node id of source module
- * @return ''|int
- * empty string if can't be counted or the count of destination nodes in source node
- * @see theme_storm_link()
- */
- function _storm_number_of_items($source_module, $destination_module, $nid) {
- global $user;
- $nmb_of_items = '';
-
- $valid_destination_modules = array();
- switch ($source_module) {
- case "stormorganization":
- $column_name = 'organization_nid';
- $valid_destination_modules = array('stormproject', 'stormtask', 'stormticket', 'stormtimetracking', 'stormnote', 'stormperson', 'stormexpense', 'storminvoice');
- break;
-
- case "stormproject":
- $column_name = 'project_nid';
- $valid_destination_modules = array('stormtask', 'stormticket', 'stormtimetracking', 'stormnote', 'stormexpense', 'storminvoice');
- break;
-
- case "stormtask":
- $column_name = 'task_nid';
- $valid_destination_modules = array('stormticket', 'stormtimetracking', 'stormnote', 'stormexpense');
- break;
-
- case "stormticket":
- $column_name = 'ticket_nid';
- $valid_destination_modules = array('stormtimetracking', 'stormexpense');
- break;
- }
-
- if (in_array($destination_module, $valid_destination_modules)) {
- switch ($destination_module) {
- case 'stormproject':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {stormproject} AS spr ON n.vid=spr.vid WHERE n.status=1 AND
- n.type='stormproject' and spr.". $column_name ." = %d";
- $sql = stormproject_access_sql($sql);
- break;
- case 'stormtask':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {stormtask} AS sta ON n.vid=sta.vid WHERE n.status=1 AND
- n.type='stormtask' and sta.". $column_name ." = %d";
- $sql = stormtask_access_sql($sql);
- break;
- case 'stormticket':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {stormticket} AS sti ON n.vid=sti.vid WHERE n.status=1 AND
- n.type='stormticket' and sti.". $column_name ." = %d";
- $sql = stormticket_access_sql($sql);
- break;
- case 'stormtimetracking':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {stormtimetracking} AS stt ON n.vid=stt.vid WHERE n.status=1 AND
- n.type='stormtimetracking' and stt.". $column_name ." = %d";
- $sql = stormtimetracking_access_sql($sql);
- break;
- case 'stormperson':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {stormperson} AS spe ON n.vid=spe.vid WHERE n.status=1 AND
- n.type='stormperson' and spe.". $column_name ." = %d";
- $sql = stormperson_access_sql($sql);
- break;
- case 'stormnote':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {stormnote} AS sno ON n.vid=sno.vid WHERE n.status=1 AND
- n.type='stormnote' and sno.". $column_name ." = %d";
- $sql = stormnote_access_sql($sql);
- break;
- case 'stormexpense':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {stormexpense} AS sex ON n.vid=sex.vid WHERE n.status=1 AND
- n.type='stormexpense' and sex.". $column_name ." = %d";
- $sql = stormexpense_access_sql($sql);
- break;
- case 'storminvoice':
- $sql = "SELECT COUNT(*) FROM {node} AS n INNER JOIN {storminvoice} AS sin ON n.vid=sin.vid WHERE n.status=1 AND
- n.type='storminvoice' and sin.". $column_name ." = %d";
- $sql = storminvoice_access_sql($sql);
- break;
-
- }
- $sql = db_rewrite_sql($sql);
- $nmb_of_items = db_result(db_query($sql, $nid));
- }
- return $nmb_of_items;
- }
-
- /**
- * Implements hook_views_pre_render to add the storm_icon_add
- *
- * @param $view
- */
- function storm_views_pre_render(&$view) {
- if (isset($view->field['operation'])
- && $view->field['operation'] instanceof storm_handler_field_operation
- && isset($view->field['operation']->definition['type'])
- && $view->field['operation']->options['display_add_icon']
- ) {
- $i = new stdClass();
- $i->type = $view->field['operation']->definition['type'];
- if ($view->field['operation']->options['display_icons']) {
- $view->attachment_before .= '<span class="storm-icon-add-view">'. storm_icon_add_node($i, $_GET) .'</span>';
- }
- else {
- global $user;
- $type = $i->type;
- $af = $type .'_access';
- if ($af('create', $i, $user)) {
- $view->attachment_before .= '<span class="storm-icon-add-view">'. l(t('Add'), 'node/add/'. $type) .'</span>';
- }
- }
- }
- }
-
- function storm_attribute_access($op, $item=NULL, $account=NULL) {
- if (empty($account)) {
- global $user;
- $account = $user;
- }
-
- if ($op == 'create') {
- return user_access('Storm: access administration pages');
- }
-
- if ($op == 'delete') {
- return user_access('Storm: access administration pages');
- }
-
- if ($op == 'update') {
- return user_access('Storm: access administration pages');
- }
-
- return FALSE;
- }
-
- function storm_attributes_bydomain($domain) {
- static $attributes_cache = array();
- $attributes = array();
-
- if (array_key_exists($domain, $attributes_cache)) return $attributes_cache[$domain];
-
- $s = "SELECT * FROM {stormattribute} WHERE LOWER(domain) LIKE LOWER('%s') AND isactive=1 ORDER BY weight, avalue";
- $r = db_query($s, $domain);
- $attributes['values'] = array();
- while ($attribute = db_fetch_object($r)) {
- // The variable is deliberately passed through t() for translatability
- $attributes['values'][$attribute->akey] = t($attribute->avalue);
- if ($attribute->isdefault) {
- $attributes['default'] = $attribute->akey;
- }
- }
- if (is_array($attributes['values']) && !array_key_exists('default', $attributes)) {
- $v = array_flip($attributes['values']);
- $attributes['default'] = array_shift($v);
- }
-
- $attributes_cache[$domain] = $attributes;
- return $attributes;
- }
-
-
- function storm_attribute_value($domain, $key) {
- $attributes_array = storm_attributes_bydomain($domain);
- $attributes = $attributes_array['values'];
- if (array_key_exists($key, $attributes)) {
- return $attributes[$key];
- }
- return $key;
- }
-