garden-globalnotify / default.php

<?php if (!defined('APPLICATION')) exit();

$PluginInfo['GlobalNotify'] = array(
	'Name' => 'GlobalNotify',
	'Description' => 'This notifies everybody about all posts. Currently XMPP only.',
	'Version' => '1.0.1',
	'Author' => "Christian Kaula",
	'AuthorEmail' => 'ck@10tok.net',
	'AuthorUrl' => 'http://christiankaula.com',
	'RequiredApplications' => false,
	'RequiredPlugins' => false,
	'HasLocale' => false,
	'RegisterPermissions' => false
);


include 'XMPPHP/XMPP.php';


class GlobalNotifyPlugin extends Gdn_Plugin {

	protected function GetJIDs() {
		$jids = array();
		$DataSet = Gdn::SQL()
			->Select('u1.Value')
			->Distinct()
			->From('UserMeta u1')
			->Join('UserMeta u2', 'u1.UserID = u2.UserID')
			->Where('u1.Name', 'Plugin.GlobalNotify.JID')
			->Where('u1.Value <>', '')
			->Where('u2.Name', 'Plugin.GlobalNotify.Disable')
			->Where('u2.Value <>', 'TRUE')
			->Get();
		foreach ($DataSet->Result() as $row) {
			$jids[] = $row->Value;
		}

		return $jids;
	}

	protected function XMPPNotify($message) {
		$jids = $this->GetJIDs();
		
		$conn = new XMPPHP_XMPP(
			C('Plugins.GlobalNotify.XMPP.Host'),
			C('Plugins.GlobalNotify.XMPP.Port', 5222),
			C('Plugins.GlobalNotify.XMPP.User'),
			C('Plugins.GlobalNotify.XMPP.Pass'),
			C('Plugins.GlobalNotify.XMPP.Resource', 'xmppphp')
		);
		try {
			$conn->connect();
			$conn->processUntil('session_start');
			$conn->presence();
			foreach ($jids as $jid)
				$conn->message($jid, $message);
			$conn->disconnect();
		} catch(XMPPHP_Exception $e) {
			error_log(($e->getMessage()));
		}
	}

	public function PostController_AfterCommentSave_Handler(&$Sender) {
		$Comment = $Sender->EventArguments['Comment'];
		$Discussion = $Sender->EventArguments['Discussion'];
		
		if ($Comment->Body == '')
			return;
		
		if ($Comment->DateUpdated == null) {
			$this->XMPPNotify(
				$Comment->InsertName
				. ' posted in "' . $Discussion->Name . '"' . ":\n"
				. $Comment->Body
			);
		}
	}

	public function PostController_AfterDiscussionSave_Handler(&$Sender) {
		$Discussion = $Sender->EventArguments['Discussion'];
		$this->XMPPNotify(
			$Discussion->InsertName . ' started "'
			. $Discussion->Name . '":\n' . $Discussion->Body
		);
	}

	public function ProfileController_AfterAddSideMenu_Handler(&$Sender) {
		$SideMenu = $Sender->EventArguments['SideMenu'];
		$Session = Gdn::Session();
		$ViewingUserID = $Session->UserID;

		if ($Sender->User->UserID == $ViewingUserID) {
			$SideMenu->AddLink(
				'Options', T('Global Notify Settings'),
				'/profile/globalnotify/', FALSE, array('class' => 'Popup')
			);
		}
	}

	public function ProfileController_GlobalNotify_Create(&$Sender) {
		$UserID = Gdn::Session()->UserID;
		$UserMeta = $this->GetUserMeta($UserID, '%');

		$Validation = new Gdn_Validation();
		$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
		$ConfigArray = array(
			'Plugin.GlobalNotify.Disable' => null,
			'Plugin.GlobalNotify.JID' => null
		);
		$ConfigurationModel->SetField($ConfigArray);

		if ($Sender->Form->AuthenticatedPostBack() === false && is_array($UserMeta)) {
			$ConfigArray = array_merge($ConfigArray, $UserMeta);
		}
		$ConfigurationModel->SetField($ConfigArray);
		$Sender->Form->SetModel($ConfigurationModel);

		if ($Sender->Form->AuthenticatedPostBack() === false) {
			$Sender->Form->SetData($ConfigurationModel->Data);
		} else {
			$Values = $Sender->Form->FormValues();
			$FormValues = array_intersect_key($Values, $ConfigArray);
			$IsValid = $Sender->Form->ValidateRule(
				'Plugin.GlobalNotify.JID', 'function:ValidateEmail'
			);
			$IsValid = $IsValid && $Sender->Form->ValidateRule(
				'Plugin.GlobalNotify.Disable', 'function:ValidateBoolean'
			);
			if ($IsValid && sizeof($FormValues)) {
				foreach ($FormValues as $UserMetaKey => $UserMetaValue) {
					$this->SetUserMeta(
						$UserID, $this->TrimMetaKey($UserMetaKey), $UserMetaValue
					);
				}
				$Sender->StatusMessage = T('Your changes have been saved.');
			}
		}
		$Sender->Render($this->GetView('globalnotify.php'));
	}

	public function Setup() {
		// noop
	}

	public function Structure() {
		// noop
	}

}

?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.