jDownloads Support Forum

jDownloads for Joomla 3.x => jDownloads 3.9 (Support ended) => Topic started by: rikoooo on 26.05.2021 13:32:18

Title: Get all fields for a specific download within an exterior PHP script
Post by: rikoooo on 26.05.2021 13:32:18
Hi,

I'm struggling to write a PHP code to find all the custom fields from a specific download within an exterior PHP script

How to retrieve the fields and their values ? Is there a specific function ? In Jdownloads 3.2 it was easy because the information was in table __jdownloads_files

Could you tell me what would be the code for version 3.9 ?

Thank you very much in advance


Erik
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: rikoooo on 28.05.2021 13:13:46
I really need some help here, at least direction ?

Thanks a lot in advance

Erik
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: ColinM on 28.05.2021 17:33:53
HiSome of the fields in this note were imported automatically in an upgrade from jD3.2.69 to jD3.9.7.3After the import I added a field Group and two more fields attached to that group
The Field definitions are held in database table __fields see pic 'from--__fields-table.png'This table is the basic setup of the Fields
The field groups  are held in table __fields_groups see pic 'from--__fields_groups-table.png'
The Values are held in table __fields_values-table.png see pic 'from--__fields_values-table.png'
Note that all the field related tables have 'com_jdownloads.download' in the 'context table so that would be used in any DB search.
Colin
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: rikoooo on 01.06.2021 10:12:54
Thank you very much Colin for your reply,

I will study that now.

Erik
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: rikoooo on 01.06.2021 11:55:26
I came out with this code, and I'm trying to adapt it to jdownloads custom field without success. It returns an empty array.

$article_id = '824'; //<-- Jdownloads file id
       
        JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); //load fields helper
        $customFieldnames = FieldsHelper::getFields('com_jdownloads.download', $article_id, true); // get custom field names by article id
        $customFieldIds = array_map(create_function('$o', 'return $o->id;'), $customFieldnames); //get custom field Ids by custom field names
        $model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true)); //load fields model
        $customFieldValues = $model->getFieldValues($customFieldIds , $item->id); //Fetch values for custom field Ids

print_r ($customFieldValues);


Thanks in advance for your lanterns,

Erik
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: rikoooo on 01.06.2021 12:33:24
I found my solution by adapting this code to jdwonloads :

https://joomla.stackexchange.com/a/28378/15340

Thank you
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: ColinM on 01.06.2021 15:39:10
Hi
Yes I had seen the first attempt in Stack Exchange and was about to say it needs an arrayBut the second one is very much better
Could you perhaps send me a PM  (click on the button under my name) with a copy of your code as it might be a future idea for a jD module or similar to list the Fields from selected Downloads

Colin
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: rikoooo on 01.06.2021 17:15:32
Not sure the message was sent as it didn't appear in my sent box, so I re-post it here :

Hi Colin, this is the code I use :

function getJCFields($srcId, $fieldId=null, $dbfields=null, $context=null)
{
    if( is_null($context) )
        $context = 'com_jdownloads.download';
   
    if( is_array($srcId) || is_object($srcId) ) {
        JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR.'/components/com_fields/helpers/fields.php');
        $jfields = FieldsHelper::getFields($context, $srcId, true);
    }
    else
    if( is_int($srcId) )
    {
        if( is_null($dbfields) )
            $dbfields = 'id,label,value,item_id';
       
        $db = JFactory::getDbo();
        $db->setQuery('
        SELECT '.$dbfields.'
        FROM #__fields
        LEFT JOIN #__fields_values ON #__fields.id = #__fields_values.field_id
        WHERE context = "'.$context.'"
        AND #__fields_values.item_id = '.$srcId.'
        AND #__fields.state = 1
        ');
       
        $jfields = $db->loadObjectList();
    }
   
    $fieldval=[];
    foreach($jfields as $val)
    {
        $fieldval[$val->id] = (object)['label'=>$val->label,'value'=>$val->value];
    }
   
    $fval = $fieldval;
    if( !is_null($fieldId) ) {
        $fval = $fieldval[$fieldId];
    }

    return $fval;
}


And to call the function:

$srcid = ['id'=>578];                         // ID (578) of the download that we want to get the customs fields

echo getJCFields($srcid, 1)->label;    // label of the custom field number 1

echo getJCFields($srcid, 1)->value;    // value of the custom field number 1


Thank you

Erik
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: ColinM on 01.06.2021 17:33:49
Erik
Yes it was sent
  A PM (Private Message) is not an email but a secure point to point  transmission
Colin
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: rikoooo on 01.06.2021 18:15:17
I mean, it wasn't in my "Sent Items" from the forum PM system.

Many thanks,

Erik
Title: Re: Get all fields for a specific download within an exterior PHP script
Post by: ColinM on 02.06.2021 14:36:35
Hi
The ones in your Sent ityems are only the ones you sent!!
Those sent to you are initialy in the New items.  It also sends you an email when you recieve a PMColin