Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: diegolyanky on November 11, 2018, 06:03:59 PM

Title: A list of attached files...
Post by: diegolyanky on November 11, 2018, 06:03:59 PM
Hi all:

I'm trying to make a MOD wich allow to have a list of attached files...

Is this ok ? I made this MOD (see attached file), install without error, but I have no results on the list. This mod, make a new template wit the $result of query.

I don't understand at all 2.0.x ... I know only 1.1.x smf versions.

Can anyone help me ? :)

Thanks a lot !
Title: Re: A list of attached files...
Post by: SychO on November 12, 2018, 05:32:29 PM
I haven't looked at the code, but why is there a .mod file ? its contents should be made into a .xml file with operations just like the other two
Title: Re: A list of attached files...
Post by: vbgamer45 on November 12, 2018, 05:33:36 PM
.mod is the older package format is plain text format.
Title: Re: A list of attached files...
Post by: SychO on November 12, 2018, 05:37:48 PM
I see, 1.x I assume, old indeed
Title: Re: A list of attached files...
Post by: diegolyanky on November 12, 2018, 05:57:32 PM
Quote from: vbgamer45 on November 12, 2018, 05:33:36 PM
.mod is the older package format is plain text format.

Quote from: SychO on November 12, 2018, 05:37:48 PM
I see, 1.x I assume, old indeed


That's right. That's old indeed... This is the new one, under plain text format ( xml )

I tryed, and changed many query strings, by modifying to db_query ==>  $ smcFunc ['db_query'] ... and others like

mysql_fetch_assoc   ( now is $smcFunc ['db_fetch_assoc'] )
mysql_fetch_row   (now is $smcFunc ['db_fetch_row'] )

But, I don't understand how to call to a subtemplate... My brain will explode !! ...


I'm learning everything about the smf 2.x branch ... Reading all tutorial I find, as well as the smf wiki of developers

Here, I attach the modified file ...
Could someone please help me and tell me what I'm doing wrong here?

Many thanks in advance for helping me.

Title: Re: A list of attached files...
Post by: SychO on November 12, 2018, 06:02:57 PM
I don't know about 1.x, but there is no browse_special sub template in the ManageAttachements Template file in 2.0
Title: Re: A list of attached files...
Post by: diegolyanky on November 12, 2018, 06:19:23 PM
Quote from: SychO on November 12, 2018, 06:02:57 PM
I don't know about 1.x, but there is no browse_special sub template in the ManageAttachements Template file in 2.0


In 1.x either... It's supposed that template will be created by the mod.



Title: Re: A list of attached files...
Post by: SychO on November 13, 2018, 11:14:34 AM
Right, it is created by the mod, instead of using $boarddir/Themes/default/ManageAttachments.template.php you can just use $themedir/ManageAttachments.template.php, also there is a syntax error with your code

syntax error, unexpected 'else' (T_ELSE)
Title: Re: A list of attached files...
Post by: diegolyanky on November 13, 2018, 12:41:38 PM
Thanks SychO ...

Yes, me too see that error.

I can't find where is the error...

Do you think it's a permissions issue ? ...

Nice themes are you developing  ;)
Title: Re: A list of attached files...
Post by: SychO on November 13, 2018, 02:29:26 PM
permissions ? No

you are adding two functions inside of a function, you have to change the position after to before
<search position="after"><![CDATA[
$_SESSION['attachments_to_fix'] = $to_fix;
$_SESSION['attachments_to_fix2'] = $context['repair_errors'];

obExit();
}]]></search>

<search position="before"><![CDATA[
$_SESSION['attachments_to_fix'] = $to_fix;
$_SESSION['attachments_to_fix2'] = $context['repair_errors'];

obExit();
}]]></search>
Title: Re: A list of attached files...
Post by: diegolyanky on November 13, 2018, 02:57:15 PM
SychO:

Thanks for your reply ...

But, I tried that ... Also i'm getting same error

syntax error, unexpected 'user' (T_STRING), expecting ',' or ')'


Another idea ? ... It appears to be bad closed the function, but I can't see the error ... ( need a new brain lol )
Title: Re: A list of attached files...
Post by: SychO on November 13, 2018, 03:16:43 PM
You just need to add a closing curly bracket for the second function
Title: Re: A list of attached files...
Post by: diegolyanky on November 13, 2018, 03:30:50 PM
Quote from: SychO on November 13, 2018, 03:16:43 PM
You just need to add a closing curly bracket for the second function

:o Where ? ...

The two functions, has its opening and closing bracket.
Or am I seeing wrong?

Where do you see that ? ...
Title: Re: A list of attached files...
Post by: SychO on November 13, 2018, 03:55:54 PM
...

<add><![CDATA[function BrowseAttachments()
{
global $context, $smcFunc, $txt, $scripturl, $options, $modSettings, $user_info;

//Let's see who is allowed to see the list?
//No Guests :P
if($context['user']['is_guest'])
redirectexit();
   //Okay now let's work a little bit :x
else {
//Admins are everytime allowed :P
if(!$context['user']['is_admin']) {
if(empty($modSettings['Allowed_Attachments_Groups']))
redirectexit();
else {
//Clean the ModSettings i will only numbers and ,
preg_match_all('/(?:([0-9,]+)|.)/i', $modSettings['Allowed_Attachments_Groups'], $matches);
$allow_array = implode('', $matches['1']);
$allow_array = explode(',', $allow_array);
if(empty($allow_array))
redirectexit();
else {
$noredirect = FALSE;
//Huf why could it not simpler, i hate it.
foreach($allow_array as $gid) {
$noredirect = in_array($gid, $user_info['groups']);
if($noredirect) break;
}
//Okay he or she is not in the Allowed Groups... go back to the index :P
if(!$noredirect)
redirectexit();
}
}
}
}

//Language
loadLanguage('Admin');

//Need to Check something
$_REQUEST['sa'] = empty($_REQUEST['sa']) ? '' : $_REQUEST['sa'];

//Okay did you Serach something? Prepare and Redirect!
if(!empty($_REQUEST['search'])) {
$context['search'] = strtolower(trim(preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', htmlspecialchars($_REQUEST['search']))));
$context['search'] = addslashes($context['search']);

//Okay i simple encode it :) better browser compatible!
$context['hidden_search'] = base64_encode($context['search']);

//Okay go to the serach :x
redirectexit("action=BrowseAttachments;search2=".$context['hidden_search'] . (isset($_REQUEST['searchoption']) ? ';'.$_REQUEST['searchoption'] : ''));
}

//The Real Search is now working?
elseif(!empty($_GET['search2'])) {
SearchFiles();
}

//Okay only Browsing the files xD
else {
//Load the Attachements :P
BrowseFiles();

    //Creat a new Page Index!
$context['page_index'] = constructPageIndex($scripturl . '?action=BrowseAttachments' . ($context['browse_type'] == 'attachments' ? '' : ';' . $context['browse_type']) . ';sort=' . $context['sort_by'] . ($context['sort_direction'] == 'down' ? ';desc' : ''), $_REQUEST['start'], $context['num_' . $context['browse_type']], $modSettings['defaultMaxMessages']);
$context['sub_template'] = 'browse_special';
}

//Finish it!
loadTemplate('ManageAttachments');
}

function SearchFiles()
{
global $context, $smcFunc, $txt, $scripturl, $options, $modSettings;

$context['page_title'] = $txt['attachments_avatars'];
$context['description'] = $txt['smf202'];
$context['selected'] = 'search';
$context['sub_template'] = 'browse_special';

// Attachments or avatars?
$context['browse_type'] = 'attachments';

// Some preparing ;)
if(!empty($_REQUEST['search2'])) {
$context['search'] = base64_decode($_REQUEST['search2']);
$context['hidden_search'] = $_REQUEST['search2'];
}
else {
redirectexit("action=BrowseAttachments");
}

$context['search_option'] = (isset($_REQUEST['each']) ? 'each' : isset($_REQUEST['word']) ? 'word' : '');

//Okay you want to search each splited word... (Huge...)
if(isset($_REQUEST['each'])) {
$index = split(" ", $context['search']);
$index = array_unique($index);
reset($index);
//Okay lets generate the like index :P
$context['search_query_standard'] = "
filename LIKE '%".implode("%'
AND filename LIKE '%", $index)."%'";
$context['search_query_standard_a'] = "
a.filename LIKE '%".implode("%'
AND a.filename LIKE '%", $index)."%'";
}
elseif(isset($_REQUEST['word'])) {
$index = split(" ", $context['search']);
$index = array_unique($index);
reset($index);
//Okay lets generate the like index :P
$context['search_query_standard'] = "
(filename LIKE '%".implode("%'
OR filename LIKE '%", $index)."%')";
$context['search_query_standard_a'] = "
(a.filename LIKE '%".implode("%'
OR a.filename LIKE '%", $index)."%')";
}
else {
$context['search_query_standard'] = "filename LIKE '%".$context['search']."%'";
$context['search_query_standard_a'] = "a.filename LIKE '%".$context['search']."%'";
}

if ($browse_type === 'avatars')
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}attachments
WHERE id_member != {int:guest_id_member}',
array(
'guest_id_member' => 0,
)
)
else {
$request = $smcFunc['db_query']('', '
SELECT COUNT(*) AS num_attach
FROM {db_prefix}attachments AS a
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
WHERE a.attachment_type = {int:attachment_type}
AND a.id_member = {int:guest_id_member}',
array(
'attachment_type' => $browse_type === 'thumbs' ? '3 : '0',
'guest_id_member' => 0,
)
);
}

list ($num_files) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

return $num_files;

// Also get the avatar amount.
$request = $smcFunc['db_query']('', '
SELECT COUNT(id_attach)
FROM {db_prefix}attachments
WHERE id_member != 0
AND $context[search_query_standard]',
);
$smcFunc['db_free_result']($request);

// Allow for sorting of each column...
$sort_methods = array(
'name' => 'a.filename',
'date' => $context['browse_type'] == 'avatars' ? 'mem.last_Login' : 'm.id_msg',
'size' => 'a.size',
'member' => 'mem.real_name'
);

// Set up the importantant sorting variables... if they picked one...
if (!isset($_GET['sort']) || !isset($sort_methods[$_GET['sort']]))
{
$_GET['sort'] = 'date';
$descending = !empty($options['view_newest_first']);
}
// ... and if they didn't...
else {
$descending = isset($_GET['desc']);
}
$context['sort_by'] = $_GET['sort'];
$_GET['sort'] = $sort_methods[$_GET['sort']];
$context['sort_direction'] = $descending ? 'down' : 'up';

// Get the page index ready......
if (!isset($_REQUEST['start']) || $_REQUEST['start'] < 0)
$_REQUEST['start'] = 0;

$context['page_index'] = constructPageIndex($scripturl . '?action=BrowseAttachments;search2=' . $context['hidden_search'] . (!empty($context['search_option']) ? ';'.$context['search_option'] : '') . ';sort=' . $context['sort_by'] . ($context['sort_direction'] == 'down' ? ';desc' : ''), $_REQUEST['start'], $context['num_' . $context['browse_type']], $modSettings['defaultMaxMessages']);
$context['start'] = $_REQUEST['start'];

// Choose a query depending on what we are viewing.
if ($context['browse_type'] == 'avatars')
$request = $smcFunc['db_query']('', '
SELECT
'' AS id_msg, IFNULL(mem.real_Name, '$txt['not_applicable']') AS poster_Name, mem.last_Login AS poster_time, 0 AS ID_TOPIC, a.id_member,
a.ID_ATTACH, a.filename, a.attachment_Type, a.size, a.width, a.height, a.downloads, '' AS subject, 0 AS ID_BOARD
FROM {db_prefix}attachments AS a
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
WHERE a.id_member != 0
AND $context[search_query_standard_a]
ORDER BY $_GET[sort] ' . ($descending ? 'DESC' : 'ASC') . '
LIMIT $context[start], $modSettings[defaultMaxMessages]',
);
else {
$request = $smcFunc['db_query']('', '
SELECT
m.id_msg, IFNULL(mem.real_Name, m.poster_Name) AS poster_Name, m.poster_time, m.ID_TOPIC, m.id_member,
a.ID_ATTACH, a.filename, a.attachment_Type, a.size, a.width, a.height, a.downloads, mf.subject, t.ID_BOARD
FROM ({db_prefix}attachments AS a, {db_prefix}messages AS m, {db_prefix}topics AS t, {db_prefix}messages AS mf)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE a.id_msg = m.id_msg
AND a.attachment_Type = ' . ($context['browse_type'] == 'attachments' ? '0' : '3') . '
AND t.ID_TOPIC = m.ID_TOPIC
AND mf.id_msg = t.ID_FIRST_MSG
AND $context[search_query_standard_a]
ORDER BY $_GET[sort] ' . ($descending ? 'DESC' : 'ASC') . '
LIMIT $context[start], $modSettings[defaultMaxMessages]',
);
}

$context['posts'] = array();
while ($row = $smcFunc['db_fetch_assoc'] ($result))
$context['posts'][] = array(
'id' => $row['id_msg'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_Name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_Name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_Name'] . '</a>'
),
'time' => empty($row['poster_time']) ? $txt['never'] : timeformat($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
'attachment' => array(
'id' => $row['ID_ATTACH'],
'size' => $_FILES['attachment']['size'][$n],
'width' => $row['width'],
'height' => $row['height'],
'name' => $_FILES['attachment']['name'][$n],
'downloads' => $row['downloads'],
'href' => $row['attachment_Type'] == 1 ? $modSettings['custom_avatar_url'] . '/' . $row['filename'] : ($scripturl . '?action=dlattach;' . ($context['browse_type'] == 'avatars' ? 'type=avatar;' : 'topic=' . $row['ID_TOPIC'] . '.0;') . 'id=' . $row['ID_ATTACH']),
'link' => '<a href="' . ($row['attachment_Type'] == 1 ? $modSettings['custom_avatar_url'] . '/' . $row['filename'] : ($scripturl . '?action=dlattach;' . ($context['browse_type'] == 'avatars' ? 'type=avatar;' : 'topic=' . $row['ID_TOPIC'] . '.0;') . 'id=' . $row['ID_ATTACH'])) . '"' . (empty($row['width']) || empty($row['height']) ? '' : ' onclick="return reqWin(this.href + \';image\', ' . ($row['width'] + 20) . ', ' . ($row['height'] + 20) . ', true);"') . '>' . $row['filename'] . '</a>'
),
'topic' => $row['ID_TOPIC'],
'subject' => $row['subject'],
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['subject'] . '</a>'
);

$smcFunc['db_free_result']($request);]]></add>

^^^^ here
Title: Re: A list of attached files...
Post by: diegolyanky on November 13, 2018, 04:13:34 PM
SychO:

I do not know what happened ... Maybe, I confused the file I uploaded.
Here is the correct one, and it has the close brackets in its places.

Sorry for that...

Likewise, it does not work and still giving same error.



syntax error, unexpected 'user' (T_STRING), expecting ',' or ')'



No errors on Log Error Admin section...

Maybe, I'm calling any property of 'user' with error ?

Why expecting ',' or ')'   ?

Title: Re: A list of attached files...
Post by: SychO on November 13, 2018, 04:29:30 PM
It's just a syntax error, you probably missed a semicolon or something somewhere

Title: Re: A list of attached files...
Post by: Shambles on November 13, 2018, 04:32:14 PM
Copy & Paste your code blocks to a parsing website like https://www.piliapp.com/php-syntax-check/
Title: Re: A list of attached files...
Post by: diegolyanky on November 13, 2018, 04:42:36 PM
Quote from: Sh@mbles on November 13, 2018, 04:32:14 PM
Copy & Paste your code blocks to a parsing website like https://www.piliapp.com/php-syntax-check/

Sh@mbles ... Thanks for reply. I did it many times and no error found. Tried on piliapp - stackoverflow - and other sites with a php parsers

For that reason, I think it's a variable or a function specific of SMF 2.0 wich I don't understand yet.


Quote from: SychO on November 13, 2018, 04:29:30 PM
It's just a syntax error, you probably missed a semicolon or something somewhere


Thanks again ... Yeah, I thinked about a semicolon or something simple but I can't find it !

Title: Re: A list of attached files...
Post by: diegolyanky on November 13, 2018, 04:46:11 PM
Quote from: diegolyanky on November 13, 2018, 04:13:34 PM
SychO:

I do not know what happened ... Maybe, I confused the file I uploaded.
Here is the correct one, and it has the close brackets in its places.


Title: Re: A list of attached files...
Post by: -Rock Lee- on November 13, 2018, 07:29:19 PM
I have some doubts about the issue of permits but it would work fairly as it should.


Regards!
Title: Re: A list of attached files...
Post by: Biology Forums on November 30, 2018, 05:19:49 PM
Could someone reexplain what this mod does?
Title: Re: A list of attached files...
Post by: vbgamer45 on November 30, 2018, 05:20:47 PM
Shows a list of attachments
Title: Re: A list of attached files...
Post by: diegolyanky on November 30, 2018, 05:38:40 PM
It's supposed will provide a list of attached files on a forum.
With the functionallity of can search trought filenames of attached files.

But it's still on developing... Can you help?  :)
Title: Re: A list of attached files...
Post by: Arantor on November 30, 2018, 05:52:03 PM
What does that offer that 2.0's admin panel doesn't?
Title: Re: A list of attached files...
Post by: vbgamer45 on November 30, 2018, 05:53:08 PM
He is looking for a way for normal membergroups to view the information.
Title: Re: A list of attached files...
Post by: Arantor on November 30, 2018, 05:54:56 PM
Like https://live627.com/livemods.net/index.php?action=mods#59 maybe?
Title: Re: A list of attached files...
Post by: diegolyanky on November 30, 2018, 05:58:35 PM



:o



Title: Re: A list of attached files...
Post by: Biology Forums on December 01, 2018, 10:28:33 AM
@arantor, I don't think that mod has a search feature

I think what you should do instead is create a checkbox in the search template that specifies you want to search the table that houses the attachments only -- that is if your members actually give their uploads meaningful names and titles. Then, create a function within search.php that only searches that table and presents the results normally. Of course this is just an idea, not a blueprint but that's how i'd approach it rather than creating dedicated template files, etc.
Title: Re: A list of attached files...
Post by: shawnb61 on December 01, 2018, 10:42:09 AM
Wouldn't there be permissions issues?  Attachments should honor board permissions, correct?  If you don't have permission to see the board, you shouldn't see the board's attachments.



Title: Re: A list of attached files...
Post by: Arantor on December 01, 2018, 10:51:57 AM
I continue to be amazed at how "just add a checkbox" conveniently ignores the rest of the actual complexity, and I don't think solves the problem either.
Title: Re: A list of attached files...
Post by: shawnb61 on December 01, 2018, 10:59:49 AM
For some types of  forums, I think the ability to navigate attachments would be extremely helpful. 

This is how I approached it on my forum.  Far from perfect, but functional:
https://www.vguitarforums.com/smf/index.php?topic=24401.0

A nightly batch job updates this post & also puts out a .csv with the same content who want the additional ability to search/navigate in Excel. 

This only works for my forum because the permissions are flat (it's either public or not, & I filter out attachments for "backstage" boards) and because the # of attachments that meet my search criteria (patches) is relatively small (<4000).   

Speaking of big posts, that's a big post... 
Title: Re: A list of attached files...
Post by: Biology Forums on December 01, 2018, 01:57:38 PM
I mentioned it's NOT a blueprint. Obviously there would be a boat load of coding front and backend. The checkbox idea would be a miniscule template modification.
Title: Re: A list of attached files...
Post by: Arantor on December 01, 2018, 02:29:01 PM
Yes, with the intent of "reducing work" when in reality it would be better all round (and less work) to not try to modify the search code.
Title: Re: A list of attached files...
Post by: diegolyanky on December 01, 2018, 04:45:46 PM
Quote from: shawnb61 on December 01, 2018, 10:59:49 AM


This is how I approached it on my forum.  Far from perfect, but functional:
https://www.vguitarforums.com/smf/index.php?topic=24401.0





I would like to have that on my site. Where can I get it?  :o
Title: Re: A list of attached files...
Post by: shawnb61 on December 05, 2018, 03:38:23 PM
I've been thinking about this.

Limitations:
- No documentation, as I only really use it internally at the moment. 
- The install package I've created only works for 2.0.x. 
- I haven't put licensing info in it, as I wasn't sure I'd distribute it.  It's more a toolbox than a mod. 
- As noted earlier, "flat" board permissions.
- As noted earlier, < 4000 attachments suggested.   

If you're OK with these limitations, I'll put it up on Github.  If there is more interest, I'll add documentation.