Developed a social network for MODx Revolution

MODx is developing rapidly, and I decided to try to make a social network on this wonderful engine. After all, it has long been have the ability work with users (registration, authorization, etc.), a built-in mechanism user messages he working with images and a lot more.

Let's start with the users, will give them the opportunity to write articles, for example, or to add your event.

user Registration


To sign up, we'll use the snippet Login.

the
[[!Register? &postHooks=`addUser` &usergroups=`Users` &submittedResourceId=`9` &activationResourceId=`11` &activationEmailSubject=`Confirm registration` &validate=`email:email:required`]]

About the snippet written already a lot to show you what makes our hook addUser:

Snippet addUser
Snippet addUser

the
<?php
$output = "";
$parent = 2;
$template = 3;

// Create the resource to the user (his personal page)

if ($id = $modx- > runSnippet('addresource method', array(
'pagetitle' => $hook->getValue('username'),
'parent' => $parent,
'template' = > $template,
'isfolder' = > 1,
'published' => 1,
'hidemenu' => 1
))) {

// Writable id of the page user in the box website
// (although you can use User Extended)

$userId = $hook->getValue('register.user')- > get('id');
$user = $modx- > getObject('modUserProfile', array('id' => $userId));
$user->set('website',$id);
$user->save();

// And for the resource to create a TV-parameter, where you specify the user id

$modx- > runSnippet('tv', array('id' => $id, 'tv' => 1, 'value' => $userId)); 

$modx- > cacheManager- > refresh();

$output = $id;

} else {

print '<h1>Can not to create the new user</h1><p>Please,  tell  us about this</p><h2>Contacts</h2><p>email: <a href="mailto:admin@site.ru">admin@site.ru</a></p>';
die();

}

return $output;


addresource method Snippet
Snippet addresource method

the
<?php
//  Snippet  to get data from form POST method
if (!$pagetitle) $pagetitle = $_POST['title'];
if (!$longtitle) $longtitle = $_POST['longtitle'];
if (!$content) $content = $_POST['content'];
if (!$introtext) $introtext = $_POST['introtext'];
if (!$description) $description = $_POST['description'];
if (!$parent) $parent = $_POST['parent'];
if (!$isfolder) $isfolder = $_POST['isfolder'];
if (!$hidemenu) $hidemenu = $_POST['hidemenu'];
if (!$template) $template = $_POST['template'];
if (!$menutitle) $menutitle = $_POST['menutitle'];
if (!$published) $published = $_POST['published'];
if (!$publishedon) $publishedon = date('Y-m-d H:i:s');

// If some values are not specified,
// they will be installed by default
if (!$parent) $parent = 1;
if (!$template) $template = 1;

// Set the prefix for the alias
switch ($template) {
case 1: $prefix = "art"; break; // Article
case 2: $prefix = "id"; break; // User
case 3: $prefix = "id"; break; // User
case 4: $prefix = "event"; break; // Event
case 5: $prefix = "animal"; break; // pet
default: $prefix = "id"; break;
}

// Create the resource
$newResource = $modx- > newObject('modResource');

// Fill in the desired values
$newResource- > set('pagetitle',$pagetitle);
$newResource- > set('longtitle',$longtitle);
$newResource- > set('description',$description);
$newResource- > set('introtext',$introtext);
$newResource- > set('content',$content);
$newResource- > set('menutitle',$menutitle);
$newResource- > set('template',$template);
$newResource- > set('isfolder',$isfolder);
$newResource- > set('hidemenu',$hidemenu);
$newResource- > set('parent',$parent);
$newResource- > set('published',$published);
$newResource- > set('alias',$parent.date('YmdHis'));
if ($published) $newResource- > set('publishedon',$publishedon);


// Save the resource
if ($newResource- > save()) {
$id = $newResource- > get('id');
$newResource- > set('alias',$prefix.$id);
$newResource- > save();
// Clear cache so the changes were immediately visible
$modx- > cacheManager- > refresh();
return $id;
} 

return false;


Snippet tv
Snippet tv

the
<?php
if ($input) {
// Either display the value of TV by id document
$q = $modx- > newQuery('modTemplateVarResource');
$q- > select(array('value',
'tmplvarid',
'contentid'))- > where(array('tmplvarid'= > $options,
'contentid'=>$input)); 
$r = $modx- > getObject('modTemplateVarResource', $q);

return $r->value;

} else {
// either  set  to TV
if (!$id) {$id = $_POST['id'];}
if (!$tv) $tv = $_POST['tv'];
if (!$value) $value = $_POST['value'];
if ($value == ' ') $value = ";

if ($modx- > getObject('modTemplateVarResource', array('contentid' => $id, 'tmplvarid' = > $tv))) {
$its shapes = $modx- > getObject('modTemplateVarResource', array('contentid' => $id, 'tmplvarid' = > $tv));
} else {
$its shapes = $modx- > newObject('modTemplateVarResource');
$its shapes->set('contentid', $id);

}

$its shapes->set('value', $value);

$its shapes->save();

}



After the user confirmed his e-mail, send it to the page to edit information about yourself:

the
[[!FormIt? &hooks=`editUserPage` &successMessage=`saved Changes`]]
[[$editPage]]

Accordingly, the snippet editUserPage
Snippet editUserPage
Snippet editUserPage

the
<?php
// Get the id of the resource from the user profile
$id = $modx->user->getOne('Profile')->get('website');
$name = $_POST['name'];
if ($name == ") $name = ' ';

// And changes made on the page
if ($modx- > runSnippet('editResource',array('resId' => $id, 'pagetitle' => $name, 'longtitle' => $name, 'menutitle' => $name, 'hidemenu' => 'show'))) {
foreach ($_POST as $key => $value) {
switch ($key) {
case 'com_address': $tv = 2; break;
case 'com_egrul': $tv = 5; break;
case 'com_email': $tv = 9; break;
case 'com_federate': $tv = 4; break;
case 'com_head': $tv = 10; break;
case 'com_inn': $tv = 6; break;
case 'com_kpp': $tv = 7; break;
case 'com_phone': $tv = 3; break;
case 'com_regon': $tv = 8; break;
default: $tv = 0; break;
}
if (!$value) $value = ' ';
if ($tv) $modx- > runSnippet('tv', array('id' => $id, 'tv' => $tv, 'value' => $value));
}
// Clear cache so the changes were immediately visible
$modx- > cacheManager- > refresh();
return true;
}



Creating the article or user events


Let us users can add to your page of upcoming and past events:

the
[[!FormIt? &hooks=`addEvent` &successMessage=`saved Changes` &submitVar=`addEvent`]]
[[$addEventForm]]


Snippet addEvent
Snippet addEvent

the
<?php
// Events will be child resources of the user's page
$parent = $modx->user->getOne('Profile')->get('website');
$name = $_POST['name'];
$date = explode('.',$_POST['event_date']);
$publishedon = $date[2]."-".$date[1]."-".$date[0]." 10:00:00";
$introtext = $_POST['description'];
if ($id = $modx- > runSnippet('addresource method',array('pagetitle' => $name, 'longtitle' => $name, 'introtext' = > $introtext, 'menutitle' => $name, 'published' = > 1, 'publishedon' => $publishedon, 'parent' => $parent, 'template' => 4, 'isfolder' = > 1))) {

// Fill in any parameters of the event that we need
foreach ($_POST as $key => $value) {
switch ($key) {
case 'event_address': $tv = 14; break;
case 'event_type': $tv = 12; break;
case 'event_org': $tv = 34; break;
case 'event_peoples': $tv = 24; break;
default: $tv = 0; break;
}
if (!$value) $value = ' ';
if ($tv) $modx- > runSnippet('tv', array('id' => $id, 'tv' => $tv, 'value' => $value));
}

$modx- > cacheManager- > refresh();

// Redirect to view events
$url = "/companies/id".$parent."/event".$id;
header('location: '.$url); exit;
return true;
}



eventually

On our website users can register, post information about yourself, create events or write articles.

What I plan to do with it

Next I plan to make the ability for users to upload photos, add functionality for sending and reading private messages, as wrote about Goobs, as well as the ability for users to register for any events.
Article based on information from habrahabr.ru

Популярные сообщения из этого блога

Approval of WSUS updates: import, export, copy

The Hilbert curve vs. Z-order

Kaspersky Security Center — the fight for automation