<?php

/**
 * @file
 */

function stormidea_help($path, $arg) {
  $o = '';

  switch ($path) {
    case "admin/help#stormidea":
      $o = '<p>'. t("Provides quick notes support for SuiteDesk") .'</p>';
      break;
  }

  return $o;
}

function stormidea_perm() {
  return array(
    'Storm idea: access',
    'Storm idea: add',
    'Storm idea: delete all',
    'Storm idea: delete own',
    'Storm idea: edit all',
    'Storm idea: edit own',
    'Storm idea: view all',
    'Storm idea: view own',
  );
}

function stormidea_access($op, $node, $account = NULL) {
  if (empty($account)) {
    global $user;
    $account = $user;
  }

  if ($op == 'create') {
    return user_access('Storm idea: add');
  }

  if (is_numeric($node)) {
    $node = node_load($node);
  }

  if ($op == 'delete') {
    if (user_access('Storm idea: delete all')) {
      return TRUE;
    }
    elseif (user_access('Storm idea: delete own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
  }

  if ($op == 'update') {
    if (user_access('Storm idea: edit all')) {
      return TRUE;
    }
    elseif (user_access('Storm idea: edit own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
  }

  if ($op == 'view') {
    if (user_access('Storm idea: view all')) {
      return TRUE;
    }
    elseif (user_access('Storm idea: view own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
  }

  return FALSE;
}

function stormidea_access_sql($sql, $where = array()) {
  if (!user_access('Storm idea: view all')) {
    global $user;

    $cond = '';
    if (user_access('Storm idea: view own')) {
      $cond .= 'n.uid = ' . $user->uid;
    }
    $where[] = empty($cond) ? '0 = 1' : $cond;
  }

  $where[] = "'storm_access' = 'storm_access'";
  return storm_rewrite_sql($sql, $where);
}

function stormidea_storm_rewrite_where_sql_old($query, $primary_table, $account) {
  static $conds = array();

  if (isset($conds[$primary_table][$account->uid])) {
    return $conds[$primary_table][$account->uid];
  }

  $cond = '';
  if (!preg_match("/'storm_access' = 'storm_access'/", $query)) {
    if (user_access('Storm idea: view all', $account)) {
      return '';
    }
    if (user_access('Storm idea: view own', $account)) {
      $cond .= "${primary_table}.uid = " . $account->uid;
    }
    if ($cond) {
      $cond = " WHEN 'stormidea' THEN (SELECT IF($cond, 1, 0) FROM {stormidea} sno1 WHERE sno1.vid = ${primary_table}.vid) ";
    }
    else {
      $cond = " WHEN 'stormidea' THEN 0 ";
    }
  }
  $conds[$primary_table][$account->uid] = $cond;
  return $cond;
}

function stormidea_menu() {
  $items = array();

  $items['ideas'] = array(
    'title' => 'My ideas',
    'description' => 'SuiteDesk ideas',
    'page callback' => 'stormidea_list',
    'access arguments' => array('Storm idea: access'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'stormidea.admin.inc',
    'weight' => 7,
  );

  return $items;
}

function stormidea_theme() {
  return array(
    'stormidea_list' => array(
      'file'      => 'stormidea.theme.inc',
      'arguments' => array('header', 'ideas'),
    ),
    'stormidea_view' => array(
      'file'      => 'stormidea.theme.inc',
      'arguments' => array('node', 'teaser', 'page'),
    ),
  );
}

function stormidea_node_info() {
  return array(
    'stormidea' => array(
      'name' => t('Idea'),
      'module' => 'stormidea',
      'description' => t("A quick note for SuiteDesk."),
      'has_body' => TRUE,
    )
  );
}

function stormidea_form(&$node) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('SuiteDesk'), 'dashboard');
  $breadcrumb[] = l(t('My ideas'), 'ideas');
  drupal_set_breadcrumb($breadcrumb);

  $type = node_get_types('type', $node);

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'title') : -16,
  );

  return $form;
}

function stormidea_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node']) && $form['#node']->type == 'stormidea') {
    if (isset($form['type']['#value']) && $form['type']['#value'] .'_node_form' == $form_id) {
        $form['buttons']['preview']['#access'] = FALSE;
    }
  }
}

function stormidea_nodeapi($node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type == 'stormidea' && $op == 'insert') {
    $_REQUEST['destination'] = $_GET['q'];
  }
}

function stormidea_view($node, $teaser = FALSE, $page = FALSE) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('SuiteDesk'), 'dashboard');
  $breadcrumb[] = l(t('My ideas'), 'ideas');
  drupal_set_breadcrumb($breadcrumb);

  return theme('stormidea_view', $node, $teaser, $page);
}

function stormidea_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'stormidea'),
  );
}

function stormidea_storm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'storm_dashboard_link',
      'title' => t('My ideas'),
      'icon' => 'stormidea-item',
      'path' => 'ideas',
      'params' => array(),
      'access_arguments' => 'Storm idea: access',
      'node_type' => 'stormidea',
      'add_type' => 'stormidea',
      'map' => array(),
      'weight' => 9,
    );
  }
  return $links;
}
