News:

Support for jDownloads 3 has been ended
Since 17 August 2023 Joomla.org has discontinued support for Joomla 3.x. Therefore, we will no longer offer official support for our Joomla 3 jDownloads version 3.9.x from January 2024.
Please update your website to the latest Joomla version (Joomla 4 or Joomla 5) as soon as possible. Afterwards, please update jDownloads to the latest published version. The longer you delay, the more difficult the upgrade process for your website is likely to be.

Main Menu
Support-Forum

Get all fields for a specific download within an exterior PHP script

Started by rikoooo, 26.05.2021 13:32:18

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rikoooo

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
  •  

rikoooo

I really need some help here, at least direction ?

Thanks a lot in advance

Erik
  •  

ColinM

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
Colin M
  •  

rikoooo

Thank you very much Colin for your reply,

I will study that now.

Erik
  •  

rikoooo

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
  •  

rikoooo

  •  

ColinM

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
Colin M
  •  

rikoooo

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
  •  

ColinM

Erik
Yes it was sent
  A PM (Private Message) is not an email but a secure point to point  transmission
Colin
Colin M
  •  

rikoooo

I mean, it wasn't in my "Sent Items" from the forum PM system.

Many thanks,

Erik
  •  

ColinM

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
Colin M
  •