Simple Machines Community Forum

Customizing SMF => Bridges and Integrations => Topic started by: Hot Rides on April 09, 2008, 09:22:45 AM

Title: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 09:22:45 AM
I want smf to access a bridged db( i.e. coppermine, ect.) and modify at registration or through the member profile.
Basiclly what Im asking is what code do I use to tell it to look in a different db for information.

And so you dont try to assume what i'm wrongly, The main thing Im wanting to do is have an album in coppermine created everytime a user registers. I have some parts of the code needed, but do not know how to access the coppermine db to add them or where at in the register.php to put the code.
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 11:01:48 AM
actually, I think that the native coppermine bridge to SMF will do this automatically....    if you have the correct setting in coppermine. (i.e. create an album for each user)
Title: Re: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 11:22:46 AM
ok, where in the file would I need to put the code so that it did it properly?
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 11:29:43 AM
I don't think you understand.

To the bets of my knowledge, you do not have to do anything to the code.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 11:58:20 AM
my fault, it seems I have confused you. I am wanting the registration process to automatically create an album in coppermine with the username.

$catid = mysql_insert_id() + FIRST_USER_CAT;
cpg_db_query("INSERT INTO {$CONFIG['TABLE_ALBUMS']} (`title`, `category`) VALUES ('".addslashes($user_name)."', $catid)");

This should be the code I need to put in or a slight variation

I need to know where to put it so that it executes properly, I think I have figured out how to get to the right value, table, db
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 12:30:50 PM
again...   as I said, I believe that the option already exists to do just that, native to coppermine.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 12:44:05 PM
it is not, it has been brought up for use in next release, but there is no telling when a stable version will be released
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 12:46:41 PM
ah, strange.  I thought it was part of the standard functionality.
I know that several of the other galleries have this built in - Gallery2, for example.


as for where to put that code...   you'll have to ask over on the coppermine forums, since it really has nothing to do with SMF.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 01:11:08 PM
the code has to be placed in the SMF pages because its a bridged setup. registration uses the smf pages
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 01:43:38 PM
while registration might use the SMF pages, user creation in Coppermine still is triggers/handled by the coppermine functions.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 03:21:34 PM
but registering takes place soley in smf, none of it is done in cm. therefore code has to be place on the smf side to access and edit the coppermine database
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 03:39:28 PM
really?   I don't think so.

The bridge matches users between smf and coppermine, it doesn't replace one user system with the other.  While the registration might take place in SMF, there is some function which is triggered to create the same user in the coppermine user table...
Title: Re: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 03:58:30 PM
then there must be something weird with my install becasue it writes nothing to the coppermine user table only the smf user table
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 04:01:10 PM
hmmm....   Ok, I am going to have to step back, since it has been a while since I worked with Coppermine. The previous versions that I worked with used both smf and coppermine user tables.


Anyone more familiar with the current implementation care to comment?
Title: Re: call up another db and editing it
Post by: rsw686 on April 09, 2008, 04:56:18 PM
The user automatically gets their gallery space, but they need to create an album to put the pictures they upload in. It seems to me that this is best as why would you want to name the album the same name as their gallery. The users are allowed multiple albums in their gallery.
Title: Re: call up another db and editing it
Post by: rsw686 on April 09, 2008, 04:57:38 PM
Quote from: Hot Rides on April 09, 2008, 03:58:30 PM
then there must be something weird with my install becasue it writes nothing to the coppermine user table only the smf user table

It is not supposed to. The bridge reads the smf user table to see who is allowed. Why create a duplicate of this information. It uses the SSI.php file to deal with the login / logout.
Title: Re: call up another db and editing it
Post by: Kindred on April 09, 2008, 04:59:00 PM
rsw...

it USED to maintain both tables...
(and all of the other bridges do so)
Title: Re: call up another db and editing it
Post by: rsw686 on April 09, 2008, 05:05:17 PM
Quote from: Kindred on April 09, 2008, 04:59:00 PM
rsw...

it USED to maintain both tables...
(and all of the other bridges do so)

I didn't know that it used to. I have read through the coppermine bridge code as I modified mine and was just stating that from the code there it doesn't store anything in its own user table.

Yes most other bridges do but they are storing other user information. Since the coppermine bridge uses the smf group assignment it doesn't have anything else to store about the user.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 09, 2008, 07:04:46 PM
Quote from: rsw686 on April 09, 2008, 04:56:18 PM
The user automatically gets their gallery space, but they need to create an album to put the pictures they upload in. It seems to me that this is best as why would you want to name the album the same name as their gallery. The users are allowed multiple albums in their gallery.
I really dont care what the album name is, I just want one created at registration so the user doesnt have to create albums before uploading. I dont want them to have to think. Most people are to lazy to keep clicking until they figure it out.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 10, 2008, 08:53:44 PM
looks like Im gonna get no help for this. SMF says its a coppermine issue, Coppermine says its an smf issue, lol, its a both issue if you ask me.

The coding has to be done to SMF to tell it to access and edit the Coppermine DB.
Title: Re: call up another db and editing it
Post by: rsw686 on April 10, 2008, 09:49:11 PM
Quote from: Hot Rides on April 10, 2008, 08:53:44 PM
The coding has to be done to SMF to tell it to access and edit the Coppermine DB.

Actually the coding should go in the bridge file which is part of coppermine.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 11, 2008, 04:09:50 PM
could you explain further? Im in over my head here.
Title: Re: call up another db and editing it
Post by: rsw686 on April 11, 2008, 04:36:16 PM
In the coppermine\bridge folder there is smf10.inc.php. This is the bridge file to interface SMF and Coppermine. This files provides the needed functions to link the two programs. You would want to add the album creation code in here.

First check to see if they have an album with the SQL query

SELECT aid FROM cpg_albums WHERE category = [10000 + SMF USER ID]

If mysql_num_rows = 0 then add an album for that user

INSERT INTO cpg_albums (title, category)
VALUES ('My album', [10000 + SMF USER ID])

I would probably stick this code in the  function authenticate() right before the                 $this->session_update().

Make sure to check to see if they are logged in. You don't want to add albums for guests. A simple $user_settings['ID_MEMBER'] != 0 would work.
Title: Re: call up another db and editing it
Post by: Kindred on April 11, 2008, 04:46:09 PM
you probably want an IF NOT EXISTS in there as well...
Title: Re: call up another db and editing it
Post by: Hot Rides on April 11, 2008, 10:07:53 PM
thank you for the help, I will see if this works and report back
Title: Re: call up another db and editing it
Post by: Hot Rides on April 12, 2008, 12:05:59 AM
it does not like my code, did I do something wrong?
$request = db_query("
SELECT aid
FROM {$db_prefix}albums
WHERE category = [10000 + ['username']]");

If (mysql_num_rows($request) = 0 ;

INSERT INTO {$CONFIG['TABLE_ALBUMS']}  (title, category)
VALUES ('My album', [10000 + ['username']]);
Title: Re: call up another db and editing it
Post by: rsw686 on April 12, 2008, 12:26:37 AM
You need to be using the coppermine functions to run the query. Also the formatting of your code is not correct. My previous post was just an explanation of what to do, not a copy paste. ;)

I haven't tested this so there could be some things that need slight tweaking.


if($user_settings['ID_MEMBER'] != 0)
{
$sql = "SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (10000 + $user_settings['ID_MEMBER']);
$result = cpg_db_query($sql, $this->link_id);
if(mysql_num_rows($result) == 0)
{
$sql = "INSERT INTO {$CONFIG['TABLE_ALBUMS']} (title, category) VALUES ('My album', " . (10000 + $user_settings['ID_MEMBER']) . ")";
cpg_db_query($sql, $this->link_id);
}
mysql_free_result($result);
}
Title: Re: call up another db and editing it
Post by: Hot Rides on April 12, 2008, 12:43:33 AM
fatal error

I do not have the time to search right now for more answer, Im off to bed, any ideas would be helpful. Thank you with the help in the code so far I am slowly learning.
Title: Re: call up another db and editing it
Post by: rsw686 on April 12, 2008, 12:51:54 AM
Quote from: Hot Rides on April 12, 2008, 12:43:33 AM
fatal error

I do not have the time to search right now for more answer, Im off to bed, any ideas would be helpful. Thank you with the help in the code so far I am slowly learning.

What line was the error on? You can add the below line to your .htaccess file to show the errors.

php_value display_errors on

If I have time later I'll setup another gallery install and get you the exact code. I just don't want to be messing with this on my live site.
Title: Re: call up another db and editing it
Post by: rsw686 on April 12, 2008, 01:00:03 AM
This is an easy fix. I didn't see that $CONFIG wasn't included in the globals. At the top of the authenticate function change

global $USER_DATA, $user_settings;

to

global $USER_DATA, $user_settings, $CONFIG;

Works perfectly now. It created a "My album" automatically for my test user.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 12, 2008, 02:02:18 AM
didnt work :(
Title: Re: call up another db and editing it
Post by: rsw686 on April 12, 2008, 07:48:16 AM
Quote from: Hot Rides on April 12, 2008, 02:02:18 AM
didnt work :(

I have it working on a cleanly bridge coppermine and smf 1.1.4 install. Try my bridge file

http://wgnrs.dynalias.com/smf/smf10.inc.php
Title: Re: call up another db and editing it
Post by: Hot Rides on April 12, 2008, 10:46:09 PM
Quote from: rsw686 on April 12, 2008, 07:48:16 AM
Quote from: Hot Rides on April 12, 2008, 02:02:18 AM
didnt work :(

I have it working on a cleanly bridge coppermine and smf 1.1.4 install. Try my bridge file

http://wgnrs.dynalias.com/smf/smf10.inc.php
Still 'fatal error'
Right before this I updated Coppermine to 1.4.17 for a sercuity update and Im also running Stramm's Modpak on it. That might be interfering as well.
Title: Re: call up another db and editing it
Post by: rsw686 on April 12, 2008, 10:53:43 PM
The best way to debug the fatal error message, since it doesn't give a line number, is to insert some echo statements so you can narrow down which one is causing the fatal error.

The test site I have is running 1.4.16. I'll try updating it to 1.4.17 and seeing if that is the issue. Not sure about Stramm's Modpak.

EDIT: Just tried out 1.4.17. No problems with the above bridge file I provided you.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 14, 2008, 11:08:37 AM
where would I place the echo tags?
Title: Re: call up another db and editing it
Post by: Hot Rides on April 16, 2008, 06:42:28 AM
While executing query "SELECT aid FROM cpg_albums WHERE category = 10001" on Resource id #27

mySQL error: No database selected
Title: Re: call up another db and editing it
Post by: Hot Rides on April 16, 2008, 01:46:42 PM

if($user_settings['ID_MEMBER'] != 0){
$sql = "SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (10000 + $user_settings['ID_MEMBER']) ;
$result = cpg_db_query($sql, $this->link_id);

I did some research and this is causing the error, it never makes it to the rest of the code before crashing
Title: Re: call up another db and editing it
Post by: rsw686 on April 16, 2008, 02:30:11 PM
Right the error is from the database not being selected when the sql connection is made. However I don't see how that is possible as the class constructor initializes the database connection.  I've tested the code I supplied on two different gallierys and it worked on both. The only thing I can think of is a mod on your gallery is contributing to the problem.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 16, 2008, 04:09:47 PM
did either of them have stramms modpack?
Title: Re: call up another db and editing it
Post by: rsw686 on April 16, 2008, 04:32:55 PM
Quote from: Hot Rides on April 16, 2008, 04:09:47 PM
did either of them have stramms modpack?

Nope, can you give me a download link to that.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 16, 2008, 08:00:35 PM
 Modpack v1 for CPG 1.4.18 bridge support (http://stramm.st.funpic.org/albums/userpics/10001/stramm_mod1_4_18_bridge.zip)
Title: Re: call up another db and editing it
Post by: Hot Rides on April 17, 2008, 03:47:17 PM
just tried on a fresh install of coppermine and smf with no mods at all and it did not work at all.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 17, 2008, 05:25:21 PM
I found the problem while trying to fix another problem!!!

your original code

if($user_settings['ID_MEMBER'] != 0)
{
$sql = "SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (10000 + $user_settings['ID_MEMBER']);
$result = cpg_db_query($sql, $this->link_id);
if(mysql_num_rows($result) == 0)
{
$sql = "INSERT INTO {$CONFIG['TABLE_ALBUMS']} (title, category) VALUES ('My album', " . (10000 + $user_settings['ID_MEMBER']) . ")";
cpg_db_query($sql, $this->link_id);
}
mysql_free_result($result);
}


Replaced with my new code
if($user_settings['ID_MEMBER'] != 0)
{
$sql = "SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (10000 + $user_settings['ID_MEMBER']);
$result = cpg_db_query($sql);
if(mysql_num_rows($result) == 0)
{
$sql = "INSERT INTO {$CONFIG['TABLE_ALBUMS']} (title, category) VALUES ('My album', " .(10000 + $user_settings['ID_MEMBER']) . ")";
cpg_db_query($sql);
}
mysql_free_result($result);
}


not much difference except I removed the $this->link_id from the db queries

So now it works on the test site, we will see if it works on the live site.
Title: Re: call up another db and editing it
Post by: Hot Rides on April 17, 2008, 05:43:22 PM
Mark this thread as solved!

by the way, rsw686, what was the purpose of the $this->link_id ???
Title: Re: call up another db and editing it
Post by: rsw686 on April 17, 2008, 07:54:20 PM
Quote from: Hot Rides on April 17, 2008, 05:43:22 PM
by the way, rsw686, what was the purpose of the $this->link_id ???

The link_id is the mySQL resource to use for the database query. If you don't specify it, it will use the last open database connection. Technically the bridge should initialize that value for its class. I'm not sure why that is not happening on your system. If it works then I wouldn't worry too much about it.