Révision b08d2851
Ajouté par Assos Assos il y a environ 10 ans
drupal7/sites/all/modules/flag/includes/flag/flag_flag.inc | ||
---|---|---|
676 | 676 |
* Flag the item even if the $account user don't have permission to do so. |
677 | 677 |
* @param $flagging |
678 | 678 |
* (optional) This method works in tandem with Drupal's Field subsystem. |
679 |
* Pass in a flagging entity if you want operate on it as well. |
|
679 |
* Pass in a Flagging entity if you want operate on it as well. This may be |
|
680 |
* used either of the following cases: |
|
681 |
* - to save field data on a new Flagging entity at the same time as |
|
682 |
* flagging an entity. In this case, using Entity API's entity_create() |
|
683 |
* is recommended, although the Flagging entity may also be created |
|
684 |
* directly as a new stdClass object. |
|
685 |
* - to update field data an existing flagging. The $action parameter should |
|
686 |
* be set to 'flag'. The Flagging entity will need to be loaded first with |
|
687 |
* flagging_load(). |
|
680 | 688 |
* |
681 | 689 |
* @return |
682 | 690 |
* FALSE if some error occured (e.g., user has no permission, flag isn't |
... | ... | |
712 | 720 |
return FALSE; |
713 | 721 |
} |
714 | 722 |
|
715 |
// Clear various caches; We don't want code running after us to report |
|
716 |
// wrong counts or false flaggings. |
|
717 |
drupal_static_reset('flag_get_counts'); |
|
718 |
drupal_static_reset('flag_get_user_flags'); |
|
719 |
drupal_static_reset('flag_get_entity_flags'); |
|
720 |
|
|
721 | 723 |
// Find out which user id to use. |
722 | 724 |
$uid = $this->global ? 0 : $account->uid; |
723 | 725 |
|
... | ... | |
788 | 790 |
} |
789 | 791 |
} |
790 | 792 |
elseif ($action == 'flag') { |
791 |
// Invoke hook_entity_presave(). |
|
792 |
module_invoke_all('entity_presave', $flagging, 'flagging'); |
|
793 |
|
|
794 | 793 |
if ($this->uses_anonymous_cookies()) { |
795 | 794 |
$this->_flag_anonymous($entity_id); |
796 | 795 |
} |
796 |
|
|
797 |
// By definition there is no flagging entity yet, but we may have been |
|
798 |
// passed one in to save. |
|
799 |
if (!isset($flagging)) { |
|
800 |
// Construct a new flagging object if we weren't passed one. |
|
801 |
$flagging = $this->new_flagging($entity_id, $uid, $sid); |
|
802 |
} |
|
803 |
|
|
804 |
// Invoke hook_entity_presave(). |
|
805 |
// In the case that we have been passed in a flagging entity to update, |
|
806 |
// this is technically still a presave, even though the {flagging} table |
|
807 |
// itself is not changed. |
|
808 |
module_invoke_all('entity_presave', $flagging, 'flagging'); |
|
809 |
|
|
797 | 810 |
if (!$flagged) { |
798 |
// The entity is unflagged. By definition there is no flagging entity, |
|
799 |
// but we may have been passed one in to save. |
|
800 |
if (!isset($flagging)) { |
|
801 |
// Construct a new flagging object if we don't have one. |
|
802 |
$flagging = $this->new_flagging($entity_id, $uid, $sid); |
|
803 |
} |
|
811 |
// The entity is unflagged. |
|
812 |
|
|
804 | 813 |
// Save the flagging entity (just our table). |
805 |
$flagging_id = $this->_flag($entity_id, $uid, $sid); |
|
806 |
// The _flag() method is a plain DB record writer, so it's a bit |
|
807 |
// antiquated. We have to explicitly get our new ID out. |
|
808 |
$flagging->flagging_id = $flagging_id; |
|
814 |
$this->_flag($flagging); |
|
809 | 815 |
$this->_increase_count($entity_id); |
810 | 816 |
// We're writing out a flagging entity even when we aren't passed one |
811 | 817 |
// (e.g., when flagging via JavaScript toggle links); in this case |
... | ... | |
830 | 836 |
else { |
831 | 837 |
// Nothing to do. Item is already flagged. |
832 | 838 |
// |
833 |
// Except in the case a $flagging object is passed in: in this case
|
|
834 |
// we're, for example, arriving from an editing form and need to update
|
|
835 |
// the entity.
|
|
836 |
if ($flagging) {
|
|
839 |
// Except in the case a $flagging object is one that was passed in: in
|
|
840 |
// this case we're being requested to update the flagging. This occurs,
|
|
841 |
// for example, when the user updates the flagging entity form.
|
|
842 |
if (!empty($flagging->flagging_id)) {
|
|
837 | 843 |
$this->_update_flagging($flagging); |
838 | 844 |
} |
839 | 845 |
} |
... | ... | |
862 | 868 |
// Update the cache. |
863 | 869 |
entity_get_controller('flagging')->resetCache(); |
864 | 870 |
// Invoke hook_entity_update(). |
871 |
// Since there are no fields on the {flagging} table that can be |
|
872 |
// meaningfully changed, we don't perform an update on it. However, this |
|
873 |
// technically still counts as updating the flagging entity, since we update |
|
874 |
// its fields. |
|
865 | 875 |
module_invoke_all('entity_update', $flagging, 'flagging'); |
866 | 876 |
} |
867 | 877 |
private function _delete_flagging($flagging) { |
... | ... | |
892 | 902 |
return (object) array( |
893 | 903 |
'flagging_id' => NULL, |
894 | 904 |
'flag_name' => $this->name, |
905 |
'fid' => $this->fid, |
|
906 |
'entity_type' => $this->entity_type, |
|
895 | 907 |
'entity_id' => $entity_id, |
896 | 908 |
'uid' => $uid, |
897 | 909 |
'sid' => $sid, |
910 |
// The timestamp is not set until this is saved. |
|
898 | 911 |
); |
899 | 912 |
} |
900 | 913 |
|
... | ... | |
979 | 992 |
* You probably shouldn't call this raw private method: call the flag() |
980 | 993 |
* function instead. |
981 | 994 |
* |
982 |
* @return
|
|
983 |
* The 'flagging_id' column of the new {flagging} record.
|
|
995 |
* @param
|
|
996 |
* The flagging record.
|
|
984 | 997 |
* |
985 | 998 |
* @private |
986 | 999 |
*/ |
987 |
function _flag($entity_id, $uid, $sid) { |
|
988 |
$flagging_id = db_insert('flagging') |
|
989 |
->fields(array( |
|
990 |
'fid' => $this->fid, |
|
991 |
'entity_type' => $this->entity_type, |
|
992 |
'entity_id' => $entity_id, |
|
993 |
'uid' => $uid, |
|
994 |
'sid' => $sid, |
|
995 |
'timestamp' => REQUEST_TIME, |
|
996 |
)) |
|
997 |
->execute(); |
|
998 |
return $flagging_id; |
|
1000 |
function _flag($flagging) { |
|
1001 |
// Set the timestamp. |
|
1002 |
$flagging->timestamp = REQUEST_TIME; |
|
1003 |
|
|
1004 |
drupal_write_record('flagging', $flagging); |
|
1005 |
|
|
1006 |
// Clear various caches; we don't want code running after us to report |
|
1007 |
// wrong counts or false flaggings. |
|
1008 |
drupal_static_reset('flag_get_user_flags'); |
|
1009 |
drupal_static_reset('flag_get_entity_flags'); |
|
1010 |
// Despite being named in the same pattern as the count API functions, these |
|
1011 |
// query the {flagging} table, so are reset here. |
|
1012 |
drupal_static_reset('flag_get_entity_flag_counts'); |
|
1013 |
drupal_static_reset('flag_get_user_flag_counts'); |
|
999 | 1014 |
} |
1000 | 1015 |
|
1001 | 1016 |
/** |
... | ... | |
1008 | 1023 |
*/ |
1009 | 1024 |
function _unflag($entity_id, $flagging_id) { |
1010 | 1025 |
db_delete('flagging')->condition('flagging_id', $flagging_id)->execute(); |
1026 |
|
|
1027 |
// Clear various caches; we don't want code running after us to report |
|
1028 |
// wrong counts or false flaggings. |
|
1029 |
drupal_static_reset('flag_get_user_flags'); |
|
1030 |
drupal_static_reset('flag_get_entity_flags'); |
|
1031 |
// Despite being named in the same pattern as the count API functions, these |
|
1032 |
// query the {flagging} table, so are reset here. |
|
1033 |
drupal_static_reset('flag_get_entity_flag_counts'); |
|
1034 |
drupal_static_reset('flag_get_user_flag_counts'); |
|
1011 | 1035 |
} |
1012 | 1036 |
|
1013 | 1037 |
/** |
1014 |
* Increases the flag count for an object. |
|
1038 |
* Increases the flag count for an object and clears the static counts cache.
|
|
1015 | 1039 |
* |
1016 | 1040 |
* @param $entity_id |
1017 | 1041 |
* For which item should the count be increased. |
... | ... | |
1036 | 1060 |
)) |
1037 | 1061 |
->expression('count', 'count + :inc', array(':inc' => $number)) |
1038 | 1062 |
->execute(); |
1063 |
|
|
1064 |
// Reset the static cache of flag counts, so code running after this gets |
|
1065 |
// correct counts. |
|
1066 |
drupal_static_reset('flag_get_counts'); |
|
1067 |
drupal_static_reset('flag_get_flag_counts'); |
|
1039 | 1068 |
} |
1040 | 1069 |
|
1041 | 1070 |
/** |
1042 |
* Decreases the flag count for an object. |
|
1071 |
* Decreases the flag count for an object and clears the static counts cache.
|
|
1043 | 1072 |
* |
1044 | 1073 |
* @param $entity_id |
1045 | 1074 |
* For which item should the count be descreased. |
... | ... | |
1066 | 1095 |
->condition('fid', $this->fid) |
1067 | 1096 |
->condition('entity_id', $entity_id) |
1068 | 1097 |
->execute(); |
1098 |
|
|
1099 |
// Reset the static cache of flag counts, so code running after this gets |
|
1100 |
// correct counts. |
|
1101 |
drupal_static_reset('flag_get_counts'); |
|
1102 |
drupal_static_reset('flag_get_flag_counts'); |
|
1069 | 1103 |
} |
1070 | 1104 |
|
1071 | 1105 |
/** |
Formats disponibles : Unified diff
Weekly update of contrib modules