source: internals/2017/FacebookAppAndFacebookPost/src/Adolfo/Commands/Search.php @ 16743

Last change on this file since 16743 was 16743, checked in by amoreno, 2 years ago

Search command fixed

File size: 3.1 KB
Line 
1<?php
2
3namespace Adolfo\Commands;
4
5class search
6{
7
8  public $isValid = true;
9  public $receivedMSG;
10  public $appIndex = 0;
11  public $storeIndex = 0;
12  public $app = array();
13  public $store = array();
14  public $answer = ["attachment"=>[
15    "type"=>"template",
16    "payload"=>[
17      "template_type"=>"generic",
18      "elements"=>[
19
20      ]
21    ]
22  ]];
23
24  public function __construct($receivedMSG = array())
25  {
26    $this->appIndex = array_search('app', $receivedMSG);
27    $this->storeIndex = array_search('store', $receivedMSG);
28
29    if($this->appIndex < $this->storeIndex){
30             for($i = $this->appIndex+1;$i<$this->storeIndex;$i++){
31                       $this->app[] = $receivedMSG[$i];
32             }
33             for($j = $this->storeIndex+1;$j<count($receivedMSG);$j++){
34                       $this->store[] = $receivedMSG[$j];
35             }
36    }else{
37             for($i = $this->storeIndex+1;$i<$this->appIndex;$i++){
38                       $this->store[] = $receivedMSG[$i];
39             }
40             for($j = $this->appIndex+1;$j<count($receivedMSG);$j++){
41                       $this->app[] = $receivedMSG[$j];
42             }
43    }
44
45    $this->app = implode(" ", $this->app);
46    $this->store = implode(" ", $this->store);
47  }
48
49  public function getData()
50  {
51    $encodedApp = urlencode($this->app);
52    $encodedStore = urlencode($this->store);
53    $contents = file_get_contents("https://ws75.aptoide.com/api/7/apps/search/query={$encodedApp}/store_name={$encodedStore}/limit=5");
54    if($contents!==false){
55      return $contents;
56    }else{
57        $this->isValid = false;
58        return null;
59    }
60  }
61
62  public function parseData($getDataReturn)
63  {
64    $toAdd = [
65      "title"=>"",
66      "item_url"=>"",
67      "image_url"=>"",
68      "subtitle"=>"",
69      "buttons"=>[
70        [
71          "type"=>"web_url",
72          "url"=>"",
73          "title"=>"Download app!"
74        ],
75        [
76          "type"=>"web_url",
77          "url"=>"en.aptoide.com",
78          "title"=>"Go to Aptoide"
79        ],
80        [
81          "type"=>"element_share"
82        ]
83      ]
84    ];
85    if($this->isValid){
86      $parseThis = json_decode($getDataReturn);
87      for($i = 0; $i < count($parseThis->datalist->list); $i++){
88        $app = file_get_contents("https://ws75.aptoide.com/api/7/app/getMeta?app_id=".$parseThis->datalist->list[$i]->id);
89        $decodedApp = json_decode($app);
90        if(strcmp($decodedApp->info->status,"OK")==0){
91          $toAdd['title'] = $decodedApp->data->name;
92          $toAdd['buttons'][0]['url'] = "https://en.aptoide.com/thank-you?app_id={$decodedApp->data->id}&store_name={$decodedApp->data->store->name}";
93          $toAdd['image_url'] = $decodedApp->data->icon;
94          $toAdd['item_url'] = $decodedApp->data->uname.".en.aptoide.com";
95          $toAdd['subtitle'] = $decodedApp->data->media->description;
96          array_push($this->answer['attachment']['payload']['elements'], $toAdd);
97        }
98      }
99    }else{
100      $this->answer = "The search is not valid or doesn't exist.";
101    }
102  }
103
104  public function createMSG()
105  {
106    if($this->isValid){
107      return $this->answer['attachment']['payload'];
108    }else{
109      return $this->answer;
110    }
111  }
112
113  public function isOK()
114  {
115    return $this->isValid;
116  }
117
118}
119
120?>
Note: See TracBrowser for help on using the repository browser.