Hey all, I've been searching the forums for around a week no with little success. What I'm trying to do seems quite common, although I can't seem to get it working.
I'm trying to pass an external user ID (from a 3rd party php application), and have gallery check that ID and log the user in, if it exists.
If it doesn't exist I want G2 to create it then log in.
Here's my code so far... (in a file called main_wrapper.php in the gallery2 root dir)
<?php
session_start();
error_reporting(E_ALL);
$data = runGallery();
$data['title'] = (isset($data['title']) && !empty($data['title'])) ? $data['title'] : 'Gallery';
if (isset($data['bodyHtml'])) {
print <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>{$data['title']}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
{$data['javascript']}
{$data['css']}
</head>
<body>
{$data['bodyHtml']}
</body>
</html>
EOF;
}
function runGallery() {
include('embed.php');
$data = array();
// if anonymous user, set g2 activeUser to ''
$externalUID = '1234';
if (isset($_SESSION['username'])) {
$uid = $_SESSION['username'];
}
// initiate G2 - this call ok?
$ret = GalleryEmbed::init(array('g2Uri' => './',\
'embedUri' => 'main.php',
'fullInit' => 'true',
'activeUserId' => $uid));
if (!$ret) {
$data['bodyHtml'] = $ret->getAsHtml();
return $data;
} else {
print $ret->getAsHtml();
$ret = GalleryEmbed::isExternalIdMapped($uid, 'GalleryUser');
print $ret->getAsHtml();
// Where can I find a list of error codes?
if ($ret->getErrorCode() == 5) {
// How do I get the g2 ID of the user created in the line below?
$ret = GalleryEmbed::createUser($externalUID , array('username' => $uid,
'email' => 'email@email.com',
'password' => 'test',
'fullname' => 'test user'));
print $ret->getAsHtml();
// Should the $uid in the line below be the g2 id of the user just created in the line above?
$ret = GalleryEmbed::init(array('g2Uri' => './', 'embedUri' => 'main.php',
'fullInit' => 'true', 'activeUserId' => $uid));
print $ret->getAsHtml();
//$ret = GalleryEmbed::addExternalIdMapEntry($uid, 'test', 'GalleryUser');
$data['bodyHtml'] = $ret->getAsHtml();
return;
} else {
/* user already exists, login */
}
}
// finish off
GalleryEmbed::done();
// handle the G2 request
$g2moddata = GalleryEmbed::handleRequest();
// show error message if isDone is not defined
if (!isset($g2moddata['isDone'])) {
$data['bodyHtml'] = 'isDone is not defined, something very bad must have happened.';
return $data;
}
// exit if it was an immediate view / request (G2 already outputted some data)
if ($g2moddata['isDone']) {
exit;
}
// put the body html from G2 into the xaraya template
$data['bodyHtml'] = isset($g2moddata['bodyHtml']) ? $g2moddata['bodyHtml'] : '';
// get the page title, javascript and css links from the <head> html from G2
$title = '';
$javascript = array();
$css = array();
if (isset($g2moddata['headHtml'])) {
list($data['title'], $css, $javascript) = GalleryEmbed::parseHead($g2moddata['headHtml']);
$data['headHtml'] = $g2moddata['headHtml'];
}
/* Add G2 javascript */
if (!empty($javascript)) {
foreach ($javascript as $script) {
$data['javascript'] .= "\n".$script;
}
}
/* Add G2 css */
if (!empty($css)) {
foreach ($css as $style) {
$data['css'] .= "\n".$style;
}
}
// sidebar block
if (isset($g2moddata['sidebarBlocksHtml']) && !empty($g2moddata['sidebarBlocksHtml'])) {
$data['sidebarHtml'] = $g2moddata['sidebarBlocksHtml'];
}
return $data;
}
?>
The output I get from getAsHtml();
Error (ERROR_MISSING_OBJECT) : admin GalleryUser
* in modules/core/classes/helpers/GalleryEntityHelper_simple.class at line 124 (GalleryCoreApi::error)
* in modules/core/classes/GalleryCoreApi.class at line 2298 (GalleryEntityHelper_simple::loadEntityByExternalId)
* in modules/core/classes/GalleryEmbed.class at line 215 (GalleryCoreApi::loadEntityByExternalId)
* in modules/core/classes/GalleryEmbed.class at line 120 (GalleryEmbed::checkActiveUser)
* in main_wrapper.php at line 54 (GalleryEmbed::init)
* in main_wrapper.php at line 20
Error (ERROR_MISSING_OBJECT) : admin GalleryUser
* in modules/core/classes/GalleryEmbed.class at line 904 (GalleryCoreApi::error)
* in main_wrapper.php at line 61 (GalleryEmbed::isExternalIdMapped)
* in main_wrapper.php at line 20
Error (ERROR_COLLISION)
* in modules/core/classes/GalleryUser.class at line 149 (GalleryCoreApi::error)
* in modules/core/classes/GalleryEmbed.class at line 344 (GalleryUser::create)
* in main_wrapper.php at line 68 (GalleryEmbed::createUser)
* in main_wrapper.php at line 20
Error (ERROR_MISSING_OBJECT) : admin GalleryUser
* in modules/core/classes/helpers/GalleryEntityHelper_simple.class at line 124 (GalleryCoreApi::error)
* in modules/core/classes/GalleryCoreApi.class at line 2298 (GalleryEntityHelper_simple::loadEntityByExternalId)
* in modules/core/classes/GalleryEmbed.class at line 215 (GalleryCoreApi::loadEntityByExternalId)
* in modules/core/classes/GalleryEmbed.class at line 120 (GalleryEmbed::checkActiveUser)
* in main_wrapper.php at line 71 (GalleryEmbed::init)
* in main_wrapper.php at line 20
Posts: 20
Ok, after alot more reading through the forums, I've managed to get it to create/login users on the fly, still having some problems with CSS display tho, but it's 5 mins till knockoff on a Friday, so that can wait. :D
I've now started again with code I found in this thread http://gallery.menalto.com/node/69886
incase anyone wants it: (not complete yet, but working better than the previous post, will update more on monday)
btw, thanks for the code Slayergirl
Posts: 180
YW, I found it almost complete in the documentation after some digging around. My editted version worked with usergroups too but I kicked that out of the code in the forum ;)