class flag_comment in Flag 7.3
Same name and namespace in other branches
- 5 flag.inc \flag_comment
- 6.2 flag.inc \flag_comment
- 6 flag.inc \flag_comment
- 7.2 flag.inc \flag_comment
Implements a comment flag.
Hierarchy
- class \flag_flag
- class \flag_entity
- class \flag_comment
- class \flag_entity
Expanded class hierarchy of flag_comment
1 string reference to 'flag_comment'
- flag_flag_type_info in ./
flag.flag.inc - Implements hook_flag_type_info().
File
- includes/
flag/ flag_comment.inc, line 11 - Contains the flag_comment class.
View source
class flag_comment extends flag_entity {
function options() {
$options = parent::options();
$options += array(
'access_author' => '',
);
return $options;
}
/**
* Options form extras for comment flags.
*/
function options_form(&$form) {
parent::options_form($form);
$form['access']['access_author'] = array(
'#type' => 'radios',
'#title' => t('Flag access by content authorship'),
'#options' => array(
'' => t('No additional restrictions'),
'comment_own' => t('Users may only flag own comments'),
'comment_others' => t('Users may only flag comments by others'),
'node_own' => t('Users may only flag comments of nodes they own'),
'node_others' => t('Users may only flag comments of nodes by others'),
),
'#default_value' => $this->access_author,
'#description' => t("Restrict access to this flag based on the user's ownership of the content. Users must also have access to the flag through the role settings."),
);
}
function type_access_multiple($entity_ids, $account) {
$access = array();
// If all subtypes are allowed, we have nothing to say here.
if (empty($this->types)) {
return $access;
}
// Ensure node types are granted access. This avoids a
// node_load() on every type, usually done by applies_to_entity_id().
$query = db_select('comment', 'c');
$query
->innerJoin('node', 'n', 'c.nid = n.nid');
$result = $query
->fields('c', array(
'cid',
))
->condition('c.cid', $entity_ids, 'IN')
->condition('n.type', $this->types, 'NOT IN')
->execute();
foreach ($result as $row) {
$access[$row->nid] = FALSE;
}
return $access;
}
function get_entity_id($comment) {
// Store the comment object in the static cache, to avoid getting it
// again unneedlessly.
$this
->remember_entity($comment->cid, $comment);
return $comment->cid;
}
/**
* Overrides flag_flag::get_flagging_record().
*
* This queries for flagging records for all comments on the node for the
* current comment, and prefills the flag_get_user_flags() static cache with
* the result for a performance gain.
*/
function get_flagging_record($entity_id, $uid = NULL, $sid = NULL) {
static $seen_comment_nids = array();
$comment = $this
->fetch_entity($entity_id);
// Figure out if this is the first comment we've seen for its parent node.
if (!isset($seen_comment_nids[$comment->nid])) {
// Preload the flag_get_user_flags() static cache with flagging records
// for all the comments on the node. This means that if multiple comments
// on this node are being viewed, only one query is run for all their
// flagging records, rather than one for each comment. This is because
// flag_get_user_flags() can only optimized across flags on one entity.
$flag_get_user_flags_cache =& drupal_static('flag_get_user_flags');
// We need to get a row for each comment, including empty ones if there is
// no flagging, so that the cache is warmed up for all comments. Therefore
// the query has to have the {comment} table as its base, and include the
// comment cid field.
$query = db_select('comment', 'c');
$query
->leftJoin('flagging', 'f', "c.cid = f.entity_id AND f.entity_type = 'comment'");
$query
->fields('f')
->fields('c', array(
'cid',
))
->condition('c.nid', $comment->nid)
->condition(db_or()
->isNull('f.flagging_id')
->condition(db_and()
->condition(db_or()
->condition('f.uid', $uid)
->condition('f.uid', 0))
->condition('f.sid', $sid)));
// The result set can have multiple rows for a single comment, and rows
// which have no flagging ID, so there's nothing useful to index it by.
$result = $query
->execute()
->fetchAll();
$flag_names = _flag_get_flag_names();
foreach ($result as $flagging_data) {
$cid = $flagging_data->cid;
// At the very least, we need an empty array for the entity ID key in
// the cache array, so it counts as present.
if (!isset($flag_get_user_flags_cache[$uid][$sid]['comment'][$cid])) {
$flag_get_user_flags_cache[$uid][$sid]['comment'][$cid] = array();
}
// If the flagging table gave us no data, we're done with this row.
if (is_null($flagging_data->flagging_id)) {
continue;
}
// Remove the comment ID field from the row, so it's just the flagging
// table row.
unset($flagging_data->cid);
$flag_get_user_flags_cache[$uid][$sid]['comment'][$cid][$flag_names[$flagging_data->fid]] = $flagging_data;
}
// Mark that we've seen this node so we don't process it again.
$seen_comment_nids[$comment->nid] = TRUE;
}
// Return data for the comment we were asked about.
$user_flags = flag_get_user_flags($this->entity_type, $entity_id, $uid, $sid);
return isset($user_flags[$this->name]) ? $user_flags[$this->name] : NULL;
}
function get_labels_token_types() {
return array_merge(array(
'comment',
'node',
), parent::get_labels_token_types());
}
function replace_tokens($label, $contexts, $options, $entity_id) {
if ($entity_id) {
if (($comment = $this
->fetch_entity($entity_id)) && ($node = node_load($comment->nid))) {
$contexts['node'] = $node;
$contexts['comment'] = $comment;
}
}
return parent::replace_tokens($label, $contexts, $options, $entity_id);
}
function get_flag_action($entity_id) {
$flag_action = parent::get_flag_action($entity_id);
$comment = $this
->fetch_entity($entity_id);
$flag_action->content_title = $comment->subject;
$flag_action->content_url = $this
->_flag_url("comment/{$comment->cid}", "comment-{$comment->cid}");
return $flag_action;
}
function get_relevant_action_objects($entity_id) {
$comment = $this
->fetch_entity($entity_id);
return array(
'comment' => $comment,
'node' => node_load($comment->nid),
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
flag_comment:: |
function |
Returns the entity id, if it already exists. Overrides flag_entity:: |
||
flag_comment:: |
function |
Overrides flag_flag::get_flagging_record(). Overrides flag_flag:: |
||
flag_comment:: |
function |
Returns a 'flag action' object. Overrides flag_entity:: |
||
flag_comment:: |
function |
Returns token types for the current entity type. Overrides flag_entity:: |
||
flag_comment:: |
function |
Returns objects the action may possible need. Overrides flag_entity:: |
||
flag_comment:: |
function |
Adds additional options that are common for all entity types. Overrides flag_entity:: |
||
flag_comment:: |
function |
Options form extras for comment flags. Overrides flag_entity:: |
||
flag_comment:: |
function |
Replaces tokens. Overrides flag_entity:: |
||
flag_comment:: |
function |
Implements access_multiple() implemented by each child class. Overrides flag_flag:: |
||
flag_entity:: |
function |
Checks whether the flag applies for the current entity bundle. Overrides flag_flag:: |
||
flag_entity:: |
function |
Provides permissions for this flag. Overrides flag_flag:: |
||
flag_entity:: |
function |
Returns information for the Views integration. Overrides flag_flag:: |
1 | |
flag_entity:: |
protected | function |
Invoke a Rules event in reaction to a flagging or unflagging. Overrides flag_flag:: |
|
flag_entity:: |
function |
Determine whether the flag should show a flag link in entity links. Overrides flag_flag:: |
||
flag_entity:: |
function |
Loads the entity object. Overrides flag_flag:: |
||
flag_flag:: |
property | The entity type this flag works with. | ||
flag_flag:: |
public | property | An associative array containing textual errors that may be created during validation. | |
flag_flag:: |
property | The database ID. | ||
flag_flag:: |
property | Whether this flag state should act as a single toggle to all users. | ||
flag_flag:: |
property | The flag's "machine readable" name. | ||
flag_flag:: |
property | The roles array. This can be populated by fetch_roles() when needed. | ||
flag_flag:: |
property | The human-readable title for this flag. | ||
flag_flag:: |
property | The sub-types, AKA bundles, this flag applies to. | ||
flag_flag:: |
function | Determines whether the user may flag, or unflag, the given entity. | ||
flag_flag:: |
function | Utility function: Checks whether a flag applies to a certain type, and possibly subtype, of entity. | ||
flag_flag:: |
function | Determine access to multiple objects. | ||
flag_flag:: |
function | Returns administrative menu path for carrying out some action. | ||
flag_flag:: |
function | Returns TRUE if the flag applies to the entity with the given ID. | ||
flag_flag:: |
function | Default constructor. Loads the default options. | ||
flag_flag:: |
function | Deletes a flag from the database. | ||
flag_flag:: |
function | Disable a flag provided by a module. | ||
flag_flag:: |
function | Enable a flag provided by a module. | ||
flag_flag:: |
static | function | Create a complete flag (except an FID) from an array definition. | |
flag_flag:: |
static | function | Another factory method. Returns a new, "empty" flag; e.g., one suitable for the "Add new flag" page. | |
flag_flag:: |
static | function | Creates a flag from a database row. Returns it. | |
flag_flag:: |
function | Fetches, possibly from some cache, an entity this flag works with. | ||
flag_flag:: |
function | Load this flag's role data from permissions. | ||
flag_flag:: |
function | Finds the "default flag" corresponding to this flag. | ||
flag_flag:: |
function | Flags, or unflags, an item. | 1 | |
flag_flag:: |
private | function | Unflag an entity by deleting a Flagging. | |
flag_flag:: |
private | function | Create a new Flagging to flag an entity. | |
flag_flag:: |
private | function | Update a Flagging. | |
flag_flag:: |
function | Update the flag with settings entered in a form. | 1 | |
flag_flag:: |
function | Returns the number of times an item is flagged. | ||
flag_flag:: |
function | Returns an array of errors set during validation. | ||
flag_flag:: |
function | Similar to is_flagged() excepts it returns the flagging entity. | ||
flag_flag:: |
function | Processes a flag label for display. This means language translation and token replacements. | ||
flag_flag:: |
function | Get the link type for this flag. | ||
flag_flag:: |
function | Options are stored serialized in the database. | ||
flag_flag:: |
function | A convenience method for getting the flag title. | ||
flag_flag:: |
function | Returns the number of items a user has flagged. | ||
flag_flag:: |
function | Returns an array of all actions that are executable with this flag. | ||
flag_flag:: |
function | Saves a new flag to the database. Better use save(). | ||
flag_flag:: |
function | Returns TRUE if this flag's declared API version is compatible with this module. | ||
flag_flag:: |
function | Determines if a certain user has flagged this object. | ||
flag_flag:: |
function | Construct a new, empty flagging entity object. | ||
flag_flag:: |
function | Store an object in the flag handler's cache. | ||
flag_flag:: |
function | Reverts an overriding flag to its default state. | ||
flag_flag:: |
function | Saves a flag to the database. It is a wrapper around update() and insert(). | ||
flag_flag:: |
function | Renders a flag/unflag link. | ||
flag_flag:: |
function | Provides an array of possible themes to try for a given flag. | ||
flag_flag:: |
function | Implements access() implemented by each child class. | 1 | |
flag_flag:: |
function | Saves an existing flag to the database. Better use save(). | ||
flag_flag:: |
function | Determines whether the user has the permission to use this flag. | ||
flag_flag:: |
function | Returns TRUE if this flag requires anonymous user cookies. | ||
flag_flag:: |
function | Validates this flag's options. | ||
flag_flag:: |
function | Validates that the current flag's access settings are valid. | ||
flag_flag:: |
function | Validates that the current flag's name is valid. | ||
flag_flag:: |
function | Decreases the flag count for an object and clears the static counts cache. | ||
flag_flag:: |
function | Set a cookie for anonymous users to record their flagging. | ||
flag_flag:: |
function | A shortcut function to output the link URL. | ||
flag_flag:: |
function | Increases the flag count for an object and clears the static counts cache. | ||
flag_flag:: |
function | Determines if a certain user has flagged this object. | ||
flag_flag:: |
function | Remove the cookie for anonymous users to record their unflagging. |