public function getList()
 {
     $list = AUCMetric::get()->sort(['Identifier ASC', 'Expires ASC']);
     $params = $this->request->requestVar('q');
     if (!empty($params['MemberSearch'])) {
         $s = Convert::raw2sql($params['MemberSearch']);
         $list = $list->innerJoin('Member', 'FoundationMemberID = Member.ID')->where("\n                  \tMember.Email LIKE '%{$s}%'\n                  \tOR Member.SecondEmail LIKE '%{$s}%'\n                  \tOR Member.ThirdEmail LIKE '%{$s}%'\n                    OR (CONCAT_WS(' ', Member.FirstName, Member.Surname)) LIKE '%{$s}%'\n\t            ");
     }
     if (!empty($params['Metric'])) {
         $list = $list->filter('Identifier', $params['Metric']);
     }
     return $list;
 }
 /**
  * @throws ValidationException
  * @throws null
  */
 public function run()
 {
     SapphireTransactionManager::getInstance()->transaction(function () {
         SS_Log::log('Running AUCMetricTask', SS_Log::INFO);
         $expiredMetrics = AUCMetric::get()->where("Expires < DATE(NOW())");
         if ($expiredMetrics->exists()) {
             $msg = "Deleting {$expiredMetrics->count()} metrics.";
             $this->writeln($msg);
             SS_Log::log($msg, SS_Log::INFO);
             $expiredMetrics->removeAll();
         }
         $expiryDate = date('Y-m-d', strtotime(AUCMetric::config()->expiry));
         $newMetricCount = 0;
         $errorData = ArrayList::create();
         foreach ($this->services as $service) {
             $identifier = $service->getMetricIdentifier();
             $this->writeHeader("Running service {$identifier}");
             try {
                 $service->run();
             } catch (Exception $e) {
                 $this->writeln($e->getMessage());
                 SS_Log::log($e->getMessage(), SS_Log::ERR);
                 continue;
             }
             $results = $service->getResults();
             if (!$results) {
                 $this->writeln('No results!');
                 continue;
             }
             $members = $results->getMemberList();
             if (!$members) {
                 $this->writeln("--- no members found ---");
                 continue;
             }
             foreach ($members as $m) {
                 if ($m->hasAUCMetric($identifier)) {
                     continue;
                 }
                 $m->AUCMetrics()->add(AUCMetric::create(['Identifier' => $identifier, 'ValueDescription' => $service->getMetricValueDescription(), 'Value' => $results->getValueForMember($m), 'Expires' => $expiryDate])->write());
                 $this->writeln(sprintf("Added metric to %s %s %s", $m->Email, $service->getMetricValueDescription(), $results->getValueForMember($m)));
                 $newMetricCount++;
             }
             $errors = $service->getErrors();
             if (!empty($errors)) {
                 $errorList = ArrayList::create();
                 foreach ($errors as $e) {
                     $errorList->push(ArrayData::create(['Title' => $e]));
                 }
                 $serviceData = ArrayData::create(['Title' => $identifier, 'Errors' => $errorList]);
                 $errorData->push($serviceData);
             }
         }
         $msg = "Done. Created {$newMetricCount} new metric assignments";
         SS_Log::log($msg, SS_Log::INFO);
         $this->writeln($msg);
         if ($errorData->exists()) {
             $email = EmailFactory::getInstance()->buildEmail(null, $this->config()->email_to, 'AUC Metric task: A few things need your attention');
             $email->setTemplate('AUCMetricTaskEmail');
             $email->populateTemplate(['Services' => $errorData]);
             $email->send();
         }
     });
 }