I did this a little while agousing the template and a snippet. The snippet was taken from NewsEditor snippet and it works on every page view (lame I know). If a user gallery isn’t found then one is created using the users Username.
Create Template Variable:
Maxigallery Code:
[!MaxiGallery? &admin_webusers=`[*WebUserOwner*]` &manager_webusers=`[*WebUserOwner*]` !]
Note the use of the template variable. Don’t forget the rest of your Maxigallery parameters the above is just a brief example.
Create new snippet called NewGallery with code below:
<?php
/*
// C Hunton 22/1/09
// NewGallery
// This code should create a modx document in the relevant gallery and generate the TV's to associate it as a user gallery
*/
// Default parameters required - Change where applicable
// To find your template varaiable number goto Template Variable page in the manager.
// Hover over your template variable and you'll notice: example: /manager/index.php?id=2&a=301
// The id= is your template variable number
$gallery = isset($gallery) ? $gallery: "8"; // This is the document where you user gallery is stored
$tmplvar = isset($tmplvar) ? $tmplvar: "2"; // This is the TemplateVariable where the Username is stored.
$folder = $gallery;
$clearcache = "1";
// get template
$template = isset($template) ? $modx->db->getValue('SELECT id FROM '.$modx->getFullTableName('site_templates').' WHERE templatename=\''.mysql_escape_string($template).'\'') : $modx->config['default_template'];
// get menu status
$hidemenu = 1;
// get created date
$createdon = time();
// set menu index
$mnuidx = $modx->db->getValue('SELECT MAX(menuindex)+1 as \'mnuidx\' FROM '.$modx->getFullTableName('site_content').' WHERE parent=\''.$folder.'\'');
if($mnuidx<1) $mnuidx = 0;
$user = $modx->getWebUserInfo($modx->getLoginUserID());
$userid = $modx->getLoginUserID();
// Username valid check
if (!$user['username']) {
echo "You must log in to create your gallery.";
return;
}
// Make the alias for the document
// remove any unnecessary tags
$aliasname = $modx->stripTags($user['username']);
$aliasname = strtolower($aliasname);
$aliasname = preg_replace('/&.+?;/', '', $aliasname); // kill entities
$aliasname = preg_replace('/[^\.%a-z0-9 _-]/', '', $aliasname);
$aliasname = preg_replace('/\s+/', '-', $aliasname);
$aliasname = preg_replace('|-+|', '-', $aliasname);
$aliasname = trim($aliasname, '-');
$aliasname = $modx->db->escape($aliasname);
$aliasname = str_replace('.', '', $aliasname); // remove any .'s just incase
$alias = $aliasname;
$aliasname = $aliasname."-gallery";
// Make the title for the document
$title = $alias."'s Gallery";
// Check if the user already has a gallery
$rs = $modx->db->query("SELECT *
FROM ".$modx->getFullTableName('site_tmplvar_contentvalues')."
WHERE tmplvarid = '".$tmplvar."' AND value = '".$user['username']."'");
$count = $modx->db->getRecordCount($rs);
$row = mysql_fetch_array($rs);
if ($count >= 1) {
echo 'Your <a href="'.$aliasname.'" title="Your Gallery">Gallery</a> is Ready!';
return;
}
$published= 1;
$pub_date = 0;
$unpub_date = 0;
$flds = array(
'pagetitle' => mysql_escape_string($modx->stripTags($title)),
'longtitle' => mysql_escape_string($modx->stripTags($title)),
'description' => mysql_escape_string($modx->stripTags($title)),
'introtext' => mysql_escape_string($modx->stripTags($title)),
'alias' => $aliasname,
'parent' => $folder,
'createdon' => $createdon,
'createdby' => ($userid>0 ? $userid * -1:0),
'editedon' => '0',
'editedby' => '0',
'published' => $published,
'pub_date' => $pub_date,
'unpub_date' => $unpub_date,
'deleted' => '0',
'hidemenu' => $hidemenu,
'menuindex' => $mnuidx,
'template' => $template,
'cacheable' => '0',
'content' => "<p>{{Screenshots}}</p>"
);
$redirectid = $modx->db->insert($flds,$modx->getFullTableName('site_content'));
echo "Gallery Created!<br />";
$lastInsertId = $modx->db->getInsertId();
$flds = array(
'tmplvarid' => $tmplvar,
'contentid' => $lastInsertId,
'value' => $user['username']
);
$tmpInsert = $modx->db->insert($flds,$modx->getFullTableName('site_tmplvar_contentvalues'));
echo 'Your <a href="'.$aliasname.'" title="Your Gallery">Gallery</a> is ready!';
// empty cache
if($clearcache == 1) {
include_once $modx->config['base_path']."manager/processors/cache_sync.class.processor.php";
$sync = new synccache();
$sync->setCachepath("assets/cache/");
$sync->setReport(false);
$sync->emptyCache(); // first empty the cache
}
// Get doc groups based on $folder (parent id)
$parentDocGroupsSql = "SELECT * FROM " . $modx->getFullTableName('document_groups'). " where document=".$folder;
$parentDocGroupsRs = $modx->db->query($parentDocGroupsSql);
$parentDocGroupsLimit = $modx->db->getRecordCount($parentDocGroupsRs);
for ($pdgi = 0; $pdgi < $parentDocGroupsLimit; $pdgi++) {
$currentDocGroup = $modx->db->getRow($parentDocGroupsRs);
$parentDocGroupsArray[$pdgi] = $currentDocGroup['document_group'];
}
// put the document in the document_groups it should be in
// check that up_perms are switched on!
if($modx->config['use_udperms']==1) {
if(is_array($parentDocGroupsArray)) {
foreach ($parentDocGroupsArray as $dgKey=>$dgValue) {
$insertDocSql = "INSERT INTO ".$modx->getFullTableName('document_groups')."(document_group, document) values(".stripslashes($dgValue).", $lastInsertId)";
$insertDocRs = $modx->db->query($insertDocSql);
if(!$insertDocRs){
exit;
}
} // end foreach
} // end if doc group array exists
} // end if perms are used
// empty cache
if($clearcache == 1) {
include_once $modx->config['base_path']."manager/processors/cache_sync.class.processor.php";
$sync = new synccache();
$sync->setCachepath("assets/cache/");
$sync->setReport(false);
$sync->emptyCache(); // first empty the cache
}
?>
Add snippet into your template (close to webloginpe snippet call is a good idea) with:
If a user is not logged in there will be a message saying:
You must log in to create your gallery
When you first log in a new document resource is created with the Template Variable of the username.
There is one caveat that I have yet to figure out. WebLoginPE isn’t bothered about username case sensitivity when logging in but Maxigallery is. Therefore if I were to login using "bbloke" but my actual username and Template Variable for my Galley is "BBloke" then it won’t show me the "manage pictures" buttons.
I have it working
here. The login in fields are at the top left hand corner.
EDIT: amended snippet details and where to put the snippet call in the template.