<?php 
//GET CUSTOM FIELDS FROM ARTICLES
// $customFieldnames = FieldsHelper::getFields('com_content.article', $item->id, true);
// $customFieldIds = array_column($customFieldnames, 'id'); //get custom field Ids by custom field names
// $fields_title = array_column($customFieldnames, 'title'); //get custom field Ids by custom field names
// $model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true)); //load fields model
// $customFieldValues = $model->getFieldValues($customFieldIds , 460); //Fetch values for custom field Ids
// var_dump($fields_title);
// var_dump($customFieldValues)

// foreach ($variable as $key => $value) {

//     foreach ($customFieldnames as $key => $value) {
//         echo $key;
//     }
// }

//GET BASIC FIELDS FROM ARTICLES(IMG,CONTENT TEXT)
// $id = JFactory::getApplication()->input->getInt('id');
// $article = JTable::getInstance("content");
// $article->load(460);
// echo $article->get("introtext");
// echo $article->get("images");

// var_dump($article);

// function articles_info()
// {
    
// }

//get all articles name by title
// $db = JFactory::getDbo();
// $some_value='2CHA-2500-2,8';
// $query = $db
//     ->getQuery(true)
//     ->select('id')
//     ->from($db->quoteName('#__content'))
//     ->where($db->quoteName('title') . " = " . $db->quote($some_value));

// $db->setQuery($query);
// $result = $db->loadResult();
// $num = intval($result);
// var_dump($num)


//Get basic fields from article (images,introtext,etc...)
function basicFields($id_basicFields){
        //Get basic fields from article (images,introtext,etc...)
        $id = JFactory::getApplication()->input->getInt('id');
        $article = JTable::getInstance("content");
        $article->load($id_basicFields);
        // echo $article->get("introtext");
        //echo $article->get("images");
        if($article->get("introtext")==""){
           // echo "introtext vacio";
        }else{
            //echo $article->get("introtext");
    
        }
        $objFields=["introtext"=>$article->get("introtext"),"img"=>$article->get("images")];
        return $objFields;
}

function getAllCustomFields()
{
    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); //load fields helper
    $customFieldnames = FieldsHelper::getFields('com_content.article', $article_id, true); // get custom field names by article id
    $customFieldIds = array_column($customFieldnames, 'id' );//get custom field Ids by custom field names   
    $fields_title = array_column($customFieldnames, 'title'); //titles from all id
    $model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true)); //load fields model
    $customFieldValues = $model->getFieldValues($customFieldIds ,441); //Fetch values for custom field Ids
   // var_dump($customFieldValues);
    //var_dump($customFieldnames);

    //Put fields title with them value 
    $array_fields_value =array(); 
    foreach ($customFieldnames as $key => $value) {
     //   var_dump($value->{id});
        // if($value->{id}==)
        echo "<br>";
        $id_field_selected=$value->{id};
        $title_field_selected=$value->{title};
        foreach ($customFieldValues as $key => $value) {
            if($id_field_selected==$key)
            {
              //  echo $title_field_selected." = ".$value;
                $array_fields_value[] = (object) [$title_field_selected => $value];
            }
            // echo $key;
            // echo "<br>";
           

        }
    }
    var_dump($array_fields_value);
   // var_dump($customFieldValues);
}
getAllCustomFields();
//Function to get all data from article by title
function articles_info($article_title)
{
    //get id of article by title
    $db = JFactory::getDbo();
    // $some_value='2CHA-2500-2,8';
    $query = $db
    ->getQuery(true)
    ->select('id')
    ->from($db->quoteName('#__content'))
    ->where($db->quoteName('title') . " = " . $db->quote($article_title));

    $db->setQuery($query);
    $result = $db->loadResult();
    $id_article = intval($result);
    //var_dump($id_article);

  basicFields($id_article);
  getAllCustomFields($id_article);

}
//articles_info('2CHA-3000-2,8');



?>