1 <?php
namespace Tilmeld\Entities;
50 const ETYPE =
'tilmeld_group';
51 const DEFAULT_PRIVATE_DATA = [
60 'addressInternational',
64 const DEFAULT_WHITELIST_DATA = [];
66 public $clientEnabledMethods = [
75 public static $clientEnabledStaticMethods = [
79 protected $privateData = Group::DEFAULT_PRIVATE_DATA;
80 public static $searchRestrictedData = Group::DEFAULT_PRIVATE_DATA;
81 protected $whitelistData = Group::DEFAULT_WHITELIST_DATA;
82 protected $whitelistTags = [];
90 private $skipAcWhenSaving =
false;
98 if ((is_int($id) && $id > 0) || is_string($id)) {
100 $entity = Nymph::getEntity(
101 [
'class' => get_class($this)],
105 $entity = Nymph::getEntity(
106 [
'class' => get_class($this)],
110 str_replace([
'\\',
'%',
'_'], [
'\\\\\\\\',
'\%',
'\_'], $id)
115 if (isset($entity)) {
116 $this->guid = $entity->guid;
117 $this->tags = $entity->tags;
118 $this->cdate = $entity->cdate;
119 $this->mdate = $entity->mdate;
120 $this->putData($entity->getData(), $entity->getSData());
125 $this->enabled =
true;
126 $this->abilities = [];
127 $this->addressType =
'us';
138 return self::getAssignableGroups(
140 Tilmeld::$config[
'highest_primary']
151 return self::getAssignableGroups(
153 Tilmeld::$config[
'highest_secondary']
157 private static function getAssignableGroups($search, $highestParent) {
158 $assignableGroups = [];
159 if ($search !==
null) {
160 $assignableGroups = Nymph::getEntities(
161 [
'class' =>
'\Tilmeld\Entities\Group'],
163 'equal' => [
'enabled',
true]
168 [
'groupname', $search]
172 if ($highestParent != 0) {
173 $assignableGroups = array_values(
176 function ($curGroup) use ($highestParent) {
177 while (isset($curGroup->parent) && $curGroup->parent->guid) {
178 if ($curGroup->parent->guid === $highestParent) {
181 $curGroup = $curGroup->parent;
189 if ($highestParent == 0) {
190 $assignableGroups = Nymph::getEntities(
191 [
'class' =>
'\Tilmeld\Entities\Group'],
193 'equal' => [
'enabled',
true]
197 if ($highestParent > 0) {
198 $highestParent = Group::factory($highestParent);
199 if (isset($highestParent->guid)) {
200 $assignableGroups = $highestParent->getDescendants();
205 return $assignableGroups;
208 public function getAvatar() {
209 if (isset($this->avatar)) {
210 return $this->avatar;
212 $proto = $_SERVER[
'HTTPS'] ?
'https' :
'http';
213 if (!isset($this->email) || empty($this->email)) {
214 return $proto.
'://secure.gravatar.com/avatar/?d=mm&s=40';
216 return $proto.
'://secure.gravatar.com/avatar/'.
217 md5(strtolower(trim($this->email))).
'?d=identicon&s=40';
220 public function putData($data, $sdata = []) {
221 $return = parent::putData($data, $sdata);
222 $this->updateDataProtection();
235 $user = User::current();
238 $this->privateData = self::DEFAULT_PRIVATE_DATA;
239 $this->whitelistData = self::DEFAULT_WHITELIST_DATA;
241 if (Tilmeld::$config[
'email_usernames']) {
242 $this->privateData[] =
'groupname';
244 if ($user !==
null && $user->gatekeeper(
'tilmeld/admin')) {
246 $this->privateData = [];
247 $this->whitelistData =
false;
250 if (isset($this->user) && isset($user) && $this->user->is($user)) {
252 $this->privateData = [];
263 if (is_numeric($group)) {
264 $group = Group::factory((
int) $group);
266 if (!isset($group->guid)) {
270 if (!isset($this->parent)) {
273 if ($this->parent->is($group)) {
276 if ($this->parent->isDescendant($group)) {
288 $return = (array) Nymph::getEntities(
289 [
'class' =>
'\Tilmeld\Entities\Group'],
291 'equal' => [
'enabled',
true],
292 'ref' => [
'parent', $this]
306 $entities = Nymph::getEntities(
307 [
'class' =>
'\Tilmeld\Entities\Group'],
309 'equal' => [
'enabled',
true],
310 'ref' => [
'parent', $this]
313 foreach ($entities as $entity) {
314 $childArray = $entity->getDescendants(
true);
315 $return = array_merge($return, $childArray);
318 if (class_exists(
'\SciActive\Hook')) {
319 $class = get_class();
320 \SciActive\Hook::hookObject($hooked, $class.
'->',
false);
340 while (isset($group->parent) && $group->parent->enabled) {
342 $group = $group->parent;
356 $descendants =
false,
362 $groups = $this->getDescendants();
365 $options = [
'class' =>
'\Tilmeld\Entities\User'];
367 $options[
'limit'] = $limit;
369 if (isset($offset)) {
370 $options[
'offset'] = $offset;
372 $return = Nymph::getEntities(
375 'equal' => [
'enabled',
true]
396 if (Tilmeld::$config[
'email_usernames']
397 && $this->groupname === $this->email
399 return $this->checkEmail();
401 if (empty($this->groupname)) {
402 return [
'result' =>
false,
'message' =>
'Please specify a groupname.'];
404 if (Tilmeld::$config[
'max_username_length'] > 0
405 && strlen($this->groupname) > Tilmeld::$config[
'max_username_length']
409 'message' =>
'Groupnames must not exceed '.
410 Tilmeld::$config[
'max_username_length'].
' characters.' 414 str_split($this->groupname),
415 str_split(Tilmeld::$config[
'valid_chars'])
419 'message' => Tilmeld::$config[
'valid_chars_notice']
422 if (!preg_match(Tilmeld::$config[
'valid_regex'], $this->groupname)) {
425 'message' => Tilmeld::$config[
'valid_regex_notice']
433 [
'\\\\\\\\',
'\%',
'\_'],
438 if (isset($this->guid)) {
439 $selector[
'!guid'] = $this->guid;
441 $test = Nymph::getEntity(
442 [
'class' =>
'\Tilmeld\Entities\Group',
'skip_ac' =>
true],
445 if (isset($test->guid)) {
446 return [
'result' =>
false,
'message' =>
'That groupname is taken.'];
452 isset($this->guid) ?
'Groupname is valid.' :
'Groupname is available!' 464 if ($this->email ===
'') {
465 return [
'result' =>
true,
'message' =>
''];
467 if (empty($this->email)) {
468 return [
'result' =>
false,
'message' =>
'Please specify a valid email.'];
471 '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',
476 'message' =>
'Email must be a correctly formatted address.' 482 str_replace([
'\\',
'%',
'_'], [
'\\\\\\\\',
'\%',
'\_'], $this->email)
485 if (isset($this->guid)) {
486 $selector[
'!guid'] = $this->guid;
488 $test = Nymph::getEntity(
489 [
'class' =>
'\Tilmeld\Entities\Group',
'skip_ac' =>
true],
492 if (isset($test->guid)) {
495 'message' =>
'That email address is already registered.' 502 isset($this->guid) ?
'Email is valid.' :
'Email address is valid!' 507 public function save() {
508 if (!isset($this->groupname)) {
513 $this->groupname = trim($this->groupname);
514 $this->email = trim($this->email);
515 $this->name = trim($this->name);
516 $this->phone = trim($this->phone);
519 $unCheck = $this->checkGroupname();
520 if (!$unCheck[
'result']) {
521 throw new \Tilmeld\Exceptions\BadUsernameException($unCheck[
'message']);
523 if (!(Tilmeld::$config[
'email_usernames']
524 && $this->groupname === $this->email)
526 $emCheck = $this->checkEmail();
527 if (!$emCheck[
'result']) {
528 throw new \Tilmeld\Exceptions\BadEmailException($emCheck[
'message']);
533 if (isset($this->parent) &&
535 !isset($this->parent->guid) ||
536 $this->is($this->parent) ||
537 $this->parent->isDescendant($this)
540 throw new \Tilmeld\Exceptions\BadDataException(
541 'Group parent can\'t be itself or descendant of itself.' 546 Tilmeld::$config[
'validator_group']->assert($this->getValidatable());
548 }
catch (\Respect\Validation\Exceptions\NestedValidationException $exception) {
549 throw new \Tilmeld\Exceptions\BadDataException(
550 $exception->getFullMessage()
555 if ($this->defaultPrimary) {
556 $currentPrimary = Nymph::getEntity(
557 [
'class' =>
'\Tilmeld\Entities\Group'],
558 [
'&',
'equal' => [
'defaultPrimary',
true]]
560 if (isset($currentPrimary) && !$this->is($currentPrimary)) {
561 $currentPrimary->defaultPrimary =
false;
562 if (!$currentPrimary->save()) {
564 throw new \Tilmeld\Exceptions\CouldNotChangeDefaultPrimaryGroupException(
565 "Could not change new user primary group from ".
566 "{$currentPrimary->groupname}." 572 return parent::save();
578 public function saveSkipAC() {
579 $this->skipAcWhenSaving =
true;
580 return $this->save();
583 public function tilmeldSaveSkipAC() {
584 if ($this->skipAcWhenSaving) {
585 $this->skipAcWhenSaving =
false;
591 public function delete() {
595 $entities = Nymph::getEntities(
596 [
'class' =>
'\Tilmeld\Entities\Group'],
598 'ref' => [
'parent', $this]
601 foreach ($entities as $curGroup) {
602 if (!$curGroup->delete()) {
606 return parent::delete();
getUsers( $descendants=false, $limit=null, $offset=null)
Gets an array of users in the group.
checkGroupname()
Check that a groupname is valid.
getChildren()
Gets an array of the group's child groups.
static gatekeeper($ability=null)
Check to see if the current user has an ability.
checkEmail()
Check that an email is unique.
getDescendants($andSelf=false)
Gets an array of the group's descendant groups.
__construct($id=0)
Load a group.
static getPrimaryGroups($search=null)
Get all the groups that can be assigned as primary groups.
static getSecondaryGroups($search=null)
Get all the groups that can be assigned as secondary groups.
isDescendant($group=null)
Check whether the group is a descendant of a group.
updateDataProtection($user=null)
Update the data protection arrays for a user.
getLevel()
Get the number of parents the group has.