diff --git a/home/zkday/Desktop/flag.inc b/flag.inc index 1eb474b..c054d03 100644 --- a/home/zkday/Desktop/flag.inc +++ b/flag.inc @@ -1191,82 +1191,167 @@ class flag_flag { } } -/** - * Implements a node flag. - */ -class flag_node extends flag_flag { - function options() { - $options = parent::options(); - $options += array( - 'show_on_page' => TRUE, - 'show_on_teaser' => TRUE, - 'show_on_form' => FALSE, - 'access_author' => '', - 'i18n' => 0, - ); - return $options; - } - - function options_form(&$form) { - parent::options_form($form); - // Support for i18n flagging requires Translation helpers module. - $form['i18n'] = array( - '#type' => 'radios', - '#title' => t('Internationalization'), - '#options' => array( - '1' => t('Flag translations of content as a group'), - '0' => t('Flag each translation of content separately'), - ), - '#default_value' => $this->i18n, - '#description' => t('Flagging translations as a group effectively allows users to flag the original piece of content regardless of the translation they are viewing. Changing this setting will not update content that has been flagged already.'), - '#access' => module_exists('translation_helpers'), - '#weight' => 5, - ); - - $form['access']['access_author'] = array( - '#type' => 'radios', - '#title' => t('Flag access by content authorship'), - '#options' => array( - '' => t('No additional restrictions'), - 'own' => t('Users may only flag content they own'), - 'others' => t('Users may only flag content of 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."), - ); - $form['display']['show_on_teaser'] = array( - '#type' => 'checkbox', - '#title' => t('Display link on node teaser'), - '#default_value' => $this->show_on_teaser, - '#access' => empty($this->locked['show_on_teaser']), - ); - $form['display']['show_on_page'] = array( - '#type' => 'checkbox', - '#title' => t('Display link on node page'), - '#default_value' => $this->show_on_page, - '#access' => empty($this->locked['show_on_page']), - ); - $form['display']['show_on_form'] = array( - '#type' => 'checkbox', - '#title' => t('Display checkbox on node edit form'), - '#default_value' => $this->show_on_form, - '#description' => t('If you elect to have a checkbox on the node edit form, you may specify its initial state in the settings form for each content type.', array('@content-types-url' => url('admin/structure/types'))), - '#access' => empty($this->locked['show_on_form']), - ); - } - function _load_content($content_id) { - return is_numeric($content_id) ? node_load($content_id) : NULL; - } +//need for entity. - function applies_to_content_object($node) { - if ($node && in_array($node->type, $this->types)) { - return TRUE; - } - return FALSE; - } +/** + * Declared abstract class of entity. + */ +abstract class flag_entity_flag extends flag_flag { + // name of entity e.g: node, comment, bundle. + protected $entityType; + // name of bundle + //protected $entityBundle; + + /** + * Constructor of class. + * @param string $entity_type + * @param string $bundle + */ + public function construct($entity_type) { + parent::construct(); + //$this->entityBundle = $bundle; + if ($entity_type == NULL) { + $entity_type = 'node'; + } + $this->entityType = $entity_type; + } + + /** + * (non-PHPdoc) + * @see flag_flag::_load_content() + */ + public function _load_content($content_id) { + $content_id = is_array($content_id) ? $content_id : array($content_id); + $entity = entity_load($this->entityType, $content_id); + //dpm($entity, "This is load_content:"); + return reset($entity); + } + + /** + * (non-PHPdoc) + * @see flag_flag::access_multiple() + */ + public function access_multiple($content_ids, $account = NULL) { + $access = parent::access_multiple($content_ids, $account); + // Ensure that only flaggable node types are granted access. This avoids a + // entity_load() on every type, usually done by applies_to_content_id(). + // load all enttiy + $result = entity_load($this->entityType, FALSE); + // featch all rows + foreach ($result as $row) { + $access[$row->identifier()] = FALSE; + } + return $access; + } + public function access($content_id) { + return TRUE; + } + /** + * (non-PHPdoc) + * @see flag_flag::applies_to_content_object() + */ + function applies_to_content_object($entity) { + if ($entity && in_array($this->entityType, $this->types)) { + return TRUE; + } + return FALSE; + } + + /** + * (non-PHPdoc) + * @see flag_flag::uses_hook_link() + */ + function uses_hook_link($teaser) { + return TRUE; + } + + function options() { + $options = parent::options(); + $options += array( + 'show_on_page' => TRUE, + 'show_on_teaser' => TRUE, + 'show_on_form' => FALSE, + 'access_author' => '', + 'i18n' => 0, + ); + return $options; + } + + function options_form(&$form) { + parent::options_form($form); + // Support for i18n flagging requires Translation helpers module. + $form['i18n'] = array( + '#type' => 'radios', + '#title' => t('Internationalization'), + '#options' => array( + '1' => t('Flag translations of content as a group'), + '0' => t('Flag each translation of content separately'), + ), + '#default_value' => $this->i18n, + '#description' => t('Flagging translations as a group effectively allows users to flag the original piece of content regardless of the translation they are viewing. Changing this setting will not update content that has been flagged already.'), + '#access' => module_exists('translation_helpers'), + '#weight' => 5, + ); + + $form['access']['access_author'] = array( + '#type' => 'radios', + '#title' => t('Flag access by content authorship'), + '#options' => array( + '' => t('No additional restrictions'), + 'own' => t('Users may only flag content they own'), + 'others' => t('Users may only flag content of 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."), + ); + + $form['display']['show_on_teaser'] = array( + '#type' => 'checkbox', + '#title' => t('Display link on node teaser'), + '#default_value' => $this->show_on_teaser, + '#access' => empty($this->locked['show_on_teaser']), + ); + $form['display']['show_on_page'] = array( + '#type' => 'checkbox', + '#title' => t('Display link on node page'), + '#default_value' => $this->show_on_page, + '#access' => empty($this->locked['show_on_page']), + ); + $form['display']['show_on_form'] = array( + '#type' => 'checkbox', + '#title' => t('Display checkbox on node edit form'), + '#default_value' => $this->show_on_form, + '#description' => t('If you elect to have a checkbox on the node edit form, you may specify its initial state in the settings form for each content type.', array('@content-types-url' => url('admin/structure/types'))), + '#access' => empty($this->locked['show_on_form']), + ); + } + + function get_content_id($entity) { + return $entity->identifier(); + } + + function get_labels_token_types() { + return array_merge(array($this->entityType), parent::get_labels_token_types()); + } + + function get_flag_action($content_id) { + $flag_action = parent::get_flag_action($content_id); + $entity = $this->fetch_content($content_id); + $flag_action->content_title = $entity->label(); + $flag_action->content_url = _flag_url($entity->uri()); + return $flag_action; + } +} +/** + * Implements a node flag. + */ +class flag_node extends flag_entity_flag { + public function construct() { + parent::construct('node'); + } function access_multiple($content_ids, $account = NULL) { $access = parent::access_multiple($content_ids, $account); @@ -1399,7 +1484,7 @@ class flag_node extends flag_flag { /** * Implements a comment flag. */ -class flag_comment extends flag_flag { +class flag_comment extends flag_entity_flag { function options() { $options = parent::options(); $options += array( @@ -1434,9 +1519,6 @@ class flag_comment extends flag_flag { ); } - function _load_content($content_id) { - return comment_load($content_id); - } function applies_to_content_object($comment) { if ($comment && ($node = node_load($comment->nid)) && in_array($node->type, $this->types)) {