User_group.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. <?php
  2. /**
  3. * Table Definition for user_group
  4. */
  5. class User_group extends Managed_DataObject
  6. {
  7. const JOIN_POLICY_OPEN = 0;
  8. const JOIN_POLICY_MODERATE = 1;
  9. const CACHE_WINDOW = 201;
  10. ###START_AUTOCODE
  11. /* the code below is auto generated do not remove the above tag */
  12. public $__table = 'user_group'; // table name
  13. public $id; // int(4) primary_key not_null
  14. public $profile_id; // int(4) primary_key not_null
  15. public $nickname; // varchar(64)
  16. public $fullname; // varchar(191) not 255 because utf8mb4 takes more space
  17. public $homepage; // varchar(191) not 255 because utf8mb4 takes more space
  18. public $description; // text
  19. public $location; // varchar(191) not 255 because utf8mb4 takes more space
  20. public $original_logo; // varchar(191) not 255 because utf8mb4 takes more space
  21. public $homepage_logo; // varchar(191) not 255 because utf8mb4 takes more space
  22. public $stream_logo; // varchar(191) not 255 because utf8mb4 takes more space
  23. public $mini_logo; // varchar(191) not 255 because utf8mb4 takes more space
  24. public $created; // datetime not_null default_0000-00-00%2000%3A00%3A00
  25. public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
  26. public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
  27. public $mainpage; // varchar(191) not 255 because utf8mb4 takes more space
  28. public $join_policy; // tinyint
  29. public $force_scope; // tinyint
  30. /* the code above is auto generated do not remove the tag below */
  31. ###END_AUTOCODE
  32. public function getObjectType()
  33. {
  34. return ActivityObject::GROUP;
  35. }
  36. public static function schemaDef()
  37. {
  38. return array(
  39. 'fields' => array(
  40. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
  41. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
  42. 'nickname' => array('type' => 'varchar', 'length' => 64, 'description' => 'nickname for addressing'),
  43. 'fullname' => array('type' => 'varchar', 'length' => 191, 'description' => 'display name'),
  44. 'homepage' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL, cached so we dont regenerate'),
  45. 'description' => array('type' => 'text', 'description' => 'group description'),
  46. 'location' => array('type' => 'varchar', 'length' => 191, 'description' => 'related physical location, if any'),
  47. 'original_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'original size logo'),
  48. 'homepage_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'homepage (profile) size logo'),
  49. 'stream_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'stream-sized logo'),
  50. 'mini_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'mini logo'),
  51. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  52. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  53. 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'),
  54. 'mainpage' => array('type' => 'varchar', 'length' => 191, 'description' => 'page for group info to link to'),
  55. 'join_policy' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=open; 1=requires admin approval'),
  56. 'force_scope' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=never,1=sometimes,-1=always'),
  57. ),
  58. 'primary key' => array('id'),
  59. 'unique keys' => array(
  60. 'user_group_uri_key' => array('uri'),
  61. // when it's safe and everyone's run upgrade.php 'user_profile_id_key' => array('profile_id'),
  62. ),
  63. 'foreign keys' => array(
  64. 'user_group_id_fkey' => array('profile', array('profile_id' => 'id')),
  65. ),
  66. 'indexes' => array(
  67. 'user_group_nickname_idx' => array('nickname'),
  68. 'user_group_profile_id_idx' => array('profile_id'), //make this unique in future
  69. ),
  70. );
  71. }
  72. protected $_profile = array();
  73. /**
  74. * @return Profile
  75. *
  76. * @throws GroupNoProfileException if user has no profile
  77. */
  78. public function getProfile()
  79. {
  80. if (!isset($this->_profile[$this->profile_id])) {
  81. $profile = Profile::getKV('id', $this->profile_id);
  82. if (!$profile instanceof Profile) {
  83. throw new GroupNoProfileException($this);
  84. }
  85. $this->_profile[$this->profile_id] = $profile;
  86. }
  87. return $this->_profile[$this->profile_id];
  88. }
  89. public function getNickname()
  90. {
  91. return $this->getProfile()->getNickname();
  92. }
  93. public static function defaultLogo($size)
  94. {
  95. static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
  96. AVATAR_STREAM_SIZE => 'stream',
  97. AVATAR_MINI_SIZE => 'mini');
  98. return Theme::path('default-avatar-'.$sizenames[$size].'.png');
  99. }
  100. function homeUrl()
  101. {
  102. return $this->getProfile()->getUrl();
  103. }
  104. function getUri()
  105. {
  106. $uri = null;
  107. if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
  108. if (!empty($this->uri)) {
  109. $uri = $this->uri;
  110. } elseif ($this->isLocal()) {
  111. $uri = common_local_url('groupbyid',
  112. array('id' => $this->id));
  113. }
  114. }
  115. Event::handle('EndUserGroupGetUri', array($this, &$uri));
  116. return $uri;
  117. }
  118. function permalink()
  119. {
  120. $url = null;
  121. if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
  122. if ($this->isLocal()) {
  123. $url = common_local_url('groupbyid',
  124. array('id' => $this->id));
  125. }
  126. }
  127. Event::handle('EndUserGroupPermalink', array($this, &$url));
  128. return $url;
  129. }
  130. function getNotices($offset, $limit, $since_id=null, $max_id=null)
  131. {
  132. $stream = new GroupNoticeStream($this);
  133. return $stream->getNotices($offset, $limit, $since_id, $max_id);
  134. }
  135. function getMembers($offset=0, $limit=null) {
  136. $ids = null;
  137. if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) {
  138. $ids = $this->getMemberIDs($offset,
  139. $limit);
  140. } else {
  141. $key = sprintf('group:member_ids:%d', $this->id);
  142. $window = self::cacheGet($key);
  143. if ($window === false) {
  144. $window = $this->getMemberIDs(0,
  145. User_group::CACHE_WINDOW);
  146. self::cacheSet($key, $window);
  147. }
  148. $ids = array_slice($window,
  149. $offset,
  150. $limit);
  151. }
  152. return Profile::multiGet('id', $ids);
  153. }
  154. function getMemberIDs($offset=0, $limit=null)
  155. {
  156. $gm = new Group_member();
  157. $gm->selectAdd();
  158. $gm->selectAdd('profile_id');
  159. $gm->group_id = $this->id;
  160. $gm->orderBy('created DESC');
  161. if (!is_null($limit)) {
  162. $gm->limit($offset, $limit);
  163. }
  164. $ids = array();
  165. if ($gm->find()) {
  166. while ($gm->fetch()) {
  167. $ids[] = $gm->profile_id;
  168. }
  169. }
  170. return $ids;
  171. }
  172. /**
  173. * Get pending members, who have not yet been approved.
  174. *
  175. * @param int $offset
  176. * @param int $limit
  177. * @return Profile
  178. */
  179. function getRequests($offset=0, $limit=null)
  180. {
  181. $rq = new Group_join_queue();
  182. $rq->group_id = $this->id;
  183. $members = new Profile();
  184. $members->joinAdd(['id', $rq, 'profile_id']);
  185. if ($limit != null) {
  186. $members->limit($offset, $limit);
  187. }
  188. $members->find();
  189. return $members;
  190. }
  191. public function getAdminCount()
  192. {
  193. $block = new Group_member();
  194. $block->group_id = $this->id;
  195. $block->is_admin = 1;
  196. return $block->count();
  197. }
  198. public function getMemberCount()
  199. {
  200. $key = sprintf("group:member_count:%d", $this->id);
  201. $cnt = self::cacheGet($key);
  202. if (is_integer($cnt)) {
  203. return (int) $cnt;
  204. }
  205. $mem = new Group_member();
  206. $mem->group_id = $this->id;
  207. // XXX: why 'distinct'?
  208. $cnt = (int) $mem->count('distinct profile_id');
  209. self::cacheSet($key, $cnt);
  210. return $cnt;
  211. }
  212. function getBlockedCount()
  213. {
  214. // XXX: WORM cache this
  215. $block = new Group_block();
  216. $block->group_id = $this->id;
  217. return $block->count();
  218. }
  219. function getQueueCount()
  220. {
  221. // XXX: WORM cache this
  222. $queue = new Group_join_queue();
  223. $queue->group_id = $this->id;
  224. return $queue->count();
  225. }
  226. function getAdmins($offset=null, $limit=null) // offset is null because DataObject wants it, 0 would mean no results
  227. {
  228. $admins = new Profile();
  229. $admins->joinAdd(array('id', 'group_member:profile_id'));
  230. $admins->whereAdd(sprintf('group_member.group_id = %u AND group_member.is_admin = 1', $this->id));
  231. $admins->orderBy('group_member.modified ASC');
  232. $admins->limit($offset, $limit);
  233. $admins->find();
  234. return $admins;
  235. }
  236. function getBlocked($offset=null, $limit=null) // offset is null because DataObject wants it, 0 would mean no results
  237. {
  238. $blocked = new Profile();
  239. $blocked->joinAdd(array('id', 'group_block:blocked'));
  240. $blocked->whereAdd(sprintf('group_block.group_id = %u', $this->id));
  241. $blocked->orderBy('group_block.modified DESC');
  242. $blocked->limit($offset, $limit);
  243. $blocked->find();
  244. return $blocked;
  245. }
  246. function setOriginal($filename)
  247. {
  248. // This should be handled by the Profile->setOriginal function so user and group avatars are handled the same
  249. $imagefile = new ImageFile(null, Avatar::path($filename));
  250. $sizes = array('homepage_logo' => AVATAR_PROFILE_SIZE,
  251. 'stream_logo' => AVATAR_STREAM_SIZE,
  252. 'mini_logo' => AVATAR_MINI_SIZE);
  253. $orig = clone($this);
  254. $this->original_logo = Avatar::url($filename);
  255. foreach ($sizes as $name=>$size) {
  256. $filename = Avatar::filename($this->profile_id, image_type_to_extension($imagefile->preferredType()),
  257. $size, common_timestamp());
  258. $imagefile->resizeTo(Avatar::path($filename), array('width'=>$size, 'height'=>$size));
  259. $this->$name = Avatar::url($filename);
  260. }
  261. common_debug(common_log_objstring($this));
  262. return $this->update($orig);
  263. }
  264. function getBestName()
  265. {
  266. return ($this->fullname) ? $this->fullname : $this->nickname;
  267. }
  268. /**
  269. * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
  270. * if no fullname is provided.
  271. *
  272. * @return string
  273. */
  274. function getFancyName()
  275. {
  276. if ($this->fullname) {
  277. // TRANS: Full name of a profile or group followed by nickname in parens
  278. return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
  279. } else {
  280. return $this->nickname;
  281. }
  282. }
  283. function getAliases()
  284. {
  285. $aliases = array();
  286. // XXX: cache this
  287. $alias = new Group_alias();
  288. $alias->group_id = $this->id;
  289. if ($alias->find()) {
  290. while ($alias->fetch()) {
  291. $aliases[] = $alias->alias;
  292. }
  293. }
  294. $alias->free();
  295. return $aliases;
  296. }
  297. function setAliases($newaliases) {
  298. $newaliases = array_unique($newaliases);
  299. $oldaliases = $this->getAliases();
  300. // Delete stuff that's old that not in new
  301. $to_delete = array_diff($oldaliases, $newaliases);
  302. // Insert stuff that's in new and not in old
  303. $to_insert = array_diff($newaliases, $oldaliases);
  304. $alias = new Group_alias();
  305. $alias->group_id = $this->id;
  306. foreach ($to_delete as $delalias) {
  307. $alias->alias = $delalias;
  308. $result = $alias->delete();
  309. if (!$result) {
  310. common_log_db_error($alias, 'DELETE', __FILE__);
  311. return false;
  312. }
  313. }
  314. foreach ($to_insert as $insalias) {
  315. if ($insalias === $this->nickname) {
  316. continue;
  317. }
  318. $alias->alias = Nickname::normalize($insalias, true);
  319. $result = $alias->insert();
  320. if (!$result) {
  321. common_log_db_error($alias, 'INSERT', __FILE__);
  322. return false;
  323. }
  324. }
  325. return true;
  326. }
  327. static function getForNickname($nickname, Profile $profile=null)
  328. {
  329. $nickname = Nickname::normalize($nickname);
  330. // Are there any matching remote groups this profile's in?
  331. if ($profile instanceof Profile) {
  332. $group = $profile->getGroups(0, null);
  333. while ($group instanceof User_group && $group->fetch()) {
  334. if ($group->nickname == $nickname) {
  335. // @fixme is this the best way?
  336. return clone($group);
  337. }
  338. }
  339. }
  340. // If not, check local groups.
  341. $group = Local_group::getKV('nickname', $nickname);
  342. if ($group instanceof Local_group) {
  343. return User_group::getKV('id', $group->group_id);
  344. }
  345. $alias = Group_alias::getKV('alias', $nickname);
  346. if ($alias instanceof Group_alias) {
  347. return User_group::getKV('id', $alias->group_id);
  348. }
  349. return null;
  350. }
  351. function getUserMembers()
  352. {
  353. // XXX: cache this
  354. $user = new User();
  355. if(common_config('db','quote_identifiers'))
  356. $user_table = '"user"';
  357. else $user_table = 'user';
  358. $qry =
  359. 'SELECT id ' .
  360. 'FROM '. $user_table .' JOIN group_member '.
  361. 'ON '. $user_table .'.id = group_member.profile_id ' .
  362. 'WHERE group_member.group_id = %d ';
  363. $user->query(sprintf($qry, $this->id));
  364. $ids = array();
  365. while ($user->fetch()) {
  366. $ids[] = $user->id;
  367. }
  368. $user->free();
  369. return $ids;
  370. }
  371. static function maxDescription()
  372. {
  373. $desclimit = common_config('group', 'desclimit');
  374. // null => use global limit (distinct from 0!)
  375. if (is_null($desclimit)) {
  376. $desclimit = common_config('site', 'textlimit');
  377. }
  378. return $desclimit;
  379. }
  380. static function descriptionTooLong($desc)
  381. {
  382. $desclimit = self::maxDescription();
  383. return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
  384. }
  385. function asAtomEntry($namespace=false, $source=false)
  386. {
  387. $xs = new XMLStringer(true);
  388. if ($namespace) {
  389. $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
  390. 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
  391. } else {
  392. $attrs = array();
  393. }
  394. $xs->elementStart('entry', $attrs);
  395. if ($source) {
  396. $xs->elementStart('source');
  397. $xs->element('id', null, $this->permalink());
  398. $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
  399. $xs->element('link', array('href' => $this->permalink()));
  400. $xs->element('updated', null, $this->modified);
  401. $xs->elementEnd('source');
  402. }
  403. $xs->element('title', null, $this->nickname);
  404. $xs->element('summary', null, common_xml_safe_str($this->description));
  405. $xs->element('link', array('rel' => 'alternate',
  406. 'href' => $this->permalink()));
  407. $xs->element('id', null, $this->permalink());
  408. $xs->element('published', null, common_date_w3dtf($this->created));
  409. $xs->element('updated', null, common_date_w3dtf($this->modified));
  410. $xs->element(
  411. 'content',
  412. array('type' => 'html'),
  413. common_xml_safe_str($this->description)
  414. );
  415. $xs->elementEnd('entry');
  416. return $xs->getString();
  417. }
  418. function asAtomAuthor()
  419. {
  420. $xs = new XMLStringer(true);
  421. $xs->elementStart('author');
  422. $xs->element('name', null, $this->nickname);
  423. $xs->element('uri', null, $this->permalink());
  424. $xs->elementEnd('author');
  425. return $xs->getString();
  426. }
  427. /**
  428. * Returns an XML string fragment with group information as an
  429. * Activity Streams noun object with the given element type.
  430. *
  431. * Assumes that 'activity', 'georss', and 'poco' namespace has been
  432. * previously defined.
  433. *
  434. * @param string $element one of 'actor', 'subject', 'object', 'target'
  435. *
  436. * @return string
  437. */
  438. function asActivityNoun($element)
  439. {
  440. $noun = ActivityObject::fromGroup($this);
  441. return $noun->asString('activity:' . $element);
  442. }
  443. function getAvatar()
  444. {
  445. return empty($this->homepage_logo)
  446. ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
  447. : $this->homepage_logo;
  448. }
  449. static function register($fields) {
  450. if (!empty($fields['userid'])) {
  451. $profile = Profile::getKV('id', $fields['userid']);
  452. if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
  453. common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
  454. // TRANS: Client exception thrown when a user tries to create a group while banned.
  455. throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
  456. }
  457. }
  458. $fields['nickname'] = Nickname::normalize($fields['nickname']);
  459. // MAGICALLY put fields into current scope
  460. // @fixme kill extract(); it makes debugging absurdly hard
  461. $defaults = array('nickname' => null,
  462. 'fullname' => null,
  463. 'homepage' => null,
  464. 'description' => null,
  465. 'location' => null,
  466. 'uri' => null,
  467. 'mainpage' => null,
  468. 'aliases' => array(),
  469. 'userid' => null);
  470. $fields = array_merge($defaults, $fields);
  471. extract($fields);
  472. $group = new User_group();
  473. if (empty($uri)) {
  474. // fill in later...
  475. $uri = null;
  476. }
  477. if (empty($mainpage)) {
  478. $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
  479. }
  480. // We must create a new, incrementally assigned profile_id
  481. $profile = new Profile();
  482. $profile->nickname = $nickname;
  483. $profile->fullname = $fullname;
  484. $profile->profileurl = $mainpage;
  485. $profile->homepage = $homepage;
  486. $profile->bio = $description;
  487. $profile->location = $location;
  488. $profile->created = common_sql_now();
  489. $group->nickname = $profile->nickname;
  490. $group->fullname = $profile->fullname;
  491. $group->homepage = $profile->homepage;
  492. $group->description = $profile->bio;
  493. $group->location = $profile->location;
  494. $group->mainpage = $profile->profileurl;
  495. $group->created = $profile->created;
  496. $profile->query('BEGIN');
  497. $id = $profile->insert();
  498. if ($id === false) {
  499. $profile->query('ROLLBACK');
  500. throw new ServerException(_('Profile insertion failed'));
  501. }
  502. $group->profile_id = $id;
  503. $group->uri = $uri;
  504. if (isset($fields['join_policy'])) {
  505. $group->join_policy = intval($fields['join_policy']);
  506. } else {
  507. $group->join_policy = 0;
  508. }
  509. if (isset($fields['force_scope'])) {
  510. $group->force_scope = intval($fields['force_scope']);
  511. } else {
  512. $group->force_scope = 0;
  513. }
  514. if (Event::handle('StartGroupSave', array(&$group))) {
  515. $result = $group->insert();
  516. if ($result === false) {
  517. common_log_db_error($group, 'INSERT', __FILE__);
  518. // TRANS: Server exception thrown when creating a group failed.
  519. throw new ServerException(_('Could not create group.'));
  520. }
  521. if (!isset($uri) || empty($uri)) {
  522. $orig = clone($group);
  523. $group->uri = common_local_url('groupbyid', array('id' => $group->id));
  524. $result = $group->update($orig);
  525. if (!$result) {
  526. common_log_db_error($group, 'UPDATE', __FILE__);
  527. // TRANS: Server exception thrown when updating a group URI failed.
  528. throw new ServerException(_('Could not set group URI.'));
  529. }
  530. }
  531. $result = $group->setAliases($aliases);
  532. if (!$result) {
  533. // TRANS: Server exception thrown when creating group aliases failed.
  534. throw new ServerException(_('Could not create aliases.'));
  535. }
  536. $member = new Group_member();
  537. $member->group_id = $group->id;
  538. $member->profile_id = $userid;
  539. $member->is_admin = 1;
  540. $member->created = $group->created;
  541. $result = $member->insert();
  542. if (!$result) {
  543. common_log_db_error($member, 'INSERT', __FILE__);
  544. // TRANS: Server exception thrown when setting group membership failed.
  545. throw new ServerException(_('Could not set group membership.'));
  546. }
  547. self::blow('profile:groups:%d', $userid);
  548. if ($local) {
  549. $local_group = new Local_group();
  550. $local_group->group_id = $group->id;
  551. $local_group->nickname = $nickname;
  552. $local_group->created = common_sql_now();
  553. $result = $local_group->insert();
  554. if (!$result) {
  555. common_log_db_error($local_group, 'INSERT', __FILE__);
  556. // TRANS: Server exception thrown when saving local group information failed.
  557. throw new ServerException(_('Could not save local group info.'));
  558. }
  559. }
  560. Event::handle('EndGroupSave', array($group));
  561. }
  562. $profile->query('COMMIT');
  563. return $group;
  564. }
  565. /**
  566. * Handle cascading deletion, on the model of notice and profile.
  567. *
  568. * This should handle freeing up cached entries for the group's
  569. * id, nickname, URI, and aliases. There may be other areas that
  570. * are not de-cached in the UI, including the sidebar lists on
  571. * GroupsAction
  572. */
  573. function delete($useWhere=false)
  574. {
  575. if (empty($this->id)) {
  576. common_log(LOG_WARNING, "Ambiguous User_group->delete(); skipping related tables.");
  577. return parent::delete($useWhere);
  578. }
  579. try {
  580. $profile = $this->getProfile();
  581. $profile->delete();
  582. } catch (GroupNoProfileException $unp) {
  583. common_log(LOG_INFO, "Group {$this->nickname} has no profile; continuing deletion.");
  584. }
  585. // Safe to delete in bulk for now
  586. $related = array('Group_inbox',
  587. 'Group_block',
  588. 'Group_member',
  589. 'Related_group');
  590. Event::handle('UserGroupDeleteRelated', array($this, &$related));
  591. foreach ($related as $cls) {
  592. $inst = new $cls();
  593. $inst->group_id = $this->id;
  594. if ($inst->find()) {
  595. while ($inst->fetch()) {
  596. $dup = clone($inst);
  597. $dup->delete();
  598. }
  599. }
  600. }
  601. // And related groups in the other direction...
  602. $inst = new Related_group();
  603. $inst->related_group_id = $this->id;
  604. $inst->delete();
  605. // Aliases and the local_group entry need to be cleared explicitly
  606. // or we'll miss clearing some cache keys; that can make it hard
  607. // to create a new group with one of those names or aliases.
  608. $this->setAliases(array());
  609. // $this->isLocal() but we're using the resulting object
  610. $local = Local_group::getKV('group_id', $this->id);
  611. if ($local instanceof Local_group) {
  612. $local->delete();
  613. }
  614. // blow the cached ids
  615. self::blow('user_group:notice_ids:%d', $this->id);
  616. return parent::delete($useWhere);
  617. }
  618. public function update($dataObject=false)
  619. {
  620. // Whenever the User_group is updated, find the Local_group
  621. // and update its nickname too.
  622. if ($this->nickname != $dataObject->nickname) {
  623. $local = Local_group::getKV('group_id', $this->id);
  624. if ($local instanceof Local_group) {
  625. common_debug("Updating Local_group ({$this->id}) nickname from {$dataObject->nickname} to {$this->nickname}");
  626. $local->setNickname($this->nickname);
  627. }
  628. }
  629. // Also make sure the Profile table is up to date!
  630. $fields = array(/*group field => profile field*/
  631. 'nickname' => 'nickname',
  632. 'fullname' => 'fullname',
  633. 'mainpage' => 'profileurl',
  634. 'homepage' => 'homepage',
  635. 'description' => 'bio',
  636. 'location' => 'location',
  637. 'created' => 'created',
  638. 'modified' => 'modified',
  639. );
  640. $profile = $this->getProfile();
  641. $origpro = clone($profile);
  642. foreach ($fields as $gf=>$pf) {
  643. $profile->$pf = $this->$gf;
  644. }
  645. if ($profile->update($origpro) === false) {
  646. throw new ServerException(_('Unable to update profile'));
  647. }
  648. return parent::update($dataObject);
  649. }
  650. function isPrivate()
  651. {
  652. return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
  653. intval($this->force_scope) === 1);
  654. }
  655. public function isLocal()
  656. {
  657. $local = Local_group::getKV('group_id', $this->id);
  658. return ($local instanceof Local_group);
  659. }
  660. static function groupsFromText($text, Profile $profile)
  661. {
  662. $groups = array();
  663. /* extract all !group */
  664. $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
  665. strtolower($text),
  666. $match);
  667. if (!$count) {
  668. return $groups;
  669. }
  670. foreach (array_unique($match[1]) as $nickname) {
  671. $group = self::getForNickname($nickname, $profile);
  672. if ($group instanceof User_group && $profile->isMember($group)) {
  673. $groups[] = clone($group);
  674. }
  675. }
  676. return $groups;
  677. }
  678. static function idsFromText($text, Profile $profile)
  679. {
  680. $ids = array();
  681. $groups = self::groupsFromText($text, $profile);
  682. foreach ($groups as $group) {
  683. $ids[$group->id] = true;
  684. }
  685. return array_keys($ids);
  686. }
  687. }