D
D
Dmitry Antonov2016-08-06 17:37:19
MySQL
Dmitry Antonov, 2016-08-06 17:37:19

How to select resources by TV MIGX?

Good evening, I have resources with TV (MIGX) which uses checkbox and radio . The task is that I need to display resources depending on the values ​​of these checkbox and radio .
feec3fb62b574574bd6b979aa5e4a63c.png07b9364d6f884dfa9398693e86533664.png
For example, I need to display all the games that have " Action " selected in the Genre , and even better if it is possible to make a selection according to 2 criteria (For example, so that there are resources that have the genre Action and Racing , or Genre => Action and Activation Method = > Steam), but even if I can filter by 1 criterion, I will also be very happy.
And now let's move on to where the snag began.
I initially thought that everything was easy and just take the pdoResources snippet, connect TV to it via &includeTVs and use &tvFilters . But since TV is made via MIGX, it is stored in the database as JSON , and although the snippet converts it to an array, I don’t use the &tvFilters parameter and using &where is also not like JSON is stored in the database .
For example, what is in the database

[{"MIGX_id":"1","image":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/271590\/header.jpg?t=1459273772","alt_image":"GTAV","title_image":"GTAV","video":"1","janre":["\u042d\u043a\u0448\u0435\u043d ","\u0421\u0438\u043c\u0443\u043b\u044f\u0442\u043e\u0440\u044b ","\u0424\u0430\u0439\u0442\u0438\u043d\u0433\u0438 ","\u0414\u0438\u0437\u0430\u0439\u043d "],"activation":"Origin","platform":"Windows ","region":"\u0412\u0435\u0441\u044c \u043c\u0438\u0440 ","rejim":"\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u0435\u0440 ","news":"1"}]

As an array:
[tovar_info] => Array
                (
                    [0] => Array
                        (
                            [MIGX_id] => 1
                            [image] => http://cdn.akamai.steamstatic.com/steam/apps/271590/header.jpg?t=1459273772
                            [alt_image] => GTAV
                            [title_image] => GTAV
                            [video] => 1
                            [janre] => Array
                                (
                                    [0] => Экшен 
                                    [1] => Симуляторы 
                                    [2] => Файтинги 
                                    [3] => Дизайн 
                                )

                            [activation] => Origin
                            [platform] => Windows 
                            [region] => Весь мир 
                            [rejim] => Мультиплеер 
                            [news] => 1
                        )

                )

And now the most difficult thing for me.
Because I can’t use ready-made snippets and probably still don’t know how to create the right queries in &where and &tvFilters decided that it was time to write my own snippet (The more difficult the better, considering that I only know PHP superficially, and I don’t know the filling of MODX at all).
So, from the beginning, the thought was spinning in my head that I need to use the pdoResources functionality and add my own filter to it (For me, this is a function that takes values ​​\u200b\u200b(an array in this case) and one more variable is the key that we will look for in this array. Well this is probably not entirely correct, the input still needs 3 values, for example:
function migxFilter($array, $key, $val){
  
}

Where $array is an array obtained from MIGX converted already by MODX like this:
$val = $resource->getTVValue(2);
$val = $modx->fromJSON($val);

$key - should be a KEY so as not to get superfluous and to search exactly in a certain place (in this case, in a category, for example Genre , which is in the [janre] array ).
And of course $val is the value by which we will filter (For example, "Action").
Okay, we ran through the array and found a match .... It turns out that we just need to get boolean (true || false) . That is, we have the received resource, or rather, resources with this TV are probably better .
For example :
$pdo = $modx->getService('pdoFetch');
$parameters = array(
  'showUnpublished' => 1,
  'parents' => 2,
  'templates' => 4,
  'includeTVs' => 'Нужные TV',
);
$res = $pdo->getCollection('modResource', null, $parameters);

The $res variable will store a multidimensional array:
Array
(
    [0] => Array
        (
            [id] => 3
            [type] => document
            [contentType] => text/html
            [pagetitle] => Grand Theft Auto V
            [longtitle] => Купить игру GTA 5 / ГТА 5 / Grand Theft Auto V для STEAM
            [description] => 
            [alias] => grand-theft-auto-v
            [link_attributes] => 
            [published] => 1
            [pub_date] => 0
            [unpub_date] => 0
            [parent] => 4
            [isfolder] => 0
            [introtext] => 
            [content] => 
            [richtext] => 1
            [template] => 4
            [menuindex] => 0
            [searchable] => 1
            [cacheable] => 1
            [createdby] => 1
            [createdon] => 1468714413
            [editedby] => 1
            [editedon] => 1470168281
            [deleted] => 0
            [deletedon] => 0
            [deletedby] => 0
            [publishedon] => 1468714380
            [publishedby] => 1
            [menutitle] => 
            [donthit] => 0
            [privateweb] => 0
            [privatemgr] => 0
            [content_dispo] => 0
            [hidemenu] => 1
            [class_key] => modDocument
            [context_key] => web
            [content_type] => 1
            [uri] => grand-theft-auto-v.html
            [uri_override] => 0
            [hide_children_in_tree] => 0
            [show_in_tree] => 1
            [properties] => Array
                (
                    [autoredirector] => Array
                        (
                            [old_uri] => grand-theft-auto-v.html
                        )

                )

            [tovar_info] => Array
                (
                    [0] => Array
                        (
                            [MIGX_id] => 1
                            [image] => http://cdn.akamai.steamstatic.com/steam/apps/271590/header.jpg?t=1459273772
                            [alt_image] => GTAV
                            [title_image] => GTAV
                            [video] => 1
                            [janre] => Array
                                (
                                    [0] => Экшен 
                                    [1] => Симуляторы 
                                    [2] => Файтинги 
                                    [3] => Дизайн 
                                )

                            [activation] => Origin
                            [platform] => Windows 
                            [region] => Весь мир 
                            [rejim] => Мультиплеер 
                            [news] => 1
                        )

                )
        )
 и т.д
)

In these resources, we must get our array from TV (product_info) and apply our filter to it (which, alas, I myself could not write, or rather, I could make a function that runs through the array and looks for the desired value, and gives either true or false on the output )
function janr($array, $string){
      if(!is_array($array)){
        return false;  
      }
      static $da = false;
      foreach($array as $k=>$v){
          if(is_array($v)){
            janr($v, $string);  
          }else{
            if(trim($v) === $string){
              $da = true;
            }  
          }
          
      }
      return $da;
  }

And here my knowledge has completely dried up because I don’t know how to filter or add this filter to pdoResources . If you think about it, you can run foreach over the resources themselves, give the resource the TV key to get the values ​​\u200b\u200bof this TV and if there is a match, then display this resource on the screen using Chunk .
foreach($res as $tovar){
    $array = $tovar[tovar_info];
    if(janr($array,"Ролевые")){
     $pdo->getChunk(Чанк, $res)  //Вывoдим Чанк этого ресурса
    }  
}

But now I have a problem how can I display placeholders in a chunk . Do I really have to write there
In general, the train time is already 4:39, I can’t express my thoughts, and I probably don’t know how. Therefore, I ask you good people, please help with such questions?
  1. How to write a good snippet? What would be similar to pdoResources but with the ability to add your own filter as a function
  2. How to modify a filter that would accept 3 variables? (Unless of course it's not difficult and it's not difficult for you to write)
  3. How to make then normal placeholders? For example or just
  4. And what do you need to learn so that you can write Plugin snippets yourself and modify the MODX engine itself to fit your needs? And are there people who can teach? or indicate the right direction to achieve goals both in programming and in MODX itself. I'm not a lazy guy myself, but self-learning is not easy, I know that if someone helps, then you can learn many times faster

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question