Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenJoynt committed Aug 24, 2021
1 parent 97df35d commit 077b89b
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace sij\humhub\modules\sponsor;

use Yii;
use yii\helpers\Url;
use sij\humhub\modules\sponsor\widgets\Sponsors;
use sij\humhub\modules\sponsor\models\Sponsor;

class Events
{

public static function onProfileSidebarInit($event)
{
try {
if ( Yii::$app->user->isGuest ) return;
$user = $event->sender->user;
if ( ! $user ) return;
$event->sender->addWidget(
Sponsors::class,
['user' => $user],
['sortOrder' => 500]
);
} catch ( \Throwable $e ) {
Yii::error($e);
}
}

public static function onAfterInsertInvite($event)
{
try {
$identity = Yii::$app->user->getIdentity();
$sponsor = new Sponsor();
$sponsor->invited_email = strtolower(trim($event->sender->email));
$sponsor->created_by = $identity->id;
$sponsor->created_name = $identity->displayName;
$sponsor->created_email = strtolower(trim($identity->email));
$sponsor->save();
} catch ( \Throwable $e ) {
Yii::error($e);
}
}

public static function onAfterRegistration($event)
{
$identity = $event->identity;
$email = strtolower(trim($identity->email));
$user_id = $identity->id;
Sponsor::updateInvitation($email, $user_id);
}

}
19 changes: 19 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace sij\humhub\modules\sponsor;

use Yii;

class Module extends \humhub\components\Module
{

/**
* @inheritdoc
*/
public function disable()
{
// Cleanup all module data, don't remove the parent::disable()!!!
parent::disable();
}

}
Binary file added assets/module_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use sij\humhub\modules\sponsor\Events;
use humhub\modules\user\widgets\ProfileSidebar;
use humhub\modules\user\models\Invite;
use humhub\modules\user\models\forms\Registration;

return [
'id' => 'sponsor',
'class' => 'sij\humhub\modules\sponsor\Module',
'namespace' => 'sij\humhub\modules\sponsor',
'events' => [
[
'class' => ProfileSidebar::class,
'event' => ProfileSidebar::EVENT_INIT,
'callback' => [Events::class, 'onProfileSidebarInit']
],
[
'class' => Invite::class,
'event' => Invite::EVENT_AFTER_INSERT,
'callback' => [Events::class, 'onAfterInsertInvite']
],
[
'class' => Registration::class,
'event' => Registration::EVENT_AFTER_REGISTRATION,
'callback' => [Events::class, 'onAfterRegistration']
],

],
];
31 changes: 31 additions & 0 deletions migrations/m201014_165727_initial.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use yii\db\Migration;

class m201014_165727_initial extends Migration
{

public function up()
{
if ( $this->getDb()->getTableSchema('sponsor', true) === null ) {
$this->createTable('sponsor', [
'id' => 'pk',
'invited_id' => 'int(11) DEFAULT NULL',
'invited_email' => 'text NOT NULL',
'created_by' => 'int(11) NOT NULL',
'created_name' => 'text NOT NULL',
'created_email' => 'text NOT NULL',
]);
$this->createIndex('idx_sponsor_invited_id', 'sponsor', 'invited_id', false);
$this->createIndex('idx_sponsor_created_by', 'sponsor', 'created_by', false);
$this->addForeignKey('fk_sponsor_invited_id', 'sponsor', 'invited_id', 'user', 'id', 'CASCADE');
}
}

public function down()
{
echo "m201014_165727_initial cannot be reverted.\n";
return false;
}

}
20 changes: 20 additions & 0 deletions migrations/m210211_114019_lowercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use yii\db\Migration;

class m210211_114019_lowercase extends Migration
{

public function up()
{
$this->execute('UPDATE sponsor SET invited_email = LOWER(invited_email)');
$this->execute('UPDATE sponsor SET created_email = LOWER(created_email)');
}

public function down()
{
echo "m210211_114019_lowercase cannot be reverted.\n";
return false;
}

}
19 changes: 19 additions & 0 deletions migrations/uninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use yii\db\Migration;

class uninstall extends Migration
{

public function up()
{
// $this->dropTable('sponsor');
}

public function down()
{
echo "uninstall does not support migration down.\n";
return false;
}

}
66 changes: 66 additions & 0 deletions models/Sponsor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace sij\humhub\modules\sponsor\models;

use humhub\modules\user\models\User;

class Sponsor extends \humhub\components\ActiveRecord
{

public static function tableName()
{
return 'sponsor';
}

public function rules()
{
return [
[['invited_email', 'created_name', 'created_email'], 'required'],
[['invited_id', 'created_by'], 'integer'],
[['created_email'], 'email'],
];
}

public function attributeLabels()
{
return [
'id' => 'ID',
'invited_id' => 'Invited User ID',
'invited_email' => 'Invited User Email Address',
'created_by' => 'Invited By User ID',
'created_name' => 'Invited By User Name',
'created_email' => 'Invited By User Email Address',
];
}

public function getInvited()
{
return $this->hasOne(User::className(), ['id' => 'invited_id']);
}

public function getInviter()
{
return $this->hasOne(User::className(), ['id' => 'created_by']);
}

public static function findInvited($user_id) {
return Sponsor::find()
->where(['sponsor.created_by' => $user_id])
->all();
}

public static function findInviter($user_id) {
return Sponsor::find()
->where(['sponsor.invited_id' => $user_id])
->all();
}

public static function updateInvitation($email, $user_id) {
$items = Sponsor::find()->where(['invited_email' => strtolower(trim($email))])->all();
foreach ( $items as $item ) {
$item->invited_id = $user_id;
$item->update(false);
}
}

}
12 changes: 12 additions & 0 deletions module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "sponsor",
"name": "Sponsor",
"description": "Tracks which members invited others to register at the web site.",
"keywords": [
],
"version": "0.2",
"humhub": {
"minVersion": "1.2"
},
"screenshots": []
}
61 changes: 61 additions & 0 deletions widgets/Sponsors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace sij\humhub\modules\sponsor\widgets;

use Yii;
use humhub\components\Widget;
use humhub\libs\ProfileImage;
use sij\humhub\modules\sponsor\models\Sponsor;

/**
* Sponsors shows those invited by this user,
* and who invited this user, in sidebar.
*/
class Sponsors extends Widget
{

public $user;

private function getProfileImage($user) {
$cfg = [ 'showTooltip' => true ];
$img = new ProfileImage($user->guid);
return $img->render(24, $cfg) . "\n";
}

public function run()
{

$mySponsor = "";
foreach ( Sponsor::findInviter($this->user->id) as $inviter ) {
$user = $inviter->inviter;
if ( $user ) {
$mySponsor .= $this->getProfileImage($user);
} else {
$mySponsor .=
"[<a href=\"mailto:" . $inviter->created_email . "\">" .
htmlentities($inviter->created_name) .
"</a>]\n";
}
}

$iSponsor = "";
foreach ( Sponsor::findInvited($this->user->id) as $invited ) {
$user = $invited->invited;
if ( $user ) {
$iSponsor .= $this->getProfileImage($user);
}
}

if ( empty($mySponsor) && empty($iSponsor) ) return;

return $this->render(
'sponsorPanel',
[
'mySponsor' => $mySponsor,
'iSponsor' => $iSponsor,
]
);

}

}
27 changes: 27 additions & 0 deletions widgets/views/sponsorPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div class="panel">

<div class="panel panel-default sponsors" id="profile-sponsors-panel">
<?php echo \humhub\widgets\PanelMenu::widget(['id' => 'profile-sponsors-panel']); ?>


<div class="panel-heading">
<strong>Sponsors</strong>
</div>

<?php if ( ! empty($mySponsor) ) : ?>
<div class="panel-body">
My sponsor is<br>
<?= $mySponsor ?>
</div>
<?php endif; ?>

<?php if ( ! empty($iSponsor) ) : ?>
<div class="panel-body">
I sponsor these...<br>
<?= $iSponsor ?>
</div>
<?php endif; ?>

</div>

</div>

0 comments on commit 077b89b

Please sign in to comment.