| 
									
										
										
										
											2008-11-20 15:55:06 -05:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 15:55:06 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Table Definition for profile_tag | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  | class Profile_tag extends Managed_DataObject | 
					
						
							| 
									
										
										
										
											2008-11-20 15:55:06 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     public $__table = 'profile_tag';                     // table name
 | 
					
						
							|  |  |  |     public $tagger;                          // int(4)  primary_key not_null
 | 
					
						
							|  |  |  |     public $tagged;                          // int(4)  primary_key not_null
 | 
					
						
							|  |  |  |     public $tag;                             // varchar(64)  primary_key not_null
 | 
					
						
							| 
									
										
										
										
											2020-06-29 01:41:46 +03:00
										 |  |  |     public $modified;                        // timestamp()  not_null default_CURRENT_TIMESTAMP
 | 
					
						
							| 
									
										
										
										
											2008-11-20 15:55:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							|  |  |  |                 'tagger' => array('type' => 'int', 'not null' => true, 'description' => 'user making the tag'), | 
					
						
							|  |  |  |                 'tagged' => array('type' => 'int', 'not null' => true, 'description' => 'profile tagged'), | 
					
						
							|  |  |  |                 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this notice'), | 
					
						
							| 
									
										
										
										
											2020-06-29 01:41:46 +03:00
										 |  |  |                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date the tag was added'), | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('tagger', 'tagged', 'tag'), | 
					
						
							|  |  |  |             'foreign keys' => array( | 
					
						
							|  |  |  |                 'profile_tag_tagger_fkey' => array('profile', array('tagger' => 'id')), | 
					
						
							|  |  |  |                 'profile_tag_tagged_fkey' => array('profile', array('tagged' => 'id')), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'indexes' => array( | 
					
						
							|  |  |  |                 'profile_tag_modified_idx' => array('modified'), | 
					
						
							|  |  |  |                 'profile_tag_tagger_tag_idx' => array('tagger', 'tag'), | 
					
						
							|  |  |  |                 'profile_tag_tagged_idx' => array('tagged'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public function links() | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |     { | 
					
						
							|  |  |  |         return array('tagger,tag' => 'profile_list:tagger,tag'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public function getMeta() | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |     { | 
					
						
							|  |  |  |         return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function getSelfTagsArray(Profile $target) | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return self::getTagsArray($target->getID(), $target->getID(), $target); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function setSelfTags(Profile $target, array $newtags, array $privacy = []) | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return self::setTags($target->getID(), $target->getID(), $newtags, $privacy); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function getTags($tagger, $tagged, $auth_user = null) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         $profile_list = new Profile_list(); | 
					
						
							|  |  |  |         $include_priv = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!($auth_user instanceof User || | 
					
						
							|  |  |  |             $auth_user instanceof Profile) || | 
					
						
							|  |  |  |             ($auth_user->id !== $tagger)) { | 
					
						
							|  |  |  |             $profile_list->private = false; | 
					
						
							|  |  |  |             $include_priv = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $key = sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $include_priv); | 
					
						
							|  |  |  |         $tags = Profile_list::getCached($key); | 
					
						
							|  |  |  |         if ($tags !== false) { | 
					
						
							|  |  |  |             return $tags; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-08 11:42:28 +05:30
										 |  |  |         $qry = 'select profile_list.* from profile_list left join '. | 
					
						
							|  |  |  |                'profile_tag on (profile_list.tag = profile_tag.tag and '. | 
					
						
							|  |  |  |                'profile_list.tagger = profile_tag.tagger) where '. | 
					
						
							|  |  |  |                'profile_tag.tagger = %d and profile_tag.tagged = %d '; | 
					
						
							|  |  |  |         $qry = sprintf($qry, $tagger, $tagged); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$include_priv) { | 
					
						
							| 
									
										
										
										
											2020-07-21 18:06:39 +03:00
										 |  |  |             $qry .= ' AND profile_list.private IS NOT TRUE'; | 
					
						
							| 
									
										
										
										
											2011-07-08 11:42:28 +05:30
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $profile_list->query($qry); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         Profile_list::setCache($key, $profile_list); | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         return $profile_list; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function getTagsArray($tagger, $tagged, Profile $scoped = null) | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |     { | 
					
						
							|  |  |  |         $ptag = new Profile_tag(); | 
					
						
							| 
									
										
										
										
											2011-09-27 09:42:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |         $qry = sprintf( | 
					
						
							|  |  |  |             'SELECT profile_tag.tag '. | 
					
						
							|  |  |  |             'FROM profile_tag INNER JOIN profile_list '. | 
					
						
							|  |  |  |             ' ON (profile_tag.tagger = profile_list.tagger ' . | 
					
						
							|  |  |  |             '     and profile_tag.tag = profile_list.tag) ' . | 
					
						
							|  |  |  |             'WHERE profile_tag.tagger = %d ' . | 
					
						
							|  |  |  |             'AND   profile_tag.tagged = %d ', | 
					
						
							|  |  |  |             $tagger, | 
					
						
							|  |  |  |             $tagged | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2011-09-27 09:42:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |         if (!$scoped instanceof Profile || $scoped->getID() !== $tagger) { | 
					
						
							| 
									
										
										
										
											2020-07-21 18:06:39 +03:00
										 |  |  |             $qry .= 'AND profile_list.private IS NOT TRUE'; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         $tags = array(); | 
					
						
							| 
									
										
										
										
											2011-09-27 09:42:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-27 11:32:05 -04:00
										 |  |  |         $ptag->query($qry); | 
					
						
							| 
									
										
										
										
											2011-09-27 09:42:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         while ($ptag->fetch()) { | 
					
						
							|  |  |  |             $tags[] = $ptag->tag; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         return $tags; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function setTags($tagger, $tagged, array $newtags, array $privacy = []) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  |         $newtags = array_unique($newtags); | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |         $oldtags = self::getTagsArray($tagger, $tagged, Profile::getByID($tagger)); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         $ptag = new Profile_tag(); | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-30 15:43:13 +05:30
										 |  |  |         // Delete stuff that's in old and not in new
 | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         $to_delete = array_diff($oldtags, $newtags); | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-22 11:54:23 -04:00
										 |  |  |         // Insert stuff that's in new and not in old
 | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         $to_insert = array_diff($newtags, $oldtags); | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         foreach ($to_delete as $deltag) { | 
					
						
							|  |  |  |             self::unTag($tagger, $tagged, $deltag); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         foreach ($to_insert as $instag) { | 
					
						
							|  |  |  |             $private = isset($privacy[$instag]) ? $privacy[$instag] : false; | 
					
						
							|  |  |  |             self::setTag($tagger, $tagged, $instag, null, $private); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # set a single tag
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function setTag($tagger, $tagged, $tag, $desc=null, $private = false) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger, | 
					
						
							|  |  |  |                                            'tagged' => $tagged, | 
					
						
							|  |  |  |                                            'tag' => $tag)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # if tag already exists, return it
 | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |         if ($ptag instanceof Profile_tag) { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |             return $ptag; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |         $tagger_profile = Profile::getByID($tagger); | 
					
						
							|  |  |  |         $tagged_profile = Profile::getByID($tagged); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) { | 
					
						
							|  |  |  |             if (!$tagger_profile->canTag($tagged_profile)) { | 
					
						
							| 
									
										
										
										
											2011-04-10 19:59:55 +02:00
										 |  |  |                 // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged.
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |                 throw new ClientException(_('You cannot tag this user.')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $tags = new Profile_list(); | 
					
						
							|  |  |  |             $tags->tagger = $tagger; | 
					
						
							|  |  |  |             $count = (int) $tags->count('distinct tag'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($count >= common_config('peopletag', 'maxtags')) { | 
					
						
							| 
									
										
										
										
											2011-04-10 19:59:55 +02:00
										 |  |  |                 // TRANS: Client exception thrown trying to set more tags than allowed.
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |                 throw new ClientException(sprintf( | 
					
						
							|  |  |  |                     _('You already have created %d or more tags ' . | 
					
						
							|  |  |  |                       'which is the maximum allowed number of tags. ' . | 
					
						
							|  |  |  |                       'Try using or deleting some existing tags.'), | 
					
						
							|  |  |  |                     common_config('peopletag', 'maxtags') | 
					
						
							|  |  |  |                 )); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $plist = new Profile_list(); | 
					
						
							| 
									
										
										
										
											2020-06-08 12:25:01 +03:00
										 |  |  |             $plist->query('START TRANSACTION'); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |             $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) { | 
					
						
							| 
									
										
										
										
											2011-04-17 20:08:03 +02:00
										 |  |  |                 // TRANS: Client exception thrown when trying to add more people than allowed to a list.
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |                 throw new ClientException(sprintf( | 
					
						
							|  |  |  |                     _('You already have %1$d or more people in list %2$s, ' . | 
					
						
							|  |  |  |                       'which is the maximum allowed number. ' . | 
					
						
							|  |  |  |                       'Try unlisting others first.'), | 
					
						
							|  |  |  |                     common_config('peopletag', 'maxpeople'), | 
					
						
							|  |  |  |                     $tag | 
					
						
							|  |  |  |                 )); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $newtag = new Profile_tag(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $newtag->tagger = $tagger; | 
					
						
							|  |  |  |             $newtag->tagged = $tagged; | 
					
						
							|  |  |  |             $newtag->tag = $tag; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $result = $newtag->insert(); | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |             if (!$result) { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |                 common_log_db_error($newtag, 'INSERT', __FILE__); | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |                 $plist->query('ROLLBACK'); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 $plist->query('COMMIT'); | 
					
						
							|  |  |  |                 Event::handle('EndTagProfile', array($newtag)); | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 $newtag->delete(); | 
					
						
							|  |  |  |                 $profile_list->delete(); | 
					
						
							|  |  |  |                 throw $e; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |             $profile_list->taggedCount(true); | 
					
						
							|  |  |  |             self::blowCaches($tagger, $tagged); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         return $newtag; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function unTag($tagger, $tagged, $tag) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger, | 
					
						
							|  |  |  |                                            'tagged' => $tagged, | 
					
						
							|  |  |  |                                            'tag'    => $tag)); | 
					
						
							|  |  |  |         if (!$ptag) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (Event::handle('StartUntagProfile', array($ptag))) { | 
					
						
							|  |  |  |             $orig = clone($ptag); | 
					
						
							|  |  |  |             $result = $ptag->delete(); | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |             if ($result === false) { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |                 common_log_db_error($this, 'DELETE', __FILE__); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |             Event::handle('EndUntagProfile', array($orig)); | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |             $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger)); | 
					
						
							|  |  |  |             if (!empty($profile_list)) { | 
					
						
							|  |  |  |                 $profile_list->taggedCount(true); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-17 12:09:24 +02:00
										 |  |  |             self::blowCaches($tagger, $tagged); | 
					
						
							|  |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // @fixme: move this to Profile_list?
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function cleanup($profile_list) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         $ptag = new Profile_tag(); | 
					
						
							|  |  |  |         $ptag->tagger = $profile_list->tagger; | 
					
						
							|  |  |  |         $ptag->tag = $profile_list->tag; | 
					
						
							|  |  |  |         $ptag->find(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |         while ($ptag->fetch()) { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |             if (Event::handle('StartUntagProfile', array($ptag))) { | 
					
						
							|  |  |  |                 $orig = clone($ptag); | 
					
						
							|  |  |  |                 $result = $ptag->delete(); | 
					
						
							|  |  |  |                 if (!$result) { | 
					
						
							|  |  |  |                     common_log_db_error($this, 'DELETE', __FILE__); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 Event::handle('EndUntagProfile', array($orig)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |     // move a tag!
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function moveTag($orig, $new) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         $tags = new Profile_tag(); | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |         $result = $tags->query(sprintf( | 
					
						
							| 
									
										
										
										
											2020-07-27 19:10:33 +03:00
										 |  |  |             <<<'END' | 
					
						
							|  |  |  |             UPDATE profile_tag | 
					
						
							|  |  |  |               SET tag = %1$s, tagger = %2$s, modified = CURRENT_TIMESTAMP | 
					
						
							|  |  |  |               WHERE tag = %3$s AND tagger = %4$s | 
					
						
							|  |  |  |             END, | 
					
						
							|  |  |  |             $tags->_quote($new->tag), | 
					
						
							|  |  |  |             $tags->_quote($new->tagger), | 
					
						
							|  |  |  |             $tags->_quote($orig->tag), | 
					
						
							|  |  |  |             $tags->_quote($orig->tagger) | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |         )); | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-28 19:36:05 +01:00
										 |  |  |         if ($result === false) { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |             common_log_db_error($tags, 'UPDATE', __FILE__); | 
					
						
							| 
									
										
										
										
											2013-10-28 19:36:05 +01:00
										 |  |  |             throw new Exception('Could not move Profile_tag, see db log for details.'); | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-10-28 19:36:05 +01:00
										 |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function blowCaches($tagger, $tagged) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         foreach (array(0, 1) as $perm) { | 
					
						
							|  |  |  |             self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm)); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-03-06 23:28:03 +05:30
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-02-28 15:17:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-22 11:54:23 -04:00
										 |  |  |     // Return profiles with a given tag
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public static function getTagged($tagger, $tag) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         $profile = new Profile(); | 
					
						
							|  |  |  |         $profile->query('SELECT profile.* ' . | 
					
						
							|  |  |  |                         'FROM profile JOIN profile_tag ' . | 
					
						
							|  |  |  |                         'ON profile.id = profile_tag.tagged ' . | 
					
						
							| 
									
										
										
										
											2013-07-16 10:35:44 -07:00
										 |  |  |                         'WHERE profile_tag.tagger = ' . $profile->escape($tagger) . ' ' . | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |                         "AND profile_tag.tag = '" . $profile->escape($tag) . "' "); | 
					
						
							|  |  |  |         $tagged = []; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         while ($profile->fetch()) { | 
					
						
							|  |  |  |             $tagged[] = clone($profile); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-03-30 15:43:13 +05:30
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-04-14 14:01:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public function insert() | 
					
						
							| 
									
										
										
										
											2011-04-14 14:01:10 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $result = parent::insert(); | 
					
						
							|  |  |  |         if ($result) { | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |             self::blow( | 
					
						
							|  |  |  |                 'profile_list:tagged_count:%d:%s', | 
					
						
							|  |  |  |                 $this->tagger, | 
					
						
							|  |  |  |                 $this->tag | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2011-04-14 14:01:10 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |     public function delete($useWhere = false) | 
					
						
							| 
									
										
										
										
											2011-04-14 14:01:10 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-10-29 10:20:57 +01:00
										 |  |  |         $result = parent::delete($useWhere); | 
					
						
							|  |  |  |         if ($result !== false) { | 
					
						
							| 
									
										
										
										
											2019-09-11 09:46:30 +03:00
										 |  |  |             self::blow( | 
					
						
							|  |  |  |                 'profile_list:tagged_count:%d:%s', | 
					
						
							|  |  |  |                 $this->tagger, | 
					
						
							|  |  |  |                 $this->tag | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2011-04-14 14:01:10 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-11-20 15:55:06 -05:00
										 |  |  | } |