Support Forums | Demo Gallery [1.x] [2.x] | Downloads | News | Site Map ]
Nuked Gallery
  Create a FREE account or Login   As a guest, you don't have access to our FULL navigation system.
 Forum FAQForum FAQ   StatisticsStatistics   SearchSearch   UsergroupsUsergroups   FavoritesFavorites  

phpbb3 multiple templates

 
Post new topic   Reply to topic    NukedGallery.net Forum Index » phpBB Integration » phpBB3 / Gallery 2 Integration View previous topicPrinter friendly versionView next topic
Author Message
anikolop

Beginner
Beginner


Joined: Jul 28, 2007
Posts: 5

PostPosted: Tue Sep 09, 2008 1:23 pm    Post subject: phpbb3 multiple templates Reply with quote

One question.
I prepare a forum based on phpbb 3.0.2 and i have two templates that the users can choose. The AcidTech Tiger and the 610nm template.
Is it possible to assign the gallery2 theme based to phpbb3.
Example:
For AcidTech Tiger the Carbon theme
For 610nm the Matrix theme.

I have read about the themes override code but could not understand it.

Any help would be appreciated.


And sorry for my English!
Back to top
Offline View user's profile Send private message
AdBot
   Post subject: phpbb3 multiple templates  

Back to top
jettyrat

Moderator
Moderator


Joined: Nov 28, 2005
Posts: 1139

PostPosted: Tue Sep 09, 2008 8:27 pm    Post subject: Re: phpbb3 multiple templates Reply with quote

Your english seems fine to me!

I've not tried this and the development code I have installed at the moment is not the v1.0.1 code so I can't give you line by line instructions, but I would start with adding a call to the init() function of g2helper something like - GalleryEmbed::setThemeForRequest('matrix' or 'carbon') depending on the value in the phpbb user array - $user->data['user_style'] which should be available in the init() function.
Back to top
Offline View user's profile Send private message
anikolop

Beginner
Beginner


Joined: Jul 28, 2007
Posts: 5

PostPosted: Thu Sep 11, 2008 6:31 am    Post subject: Re: phpbb3 multiple templates Reply with quote

Jettyrat thanks for your time.

I found here [gallery2.org] a example and modified with a if statement.
I have put this code to the end of the g2helper.php but it dont work or must i place somewhere else.


Code: › $ret = GalleryEmbed::init($this->_init_array);
handleStatus($ret);
if ($user->data['user_style'] == '610nm')
{
$ret = GalleryEmbed::setThemeForRequest('matrix');
}
else
{
$ret = GalleryEmbed::setThemeForRequest('carbon');
}
handleStatus($ret);
$data = GalleryEmbed::handleRequest();



And here is my g2helper.php

Code: › <?php
/**
* Gallery2 Integration
*
* @package   phpBB3
* @version 1.0.1
* @copyright (c) 2007 jettyrat
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
   exit;
}

/**
* @package phpBB3
*/
class g2helper {
   var $_compatibleEmbedVersionMajor = 1;
   var $_compatibleEmbedVersionMinor = 1;

   var $_activeAdminId;
   var $_init_array;

   /**
   * Gallery2 helper constructor
   * check and initialize some settings and load the main Gallery embed file
   */
   function g2helper()
   {
      global $config, $user, $phpEx;

      $user->add_lang('mods/gallery2');
      

      if (empty($config['g2i_fullPath']) || empty($config['g2i_embedUri']) || empty($config['g2i_g2Uri']) || empty($config['g2i_activeAdminId']))
      {
         trigger_error($user->lang['OBTAIN_SETTINGS_FAILED'], E_USER_ERROR);
      }

      $this->_activeAdminId = $config['g2i_activeAdminId'];

      $this->_init_array = array(
         'embedUri' => $config['g2i_embedUri'],
         'g2Uri' => $config['g2i_g2Uri'],
         'loginRedirect' => dirname($config['g2i_embedUri']) . "/ucp.$phpEx?mode=login",
         'apiVersion' => array($this->_compatibleEmbedVersionMajor, $this->_compatibleEmbedVersionMinor)
      );

      if ($this->_activeAdminId == 0)
      {
         trigger_error($user->lang['G2_AUTHADMIN_FAILED'], E_USER_ERROR);
      }
      else
      {
         require_once($config['g2i_fullPath']);
      }
   }

   /**
   * initialize a G2 session with a valid Gallery admin
   *
   * @access private
   */
   function _initAdmin()
   {
      $this->_init_array['activeUserId'] = $this->_activeAdminId;

      $ret = GalleryEmbed::init($this->_init_array);
      if (isset($ret))
      {
         global $user;

         trigger_error($user->lang['G2_INITADMIN_FAILED'] . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }
   }

   /**
   * initialize a G2 session with the current user
   */
   function init($user)
   {
      // not anonymous user
      if ($user->data['username'] != 'Anonymous') {
         // login admin
         $this->_initAdmin();

         // set init_array to current user_id
         $this->_init_array['activeUserId'] = $user->data['user_id'];

         // check if current user exists in Gallery
         $ret = GalleryEmbed::isExternalIdMapped($user->data['user_id'], 'GalleryUser');
         if (empty($ret))
         {
            // check or set users Gallery album link
            $this->_mapGalleryLink($user->data['user_id'], $user->data['user_gallery']);

            // logout admin
            $this->done();

            // login current user
            $ret = GalleryEmbed::init($this->_init_array);
            if (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_INITUSER_FAILED'], $user->data['username']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }
         }

         // create new user account if current user does not have Gallery account
         elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
         {
            $this->_createUser($user->data['user_id'], array(
               'username' => $user->data['username'],
               'hashedpassword' => $user->data['user_password'],
               'email' => $user->data['user_email'],
               'fullname' => $user->data['username'],
               'language' => $user->data['user_lang'],
               'creationtimestamp' => time(),
               'hashmethod' => 'md5')
            );

            // logout admin
            $this->done();

            // login current user
            $ret = GalleryEmbed::init($this->_init_array);
            if (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_INITUSER_FAILED'], $user->data['username']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }
         }
         else
         {
            trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $user->data['username']), E_USER_ERROR);
         }
      }
      // login phpBB anonymous user as Gallery guest
      else
      {
         $this->_init_array['activeUserId'] = 'guest';

         $ret = GalleryEmbed::init($this->_init_array);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_INITUSER_FAILED'], 'guest') . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
   }

   /**
   * end session and logout user from Gallery
   */
   function done()
   {
      $ret = GalleryEmbed::done();
      if (isset($ret))
      {
         global $user;

         trigger_error($user->lang['G2_TRANSACTION_FAILED'] . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }
   }

   /**
   * create new user in Gallery and add user to existing groups in Gallery
   *
   * @access private
   */
   function _createUser($id, $newUserData)
   {
      global $db, $user;

      //create the user
      $ret = GalleryEmbed::createUser($id, $newUserData);
      if (isset($ret))
      {
         trigger_error(sprintf($user->lang['G2_CREATEUSER_FAILED'], $id) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }

      $phpbbDefaultGroups = array(
         'GUESTS',
         'REGISTERED',
         'REGISTERED_COPPA',
         'GLOBAL_MODERATORS',
         'ADMINISTRATORS',
         'BOTS'
      );

      // add user to groups
      $sql = 'SELECT DISTINCT g.group_name
         FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . " ug
         WHERE ug.user_id = $id
            AND ug.group_id = g.group_id
            AND " . $db->sql_in_set('g.group_name', $phpbbDefaultGroups, true);
      if (!$result = $db->sql_query($sql))
      {
         trigger_error($user->lang['FETCH_USERGROUPDATA_FAILED'], E_USER_ERROR);
      }

      while ($row = $db->sql_fetchrow($result))
      {
         $ret = GalleryEmbed::addUserToGroup($id, $row['group_name']);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_ADDUSERTOGROUP_FAILED'], $row['group_name'], $id) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
   }

   /**
   * update user data in Gallery
   */
   function updateUser($id, $userData)
   {
      global $user;

      $this->_initAdmin();

      $ret = GalleryEmbed::isExternalIdMapped($id, 'GalleryUser');
      if (empty($ret))
      {
         $ret = GalleryEmbed::updateUser($id, array(
            'username' => (isset($userData['username'])) ? $userData['username'] : null,
            'hashedpassword' => (isset($userData['user_password'])) ? $userData['user_password'] : null,
            'hashmethod' => (isset($userData['user_password'])) ? 'md5' : null,
            'email' => (isset($userData['user_email'])) ? $userData['user_email'] : null)
         );
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_UPDATEUSER_FAILED'], $id) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * user doesn't exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $id), E_USER_ERROR);
      }

      $this->done();
   }

   /**
   * delete user in Gallery
   */
   function deleteUser($id)
   {
      global $db, $user;

      $this->_initAdmin();

      $ret = GalleryEmbed::isExternalIdMapped($id, 'GalleryUser');
      if (empty($ret))
      {
         if ($this->_activeAdminId == $id) // if user to delete is current activeAdmin, then find a new Gallery admin
         {
            $this->_activeAdminId = $this->_setNewActiveAdmin($this->_activeAdminId);
            $this->done();
            $this->_initAdmin();
         }

         $ret = GalleryEmbed::deleteUser($id);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_DELETEUSER_FAILED'], $id) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * user doesn't exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $id), E_USER_ERROR);
      }

      $this->done();
   }

   /**
   * create group in Gallery
   */
   function createGroup($groupName)
   {
      global $user;

      $this->_initAdmin();

      $ret = GalleryEmbed::isExternalIdMapped($groupName, 'GalleryGroup');
      if (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         $ret = GalleryEmbed::createGroup($groupName, $groupName);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_CREATEGROUP_FAILED'], $groupName) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
      elseif (empty($ret))
      {
         /*
         * Group already exists in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $groupName), E_USER_ERROR);
      }

      $this->done();
   }

   /**
   * update group in Gallery
   */
   function updateGroup($group_id, $newGroupName)
   {
      global $db, $user;

      $this->_initAdmin();

      $sql = 'SELECT group_name
         FROM ' . GROUPS_TABLE . "
         WHERE group_id = $group_id";
      if (!$row = $db->sql_fetchrow($db->sql_query_limit($sql, 1)))
      {
         trigger_error($user->lang['FETCH_GROUPDATA_FAILED'], E_USER_ERROR);
      }

      $ret = GalleryEmbed::isExternalIdMapped($row['group_name'], 'GalleryGroup');
      if (empty($ret))
      {
         $ret = GalleryEmbed::updateGroup($row['group_name'], array('groupname' => $newGroupName));
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_UPDATEGROUP_FAILED'], $newGroupName) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }

         $ret = GalleryCoreApi::updateMapEntry('ExternalIdMap', array('externalId' => $row['group_name']) , array('externalId' => $newGroupName));
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_UPDATEMAPENTRY_FAILED'], $row['group_name']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * Group does not exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $row['group_name']), E_USER_ERROR);
      }

      $this->done();
   }

   /**
   * delete group in Gallery
   */
   function deleteGroup($groupId)
   {
      global $db, $user;

      $this->_initAdmin();

      $users = array();

      // gather users for this group
      $sql = 'SELECT user_id
         FROM ' . USER_GROUP_TABLE . "
         WHERE group_id = $groupId";
      if (!$result = $db->sql_query($sql))
      {
         trigger_error($user->lang['FETCH_USERGROUPDATA_FAILED'], E_USER_ERROR);
      }

      while ($row = $db->sql_fetchrow($result))
      {
         $users[] = $row['user_id'];
      }

      // fetch group name from group id
      $sql = 'SELECT group_name
         FROM ' . GROUPS_TABLE . "
         WHERE group_id = $groupId";
      if (!$row = $db->sql_fetchrow($db->sql_query_limit($sql, 1)))
      {
         trigger_error($user->lang['FETCH_GROUPDATA_FAILED'], E_USER_ERROR);
      }

      // delete Gallery group if it exists
      $ret = GalleryEmbed::isExternalIdMapped($row['group_name'], 'GalleryGroup');
      if (empty($ret))
      {
         $ret = GalleryEmbed::deleteGroup($row['group_name']);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_DELETEGROUP_FAILED'], $row['group_name']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * Group does not exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $row['group_name']), E_USER_ERROR);
      }

      $this->done();

      // update admin permissions for users in this group in case it had admin permissions
      if (sizeof($users) > 0)
      {
         $this->updateAdminPermissions($users, 'user');
      }
   }

   /**
   * add users to Gallery group
   *
   * @param array $members array of user_id's
   * @param int $groupId phpbb group_id
   */
   function addUserToGroup($members, $groupId)
   {
      global $db, $user;

      $this->_initAdmin();

      $sql = 'SELECT group_name
         FROM ' . GROUPS_TABLE . "
         WHERE group_id = $groupId";
      if (!$row = $db->sql_fetchrow($db->sql_query_limit($sql, 1)))
      {
         trigger_error($user->lang['FETCH_GROUPDATA_FAILED'], E_USER_ERROR);
      }

      $ret = GalleryEmbed::isExternalIdMapped($row['group_name'], 'GalleryGroup');
      if (empty($ret))
      {
         foreach ($members as $member)
         {
            $ret = GalleryEmbed::isExternalIdMapped($member, 'GalleryUser');
            if (empty($ret))
            {
               $ret = GalleryEmbed::addUserToGroup($member, $row['group_name']);
               if (isset($ret))
               {
                  trigger_error(sprintf($user->lang['G2_ADDUSERTOGROUP_FAILED'], $row['group_name'], $member) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
               }
            }
            elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
            {
               /*
               * User does not exist in gallery, so do nothing!
               */
            }
            else
            {
               trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $member), E_USER_ERROR);
            }
         }
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * Group does not exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $row['group_name']), E_USER_ERROR);
      }

      $this->done();
   }

   /**
   * remove users from Gallery group
   *
   * @param array $members array of user_id's
   * @param int $groupId phpbb group_id
   */
   function removeUserFromGroup($members, $groupId)
   {
      global $db, $user;

      $this->_initAdmin();

      $sql = 'SELECT group_name
         FROM ' . GROUPS_TABLE . "
         WHERE group_id = $groupId";
      if (!$row = $db->sql_fetchrow($db->sql_query_limit($sql, 1)))
      {
         trigger_error($user->lang['FETCH_GROUPDATA_FAILED'], E_USER_ERROR);
      }

      $ret = GalleryEmbed::isExternalIdMapped($row['group_name'], 'GalleryGroup');
      if (empty($ret))
      {
         foreach ($members as $member)
         {
            $ret = GalleryEmbed::isExternalIdMapped($member, 'GalleryUser');
            if (empty($ret))
            {
               $ret = GalleryEmbed::removeUserFromGroup($member, $row['group_name']);
               if (isset($ret))
               {
                  trigger_error(sprintf($user->lang['G2_REMOVEUSERFROMGROUP_FAILED'], $row['group_name'], $member) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
               }
            }
            elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
            {
               /*
               * User does not exist in gallery, so do nothing!
               */
            }
            else
            {
               trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $member), E_USER_ERROR);
            }
         }
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * Group does not exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $row['group_name']), E_USER_ERROR);
      }

      $this->done();

   }

   /**
   * update Gallery admin permissions for a set of users
   *
   * @param array $ids array of id's single role, multiple groups, multiple users
   * @param string $type role, group, user
   */
   function updateAdminPermissions($ids, $type)
   {
      global $db, $user, $auth;

      $this->_initAdmin();

      $users = $groups = $userData = $userToAdmin = $adminToUser = array();

      switch ($type)
      {
         case 'role' : // grab groups belonging to this role and all users belonging to these groups and the role
            $sql = 'SELECT group_id
               FROM ' . ACL_GROUPS_TABLE . '
               WHERE auth_role_id = ' . $ids[0];
            if (!$result = $db->sql_query($sql))
            {
               trigger_error($user->lang['FETCH_ACLGROUPDATA_FAILED'], E_USER_ERROR);
            }

            while ($row = $db->sql_fetchrow($result))
            {
               $groups[] = $row['group_id'];
            }

            foreach ($groups as $groupId)
            {
               $sql = 'SELECT user_id
                  FROM ' . USER_GROUP_TABLE . "
                  WHERE group_id = $groupId";
               if (!$result = $db->sql_query($sql))
               {
                  trigger_error($user->lang['FETCH_USERGROUPDATA_FAILED'], E_USER_ERROR);
               }

               while ($row = $db->sql_fetchrow($result))
               {
                  $users[] = $row['user_id'];
               }
            }

            $sql = 'SELECT user_id
               FROM ' . ACL_USERS_TABLE . '
               WHERE auth_role_id = ' . $ids[0];
            if (!$result = $db->sql_query($sql))
            {
               trigger_error($user->lang['FETCH_ACLUSERSDATA_FAILED'], E_USER_ERROR);
            }

            while ($row = $db->sql_fetchrow($result))
            {
               $users[] = $row['user_id'];
            }

         break;

         case 'group' : // grab users belonging to these groups
            foreach ($ids as $groupId)
            {
               $sql = 'SELECT user_id
                  FROM ' . USER_GROUP_TABLE . "
                  WHERE group_id = $groupId";
               if (!$result = $db->sql_query($sql))
               {
                  trigger_error($user->lang['FETCH_USERGROUPDATA_FAILED'], E_USER_ERROR);
               }

               while ($row = $db->sql_fetchrow($result))
               {
                  $users[] = $row['user_id'];
               }
            }

         break;

         case 'user' : // grab users
            foreach ($ids as $userId)
            {
               $users[] = $userId;
            }

         break;
      }

      foreach ($users as $userId) // determine permissions for each of these users
      {
         $sql = 'SELECT user_type, user_permissions
            FROM ' . USERS_TABLE . "
            WHERE user_id = $userId";
         if (!$row = $db->sql_fetchrow($db->sql_query_limit($sql, 1)))
         {
            trigger_error($user->lang['OBTAIN_USERINFO_FAILED'], E_USER_ERROR);
         }

         $userData['user_id'] = $userId;
         $userData['user_type'] = $row['user_type'];
         $userData['user_permissions'] = $row['user_permissions'];

         $auth->acl($userData);

         if ($auth->acl_get('a_gallery2') > 0)
         {
            $userToAdmin[] = $userId; // user has Gallery admin permissions
         }
         else
         {
            $adminToUser[] = $userId; // user does not have Gallery admin permissions
         }
      }

      if (sizeof($userToAdmin) > 0)
      {
         $this->_mapUserToAdmin($userToAdmin); // move array of users to admin status
      }

      if (sizeof($adminToUser) > 0)
      {
         $this->_mapAdminToUser($adminToUser); // remove array of admins to user status

         if (in_array($this->_activeAdminId, $adminToUser, true)) // are we removing the current activeAdmin from Gallery admin status
         {
            $this->_activeAdminId = $this->_setNewActiveAdmin($this->_activeAdminId); // find a new admin
         }
      }

      $this->done();
   }

   /**
   * move users to admin status in Gallery
   *
   * @param array $ids array of id's user id's
   *
   * @access private
   */
   function _mapUserToAdmin($ids)
   {
      global $db, $user;

      $adminGroupId = $this->_fetchAdminGroupId();

      foreach ($ids as $id)
      {
         $sql = 'SELECT user_regdate, username, user_password, user_email, user_lang
            FROM ' . USERS_TABLE . "
            WHERE user_id = $id";
         if (!$row = $db->sql_fetchrow($db->sql_query($sql)))
         {
            trigger_error($user->lang['OBTAIN_USERINFO_FAILED'], E_USER_ERROR);
         }

         $ret = GalleryEmbed::isExternalIdMapped($id, 'GalleryUser');
         if (empty($ret))
         {
            $this->_addUserToAdminGroup($row['username'], $adminGroupId);
         }
         elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT) // if user does not exist, create them since they are now admins
         {
            $this->_createUser($id, array(
               'username' => $row['username'],
               'hashedpassword' => $row['user_password'],
               'hashmethod' => 'md5',
               'email' => $row['user_email'],
               'creationtimestamp' => $row['user_regdate'],
               'language' => $row['user_lang'],
               'fullname' => $row['username'])
            );

            $this->_addUserToAdminGroup($row['username'], $adminGroupId);
         }
         else
         {
            trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $id), E_USER_ERROR);
         }
      }
   }
   
   /**
   * move admins to user status in Gallery
   *
   * @param array $ids array of id's user id's
   *
   * @access private
   */
   function _mapAdminToUser($ids)
   {
      global $db, $user;

      $adminGroupId = $this->_fetchAdminGroupId();

      foreach ($ids as $id)
      {
         $sql = 'SELECT username
            FROM ' . USERS_TABLE . "
            WHERE user_id = $id";
         if (!$row = $db->sql_fetchrow($db->sql_query($sql)))
         {
            trigger_error($user->lang['OBTAIN_USERINFO_FAILED'], E_USER_ERROR);
         }

         $ret = GalleryEmbed::isExternalIdMapped($id, 'GalleryUser');
         if (empty($ret))
         {
            list ($ret, $userId) = GalleryCoreApi::fetchUserByUserName($row['username']);
            if (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_FETCHUSERBYUSERNAME_FAILED'], $row['username']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }

            $ret = GalleryCoreApi::removeUserFromGroup($userId->getId(), $adminGroupId);
            if (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_REMOVEUSERFROMGROUP_FAILED'], $adminGroupId, $userId->getId()) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }
         }
         elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
         {
            /*
            * user doesn't exist in gallery, so do nothing!
            */
         }
      }
   }

   /**
   * map users Gallery album id to user_gallery in USERS_TABLE (album)
   *
   * @param int $userId currrent user_id
   * @param int $userGallery current user gallery id
   *
   * @access private
   */
   function _mapGalleryLink($userId, $userGallery)
   {
      global $db, $gallery, $user;

      if (isset($userGallery)) // check if current album id is valid
      {
         list ($entityId, $itemIds) = $this->_fetchItemsForUser($userId);

         if (in_array($userGallery, $itemIds, true))
         {
            return;
         }
      }

      // search for valid album id for current user
      if (empty($entityId))
      {
         list ($ret, $entityId) = GalleryCoreApi::loadEntityByExternalId($userId, 'GalleryUser');
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_LOADENTITYBYEXTID_FAILED'], $userId) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }

      if (empty($itemIds))
      {
         list ($ret, $itemIds) = GalleryCoreApi::fetchAllItemIdsByOwnerId($entityId->getId());
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_FETCHITEMSBYOWNER_FAILED'], $entityId->getId()) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }

      $query = "SELECT [Entity::id] FROM [Entity] WHERE [Entity::entityType] = ?";
      list ($ret, $results) = $gallery->search($query, array('GalleryAlbumItem'));
      if (isset($ret))
      {
         trigger_error($user->lang['G2_FETCHENTITYID_FAILED'] . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }

      while ($g2Result = $results->nextResult())
      {
         if (in_array($g2Result[0], $itemIds, true))
         {
            $sql = 'UPDATE ' . USERS_TABLE . '
               SET user_gallery = ' . $g2Result[0] . "
               WHERE user_id = $userId";
            if (!$db->sql_query($sql))
            {
               trigger_error($user->lang['UPDATE_GALLERYUSER_FAILED'], E_USER_ERROR);
            }

            $user->data['user_gallery'] = $g2Result[0];

            return;
         }
      }

      $sql = 'UPDATE ' . USERS_TABLE . "
         SET user_gallery = NULL
         WHERE user_id = $userId";
      if (!$db->sql_query($sql))
      {
         trigger_error($user->lang['UPDATE_GALLERYUSER_FAILED'], E_USER_ERROR);
      }

      $user->data['user_gallery'] = '';
   }

   /**
   * map all users Gallery item id's up to limit (photos)
   *
   * @param int $userId currrent user_id
   * @param bool $albums set for albums, not for photos
   * @param int $limit search result limit
   *
   * @return array of int item count and array of Gallery item links for current user
   */
   function mapAllGalleryLinks($userId, $albums, $limit)
   {
      global $gallery, $user;

      $this->_initAdmin();

      $ret = GalleryEmbed::isExternalIdMapped($userId, 'GalleryUser');
      if (empty($ret))
      {
         list ($entityId, $itemIds) = $this->_fetchItemsForUser($userId);

         if (sizeof($itemIds) == 0)
         {
            /*
            * User has no album items to display!
            */
            $this->done();
            return array(null, null);
         }
         else
         {
            list ($ret, $descendentCounts) = GalleryCoreApi::fetchDescendentCounts($itemIds, $entityId->getId());
            if (($descendentCounts[$itemIds[0]] < 1) || (isset($ret) && $ret->getErrorCode() & ERROR_STORAGE_FAILURE))
            {
               /*
               * User has no descendent items to display!
               */
               $this->done();
               return array(null, null);
            }
            elseif (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_FETCHDESCENDCOUNTS_FAILED'], $entityId->getId()) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }
         }

         $itemIds = array();
         foreach ($descendentCounts as $key => $value)
         {
            if (empty($albums) && $value == 0)
            {
               $itemIds[] = $key;
            }
            elseif (!empty($albums) && $value <> 0)
            {
               $itemIds[] = $key;
            }
         }

         $count = count($itemIds);

         $itemIds = ($count > $limit) ? array_slice($itemIds, -$limit, $limit) : $itemIds;

         list ($ret, $thumbNails) = GalleryCoreApi::fetchThumbnailsByItemIds($itemIds);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_FETCHTHUMBSBYIDS_FAILED'], $entityId->getId()) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }

         $urlGenerator =& $gallery->getUrlGenerator();

         $itemLinks = array();

         foreach ($itemIds as $id)
         {
            if (!empty($thumbNails[$id]))
            {
               $itemLinks[$id] = $urlGenerator->generateUrl(array('view' => 'core.DownloadItem', 'itemId' => $thumbNails[$id]->getId()), array('fullUrl' => true));
            }
            else
            {
               $count--;
            }
         }

         $this->done();

         return array($count, $itemLinks);
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * User does not exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $userId), E_USER_ERROR);
      }

      $this->done();

      return array(null, null);
   }

   /**
   * add user to Gallery admin group
   *
   * @param string $username currrent username
   * @param int $adminGroupId Gallery admin group id
   *
   * @access private
   */
   function _addUserToAdminGroup($username, $adminGroupId)
   {
      global $user;

      list ($ret, $userId) = GalleryCoreApi::fetchUserByUserName($username);
      if (isset($ret))
      {
         trigger_error(sprintf($user->lang['G2_FETCHUSERBYUSERNAME_FAILED'], $username) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }

      $ret = GalleryCoreApi::addUserToGroup($userId->getId(), $adminGroupId);
      if (isset($ret))
      {
         trigger_error(sprintf($user->lang['G2_ADDUSERTOGROUP_FAILED'], $adminGroupId, $userId->getId()) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }
   }

   /**
   * fetch Gallery admin group id
   *
   * @return int Gallery admin group id
   *
   * @access private
   */
   function _fetchAdminGroupId()
   {
      global $user;

      list ($ret, $adminGroupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
      if (isset($ret))
      {
         trigger_error($user->lang['G2_ADMINPARAMETER_FAILED'] . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }

      return $adminGroupId;
   }

   /**
   * set new activeAdminId based on USER_FOUNDER status since there is always at least one founder in phpbb
   *
   * @return int user_id of new activeAdmin
   *
   * @access private
   */
   function _setNewActiveAdmin($curActiveAdmin)
   {
      global $db, $user;

      $sql = 'SELECT user_id
         FROM ' . USERS_TABLE . '
         WHERE user_type = ' . USER_FOUNDER . '
            AND user_id <> ' . $curActiveAdmin;
      if (!$row = $db->sql_fetchrow($db->sql_query_limit($sql, 1)))
      {
         trigger_error($user->lang['FETCH_USERINFO_FAILED'], E_USER_ERROR);
      }

      $this->_mapUserToAdmin(array($row['user_id']));

      $sql = 'UPDATE ' . CONFIG_TABLE . '
         SET g2i_activeAdminId = ' . $row['user_id'];
      if (!$db->sql_query($sql))
      {
         trigger_error($user->lang['UPDATE_ACTIVEADMINID_FAILED'], E_USER_ERROR);
      }

      return $row['user_id'];
   }

   /**
   * utility method to fetch all Gallery items for given phpbb user_id
   *
   * @return array of Gallery user id and array of item id's for current user
   *
   * @access private
   */
   function _fetchItemsForUser($userId)
   {
      global $user;

      list ($ret, $entityId) = GalleryCoreApi::loadEntityByExternalId($userId, 'GalleryUser');
      if (isset($ret))
      {
         trigger_error(sprintf($user->lang['G2_LOADENTITYBYEXTID_FAILED'], $userId) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }

      list ($ret, $itemIds) = GalleryCoreApi::fetchAllItemIdsByOwnerId($entityId->getId());
      if (isset($ret))
      {
         trigger_error(sprintf($user->lang['G2_FETCHITEMSBYOWNER_FAILED'], $entityId->getId()) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }

      return array($entityId, $itemIds);

   }

   /**
   * fetch given Gallery plugin status
   *
   * @param string $pluginType (eg. module)
   * @param string $pluginName plugin name
   *
   * @return bool Gallery plugin status
   */
   function fetchPluginStatus($pluginType, $pluginName)
   {
      $this->_initAdmin();

      $pluginStatus = GalleryCoreApi::fetchPluginStatus($pluginType);

      $this->done();

      return (!empty($pluginStatus[1][$pluginName]['active'])) ? true : false;

   }
}
function _initUser()
{
$ret = GalleryEmbed::init($this->_init_array);
handleStatus($ret);
if ($user->data['user_style'] == '610nm')
{
$ret = GalleryEmbed::setThemeForRequest('matrix');
}
else
{
$ret = GalleryEmbed::setThemeForRequest('carbon');
}
handleStatus($ret);
$data = GalleryEmbed::handleRequest();
}
?>


Hope for your suggestions
Back to top
Offline View user's profile Send private message
jettyrat

Moderator
Moderator


Joined: Nov 28, 2005
Posts: 1139

PostPosted: Thu Sep 11, 2008 9:23 am    Post subject: Re: phpbb3 multiple templates Reply with quote

Well, you have several problems with that.

First, you need to lookup the style id's from the phpbb_styles table. The value in $user->data['user_style'] is an integer that matches the style_id in the styles table for each users chosen style.

Second, you can't just add a function like that outside of the class. The appropriate code needs to be added to the existing init() method.

Try this - in g2helper.php - Find: (2 instances in init() function)
Code: ›             // login current user
            $ret = GalleryEmbed::init($this->_init_array);
            if (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_INITUSER_FAILED'], $user->data['username']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }

After, add:
Code: › $style = ($user->data['user_style'] == 1) ? 'matrix' : 'carbon';
$ret = GalleryEmbed::setThemeForRequest($style);
if (isset($ret))
{
   trigger_error('Error setting user theme.' . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
}

You will have to adjust the number in the $style test to match the number from the styles table and again, I've not tested, so I leave that fun to you!
Smile
Back to top
Offline View user's profile Send private message
anikolop

Beginner
Beginner


Joined: Jul 28, 2007
Posts: 5

PostPosted: Mon Sep 15, 2008 5:35 am    Post subject: Re: phpbb3 multiple templates Reply with quote

Hi again.
I tried your code but it this error:
Code: › Fatal error: Call to undefined method GalleryEmbed::setthemeforrequest() in /home/www/phpbb3/g2helper.php on line 112


the g2helper.php is this

Code: › <?php
/**
* Gallery2 Integration
*
* @package   phpBB3
* @version 1.0.1
* @copyright (c) 2007 jettyrat
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
   exit;
}

/**
* @package phpBB3
*/
class g2helper {
   var $_compatibleEmbedVersionMajor = 1;
   var $_compatibleEmbedVersionMinor = 1;

   var $_activeAdminId;
   var $_init_array;

   /**
   * Gallery2 helper constructor
   * check and initialize some settings and load the main Gallery embed file
   */
   function g2helper()
   {
      global $config, $user, $phpEx;

      $user->add_lang('mods/gallery2');

      if (empty($config['g2i_fullPath']) || empty($config['g2i_embedUri']) || empty($config['g2i_g2Uri']) || empty($config['g2i_activeAdminId']))
      {
         trigger_error($user->lang['OBTAIN_SETTINGS_FAILED'], E_USER_ERROR);
      }

      $this->_activeAdminId = $config['g2i_activeAdminId'];

      $this->_init_array = array(
         'embedUri' => $config['g2i_embedUri'],
         'g2Uri' => $config['g2i_g2Uri'],
         'loginRedirect' => dirname($config['g2i_embedUri']) . "/ucp.$phpEx?mode=login",
         'apiVersion' => array($this->_compatibleEmbedVersionMajor, $this->_compatibleEmbedVersionMinor)
      );

      if ($this->_activeAdminId == 0)
      {
         trigger_error($user->lang['G2_AUTHADMIN_FAILED'], E_USER_ERROR);
      }
      else
      {
         require_once($config['g2i_fullPath']);
      }
   }

   /**
   * initialize a G2 session with a valid Gallery admin
   *
   * @access private
   */
   function _initAdmin()
   {
      $this->_init_array['activeUserId'] = $this->_activeAdminId;

      $ret = GalleryEmbed::init($this->_init_array);
      if (isset($ret))
      {
         global $user;

         trigger_error($user->lang['G2_INITADMIN_FAILED'] . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }
   }

   /**
   * initialize a G2 session with the current user
   */
   function init($user)
   {
      // not anonymous user
      if ($user->data['username'] != 'Anonymous') {
         // login admin
         $this->_initAdmin();

         // set init_array to current user_id
         $this->_init_array['activeUserId'] = $user->data['user_id'];

         // check if current user exists in Gallery
         $ret = GalleryEmbed::isExternalIdMapped($user->data['user_id'], 'GalleryUser');
         if (empty($ret))
         {
            // check or set users Gallery album link
            $this->_mapGalleryLink($user->data['user_id'], $user->data['user_gallery']);

            // logout admin
            $this->done();

            // login current user
            $ret = GalleryEmbed::init($this->_init_array);
            if (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_INITUSER_FAILED'], $user->data['username']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }
         $style = ($user->data['user_style'] == 1) ? 'matrix' : 'carbon';
         $ret = GalleryEmbed::setThemeForRequest($style);
            if (isset($ret))
            {
               trigger_error('Error setting user theme.' . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }
         }

         // create new user account if current user does not have Gallery account
         elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
         {
            $this->_createUser($user->data['user_id'], array(
               'username' => $user->data['username'],
               'hashedpassword' => $user->data['user_password'],
               'email' => $user->data['user_email'],
               'fullname' => $user->data['username'],
               'language' => $user->data['user_lang'],
               'creationtimestamp' => time(),
               'hashmethod' => 'md5')
            );

            // logout admin
            $this->done();

            // login current user
            $ret = GalleryEmbed::init($this->_init_array);
            if (isset($ret))
            {
               trigger_error(sprintf($user->lang['G2_INITUSER_FAILED'], $user->data['username']) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }
         $style = ($user->data['user_style'] == 1) ? 'matrix' : 'carbon';
         $ret = GalleryEmbed::setThemeForRequest($style);
            if (isset($ret))
            {
               trigger_error('Error setting user theme.' . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
            }            
         }
         else
         {
            trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $user->data['username']), E_USER_ERROR);
         }
      }
      // login phpBB anonymous user as Gallery guest
      else
      {
         $this->_init_array['activeUserId'] = 'guest';

         $ret = GalleryEmbed::init($this->_init_array);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_INITUSER_FAILED'], 'guest') . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
   }

   /**
   * end session and logout user from Gallery
   */
   function done()
   {
      $ret = GalleryEmbed::done();
      if (isset($ret))
      {
         global $user;

         trigger_error($user->lang['G2_TRANSACTION_FAILED'] . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }
   }

   /**
   * create new user in Gallery and add user to existing groups in Gallery
   *
   * @access private
   */
   function _createUser($id, $newUserData)
   {
      global $db, $user;

      //create the user
      $ret = GalleryEmbed::createUser($id, $newUserData);
      if (isset($ret))
      {
         trigger_error(sprintf($user->lang['G2_CREATEUSER_FAILED'], $id) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
      }

      $phpbbDefaultGroups = array(
         'GUESTS',
         'REGISTERED',
         'REGISTERED_COPPA',
         'GLOBAL_MODERATORS',
         'ADMINISTRATORS',
         'BOTS'
      );

      // add user to groups
      $sql = 'SELECT DISTINCT g.group_name
         FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . " ug
         WHERE ug.user_id = $id
            AND ug.group_id = g.group_id
            AND " . $db->sql_in_set('g.group_name', $phpbbDefaultGroups, true);
      if (!$result = $db->sql_query($sql))
      {
         trigger_error($user->lang['FETCH_USERGROUPDATA_FAILED'], E_USER_ERROR);
      }

      while ($row = $db->sql_fetchrow($result))
      {
         $ret = GalleryEmbed::addUserToGroup($id, $row['group_name']);
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_ADDUSERTOGROUP_FAILED'], $row['group_name'], $id) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
   }

   /**
   * update user data in Gallery
   */
   function updateUser($id, $userData)
   {
      global $user;

      $this->_initAdmin();

      $ret = GalleryEmbed::isExternalIdMapped($id, 'GalleryUser');
      if (empty($ret))
      {
         $ret = GalleryEmbed::updateUser($id, array(
            'username' => (isset($userData['username'])) ? $userData['username'] : null,
            'hashedpassword' => (isset($userData['user_password'])) ? $userData['user_password'] : null,
            'hashmethod' => (isset($userData['user_password'])) ? 'md5' : null,
            'email' => (isset($userData['user_email'])) ? $userData['user_email'] : null)
         );
         if (isset($ret))
         {
            trigger_error(sprintf($user->lang['G2_UPDATEUSER_FAILED'], $id) . $user->lang['G2_ERROR'] . $ret->getAsHtml(), E_USER_ERROR);
         }
      }
      elseif (isset($ret) && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
      {
         /*
         * user doesn't exist in gallery, so do nothing!
         */
      }
      else
      {
         trigger_error(sprintf($user->lang['G2_ISEXTERNALIDMAPPED_FAILED'], $id), E_USER_ERROR);
      }

      $this->done();
   }

   /**
   * delete user in Gallery
   */
   function deleteUser($id)
   {
      global $db, $user;

      $this->_initAdmin();

      $ret = GalleryEmbed::isExternalIdMapped($id, 'GalleryUser');
      if (empty($ret))
      {
         if ($this->_activeAdminId == $id) // if user to delete is current activeAdmin, then find a new Gallery admin
         {
            $this->_activeAdminId = $this->_setNewActiveAdmin($this->_activeAdminId);
            $this->done();
            $this->_initAdmin();
         }

         $ret = GalleryEmbed::deleteUser($id);
         if (isset($ret))
         {