You are here

function flagging_save in Flag 7.3

Saves a flagging entity.

For a new flagging, throws an exception is the flag action is not allowed for the given combination of flag, entity, and user.

Parameters

$flagging: The flagging entity. This may have either flag_name or the flag fid set, and may also omit the uid property to use the current user.

Throws

Exception

3 calls to flagging_save()
FlagFlaggingCRUDTestCase::testFlaggingCreate in tests/flag.test
Test creation of a flagging entity with flagging_save().
FlagFlaggingCRUDTestCase::testFlaggingCreateException in tests/flag.test
Test throwing of exceptions with flagging_save().
FlagFlaggingCRUDTestCase::testFlaggingUpdate in tests/flag.test
Test creation of a flagging entity with flagging_save().
1 string reference to 'flagging_save'
flag_entity_info in ./flag.module
Implements hook_entity_info().

File

./flag.module, line 124
The Flag module.

Code

function flagging_save($flagging) {

  // Get the flag, either way.
  if (isset($flagging->flag_name)) {
    $flag = flag_get_flag($flagging->flag_name);
  }
  else {
    $flag = flag_get_flag(NULL, $flagging->fid);
  }
  if (!$flag) {
    throw new Exception('Flag not found for flagging entity.');
  }

  // Fill in properties that may be omitted.
  $flagging->fid = $flag->fid;
  $flagging->flag_name = $flag->name;
  if (!empty($flagging->uid)) {
    $account = user_load($flagging->uid);
  }
  else {
    $account = NULL;
  }
  $result = $flag
    ->flag('flag', $flagging->entity_id, $account, FALSE, $flagging);
  if (!$result) {
    throw new Exception('Flag action not allowed for given flagging entity properties.');
  }
}