You are here

function flag_lists_add_js in Flag Lists 7

Same name and namespace in other branches
  1. 7.3 flag_lists.admin.inc \flag_lists_add_js()

Callback for adding new lists through AJAX.

1 string reference to 'flag_lists_add_js'
flag_lists_menu in ./flag_lists.module
Implementation of hook_menu().

File

./flag_lists.admin.inc, line 86
Contains administrative pages for creating, editing, and deleting flag lists.

Code

function flag_lists_add_js($type = NULL) {
  if (is_null($type)) {
    drupal_json_output(array(
      'error' => t('Type not supplied.'),
    ));
    exit;
  }
  $name = check_plain($_REQUEST['name']);
  if (!isset($name) || empty($name)) {
    drupal_json_output(array(
      'error' => t('List name not supplied.'),
    ));
    exit;
  }
  if (drupal_strlen($name) > 255) {
    drupal_json_output(array(
      'error' => t('List name is too long. Maximum is 255 characters.'),
    ));
    exit;
  }
  if (flag_lists_title_exists($name, $type)) {
    drupal_json_output(array(
      'error' => t('You already have a @name with this name for this type of content.', array(
        '@name' => variable_get('flag_lists_name', t('list')),
      )),
    ));
    exit;
  }
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }

  // New flag. Load the template row.
  $query = db_select('flags', 'f');
  $query
    ->leftJoin('flag_lists_types', 'fl', 'f.name = fl.name');
  $query
    ->fields('f')
    ->fields('fl')
    ->condition('fl.type', $type);
  $row = $query
    ->execute()
    ->fetchObject();
  $newflag = flag_flag::factory_by_content_type('node');
  $flag = $newflag
    ->factory_by_row($row);

  // The template fid becomes the flag_lists parent flag.
  $flag->pfid = $row->fid;

  // The fid is NULL because this is really a new flag.
  $flag->fid = NULL;

  // The name is created in the save function.
  $flag->name = NULL;
  $flag->link_type = 'toggle';
  $flag->title = $name;
  $flag->types = array(
    $type,
  );
  $flag->uid = $account->uid;
  flag_lists_set_messages($flag);

  // Save it.
  flag_lists_save($flag, $account);
  drupal_json_output(array(
    'error' => '',
    'flag' => $flag,
  ));
}