public function findBySponsorId($sponsorId) { $sql = 'select f.* from files f, sponsor_files sf' . ' where f.id = sf.file_id and sf.sponsor_id = :sponsorId'; $results = $this->fetch($sql, ['sponsorId' => $sponsorId]); foreach ($results as $result) { $file = new \Conftrack\Model\File($this->getDb()); $file->load($result, false); $this->add($file); } }
public function handleUpload($fileData) { $uploadPath = APP_PATH . '/upload/sponsor/' . $this->id; if (realpath($uploadPath) === false) { mkdir($uploadPath); } $hash = sha1(file_get_contents($fileData['tmp_name'])); move_uploaded_file($fileData['tmp_name'], $uploadPath . '/' . $hash); // Now save a record for it in files $file = new \Conftrack\Model\File($this->getDb()); $file->load(['name' => $fileData['name'], 'hash' => $hash]); $file->save(); $sponsorFile = new \Conftrack\Model\SponsorFile($this->getDb()); $sponsorFile->load(['sponsor_id' => $this->id, 'file_id' => $file->id]); $sponsorFile->save(); }
$sponsors = new \Conftrack\Collection\Sponsors($this->getContainer()->get('db')); $sponsors->findAll(); $data = ['sponsors' => $sponsors->toArray(true)]; $this->view->render($response, 'sponsor/index.twig', $data); }); $app->get('/view/{sponsorId}', function ($request, $response, $args) { $db = $this->getContainer()->get('db'); $sponsor = new \Conftrack\Model\Sponsor($db); $sponsor->findById($args['sponsorId']); $users = new \Conftrack\Collection\Users($db); $users->findAll(); $data = ['sponsor' => $sponsor->toArray(), 'sponsorUsers' => $sponsor->users->toArray(true), 'users' => $users->toArray(true), 'files' => $sponsor->files->toArray(true), 'events' => $sponsor->events->toArray(true)]; $this->view->render($response, 'sponsor/view.twig', $data); }); $app->get('/{sponsorId}/file/{hash}', function ($request, $response, $args) { $file = new \Conftrack\Model\File($this->getContainer()->get('db')); $file->find(['hash' => $args['hash']]); if ($file->sponsor->id !== $args['sponsorId']) { throw new \Exception('Invalid file!'); } echo file_get_contents(APP_PATH . '/upload/sponsor/' . $file->sponsor->id . '/' . $file->hash); }); $app->get('/create', function ($request, $response, $args) { $users = new \Conftrack\Collection\Users($this->getContainer()->get('db')); $users->findAll(); $data = ['users' => $users->toArray(true)]; $this->view->render($response, 'sponsor/create.twig', $data); }); $app->post('/create', function ($request, $response, $args) { $body = $request->getParsedBody(); $container = $this->getContainer();