/** * Index method * * @return \Cake\Network\Response|null */ public function index() { $releases = $this->Releases->find('all')->all(); $collection = new Collection($releases); $attribute_names = array_unique($collection->extract('attribute_name')->toArray()); $idps = array_unique($collection->extract('idp')->toArray()); $releasesByIdp = $collection->groupBy('idp')->toArray(); foreach ($idps as $idp) { foreach ($attribute_names as $attribute) { $releasesByIdPbyAttribute = $collection->match(['idp' => $idp, 'attribute_name' => $attribute]); $temp_result = $releasesByIdPbyAttribute->countBy(function ($result) { return strtolower($result->validated) == 'fail' ? 'fail' : 'pass'; }); $results[$idp][$attribute] = $temp_result->toArray(); } } # My attributes $persistentid_array = preg_split('/!/', $this->request->env('persistent-id')); $persistentid = end($persistentid_array); $myAttributesTemp = $this->Releases->find()->andWhere(['idp' => $this->request->env('Shib-Identity-Provider'), 'persistentid' => $persistentid])->all(); $myAttributesCollection = new Collection($myAttributesTemp); $myAttributes = $myAttributesCollection->groupBy('attribute_name')->toArray(); $this->set(compact('myAttributes')); $this->set('_serialize', ['myAttributes']); $this->set(compact('results')); $this->set('_serialize', ['results']); $this->set(compact('idps')); $this->set('_serialize', ['idps']); $this->set(compact('attribute_names')); $this->set('_serialize', ['attribute_names']); }
/** * Tests extract * * @return void */ public function testExtract() { $items = [['a' => ['b' => ['c' => 1]]], 2]; $collection = new Collection($items); $map = $collection->extract('a.b.c'); $this->assertInstanceOf('\\Cake\\Collection\\Iterator\\ExtractIterator', $map); $this->assertEquals([1, null], iterator_to_array($map)); }
public function getLibrary() { $this->viewBuilder()->layout("ajax"); $this->autoRender = false; $this->loadModel('Playlists'); $this->loadModel('Tracks'); $userId = $this->request->session()->read('user.id'); $library = $this->Playlists->find('all')->where(['user_id' => $userId])->contain('Tracks'); if ($library->count() == 0) { echo json_encode(['success' => false]); return; } $library = $library->toArray(); // remove tracks from playlists $libraryCollection = new Collection($library); $playlists = $libraryCollection->extract(function ($list) { unset($list->tracks); return $list; }); // if tokens is expired get new downloadUrls // GAUTH: is token expired $gotUpdatedDownloadUrls = false; $client = new \Google_Client(); $client->setAuthConfigFile('../client_secret.json'); $client->addScope(\Google_Service_Drive::DRIVE); if ($this->request->session()->check('user.drive_token')) { $client->setAccessToken($this->request->session()->read('user.drive_token')); if ($client->isAccessTokenExpired()) { $gotUpdatedDownloadUrls = true; } } else { $gotUpdatedDownloadUrls = true; } $driveFiles = $this->getMusicFromDrive(); $driveFileUrlById = (new Collection($driveFiles))->combine('id', function ($entity) { return str_replace("?e=download&gd=true", "", $entity['downloadUrl']); })->toArray(); // create tracks by playlist id, also update new download_url if gotten it $tracksByPlaylistId = []; $tracksById = []; foreach ($library as $list) { $tracksByPlaylistId[$list->id] = []; foreach ($list->tracks as $track) { if ($gotUpdatedDownloadUrls) { $this->Tracks->patchEntity($track, ['download_url' => $driveFileUrlById[$track->drive_id]]); $this->Tracks->save($track); } $tracksByPlaylistId[$track->playlist_id][] = $track; $tracksById[$track->id] = $track; } } echo json_encode(['playlists' => $playlists, 'tracksByPlaylistId' => $tracksByPlaylistId, 'tracksById' => $tracksById]); }