📜 ⬆️ ⬇️

Developing a social network on MODx Revolution

MODx is developing at a rapid pace, and I decided to try to make a social network on this wonderful engine. After all, it has long been possible to work with users (registration, authorization, etc.), the built-in mechanism of user messages , he knows how to work with images and much more.

To begin, let's deal with users, give them the opportunity to write articles, for example, or add their own events.

User registration


For registration, we will use the Login snippet.
')
[[!Register? &postHooks=`addUser` &usergroups=`Users` &submittedResourceId=`9` &activationResourceId=`11` &activationEmailSubject=`   ` &validate=`email:email:required`]] 

Much has already been written about the snippet, I’ll show what our addUser hook does :

AddUser snippet
AddUser snippet

 <?php $output = ""; $parent = 2; $template = 3; //     (  ) if ($id = $modx->runSnippet('addResource', array( 'pagetitle' => $hook->getValue('username'), 'parent' => $parent, 'template' => $template, 'isfolder' => 1, 'published' => 1, 'hidemenu' => 1 ))) { //  id     website // (     User Extended) $userId = $hook->getValue('register.user')->get('id'); $user = $modx->getObject('modUserProfile', array('id' => $userId)); $user->set('website',$id); $user->save(); //       TV-,   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>E-mail: <a href="mailto:admin@site.ru">admin@site.ru</a></p>'; die(); } return $output; 


Snippet addResource
Snippet addResource

 <?php //        POST 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('Ymd H:i:s'); //      , //      if (!$parent) $parent = 1; if (!$template) $template = 1; //     switch ($template) { case 1: $prefix = "art"; break; //  case 2: $prefix = "id"; break; //  case 3: $prefix = "id"; break; //  case 4: $prefix = "event"; break; //  case 5: $prefix = "animal"; break; //   default: $prefix = "id"; break; } //   $newResource = $modx->newObject('modResource'); //    $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); //   if ($newResource->save()) { $id = $newResource->get('id'); $newResource->set('alias',$prefix.$id); $newResource->save(); //  ,      $modx->cacheManager->refresh(); return $id; } return false; 


Snippet tv
Snippet tv

 <?php if ($input) { //    TV  id  $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 { //    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))) { $tvar = $modx->getObject('modTemplateVarResource', array('contentid' => $id, 'tmplvarid' => $tv)); } else { $tvar = $modx->newObject('modTemplateVarResource'); $tvar->set('contentid', $id); $tvar->set('tmplvarid', $tv); } $tvar->set('value', $value); $tvar->save(); } 



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

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

Respectively, the editUserPage snippet
Snippet editUserPage
Snippet editUserPage

 <?php //  id     $id = $modx->user->getOne('Profile')->get('website'); $name = $_POST['name']; if ($name == '') $name = ' '; //      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)); } //  ,      $modx->cacheManager->refresh(); return true; } 



Create articles or user events


Let our users can add upcoming and past events to their page:

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


AddEvent snippet
AddEvent snippet

 <?php //       $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',array('pagetitle' => $name, 'longtitle' => $name, 'introtext' => $introtext, 'menutitle' => $name, 'published' => 1, 'publishedon' => $publishedon, 'parent' => $parent, 'template' => 4, 'isfolder' => 1))) { //    ,    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(); //     $url = "/companies/id".$parent."/event".$id; header('location: '.$url); exit; return true; } 



Eventually

On our site, users can register, post information about themselves, create events or write articles.

What do I plan to do with it

Next, I plan to make it possible for users to upload photos, add functionality for sending and reading private messages, as Goobs wrote about this, as well as the ability of users to register for favorite events.

Source: https://habr.com/ru/post/148876/


All Articles