public function searchAction()
 {
     $title = 'Search results';
     if (sizeof(Input::get()) == 1) {
         if (Input::hasGet('provider_id')) {
             $e = Provider::fetchOne(Input::get('provider_id'));
             $title = "Search results for internet provider <strong>{$e->name}</strong>";
         } else {
             if (Input::hasGet('brand_id')) {
                 $e = Brand::fetchOne(Input::get('brand_id'));
                 $title = "Search results for brand <strong>{$e->name}</strong>";
             } else {
                 if (Input::hasGet('package_id')) {
                     $e = Package::fetchOne(Input::get('package_id'));
                     $title = "Search results for package <strong>{$e->name}</strong>";
                 } else {
                     if (Input::hasGet('package_version_id')) {
                         $e = Package\Version::fetchOne(Input::get('package_version_id'));
                         $p = Package::fetchOne($e->package_id);
                         $title = "Search results for <strong>{$p->name}</strong> version {$e->value}";
                     }
                 }
             }
         }
     }
     $packageId = (int) Input::get('package_id');
     $packageVersionId = (int) Input::get('package_version_id');
     $brandId = (int) Input::get('brand_id');
     $osVersionId = (int) Input::get('os_version_id');
     $countryId = (int) Input::get('country_id');
     $providerId = (int) Input::get('provider_id');
     $productId = (int) Input::get('product_id');
     $modelId = (int) Input::get('model_id');
     $dateFrom = Input::get('date_from');
     $dateTo = Input::get('date_to');
     $stackTraceId = (int) Input::get('stack_trace_id');
     $table = BootstrapUI::tableRemote()->title($title)->extraParams(array('package_id' => $packageId, 'package_version_id' => $packageVersionId, 'brand_id' => $brandId, 'os_version_id' => $osVersionId, 'country_id' => $countryId, 'provider_id' => $providerId, 'product_id' => $productId, 'model_id' => $modelId, 'date_from' => $dateFrom, 'date_to' => $dateTo, 'stack_trace_id' => $stackTraceId))->column('country_name', '', 20)->column('created_at', 'time', 85)->column('package_name', 'package')->column('package_version', 'version')->column('brand_name', 'brand')->column('os_version_name', 'OS version', 105)->column('country', '')->column('action', '', 30)->sortableColumns(array('created_at', 'package_name', 'package_version', 'brand_name', 'os_version_name', 'cnt'))->sortField('created_at', 'desc');
     $table->panel()->addHeaderElement(Bootstrap::button('Back')->color('red')->size('xs')->setAttribute('onclick', 'window.history.back()'));
     return View::create('base')->with('title', $title)->with('content', Bootstrap::row()->add(12, $table));
 }
 public function viewAction()
 {
     $id = Url::getVar(1);
     // todo: ubaciti ovo u cache i odatle cupat van
     $cacheKey = "report-{$id}";
     $crash = CrashArchive::fetchOne($id);
     if ($crash === false) {
         Application::throwError(404, 'Can not find crash report ' . $id);
     }
     $title = "Crash Report #{$id}";
     $content = array();
     $panel = Bootstrap::panel($title)->color('blue')->addHeaderElement(Bootstrap::button('Back')->setAttribute('onclick', 'window.history.back()')->color('red')->size('xs'));
     $table = Bootstrap::table();
     $table->column('id', '')->column('value', '');
     // time
     $table->row(array('id' => 'time', 'value' => Misc::userDate('Y-m-d H:i:s', $crash->created_at) . ' (' . $this->user['timezone'] . ')'));
     // package
     $e = $crash->getPackage();
     $v = $crash->getPackageVersion();
     $table->row(array('id' => 'package and version', 'value' => implode(' ', array($e !== null ? "{$this->getSearchLink('package_id', $e->id, $e->name)} {$this->getLabel($e->total)}" : 'unknown', $v !== null ? "{$this->getSearchLink('package_version_id', $v->id, $v->value)} {$this->getLabel($v->total)}" : 'unknown'))));
     // device
     $value = '';
     $e = $crash->getBrand();
     if ($e === null) {
         $value .= 'unknown brand<br/>';
     } else {
         $value .= "{$this->getSearchLink('brand_id', $e->id, $e->name)} {$this->getLabel($e->total)}<br/>";
     }
     $e = $crash->getPhoneModel();
     if ($e === null) {
         $value .= 'unknown phone model<br/>';
     } else {
         $value .= "{$this->getSearchLink('model_id', $e->id, $e->name)} {$this->getLabel($e->total)}<br/>";
     }
     $table->row(array('id' => 'device', 'value' => substr($value, 0, -5)));
     // product
     $e = $crash->getProduct();
     if ($e !== null) {
         $table->row(array('id' => 'product name', 'value' => "{$this->getSearchLink('product_id', $e->id, $e->name)} {$this->getLabel($e->total)}"));
     }
     // os
     $e = $crash->getOsVersion();
     $table->row(array('id' => 'OS', 'value' => $e === null ? 'unknown' : "{$this->getSearchLink('os_version_id', $e->id, "{$e->os} {$e->name}")} {$this->getLabel($e->total)}"));
     // user comment
     if ($crash->user_comment !== null && trim($crash->user_comment) != '') {
         $table->row(array('id' => 'user comment', 'value' => $crash->user_comment));
     }
     // user email
     if ($crash->user_email !== null && trim($crash->user_email) != '') {
         $table->row(array('id' => 'user email', 'value' => $crash->user_email));
     }
     // app lifetime
     if ($crash->user_app_start_date !== null && $crash->user_crash_date !== null) {
         $table->row(array('id' => 'app lifetime', 'value' => "{$crash->user_app_start_date}<br/>{$crash->user_crash_date} (duration: {$this->duration($crash->user_app_start_date, $crash->user_crash_date)})"));
     }
     // memory usage
     $table->row(array('id' => 'available / total memory size', 'value' => Convert::bytesToString($crash->available_mem_size) . ' / ' . Convert::bytesToString($crash->total_mem_size)));
     // country
     if ($crash->country_id !== null) {
         $country = Country::fetchOne($crash->country_id);
         $table->row(array('id' => 'country', 'value' => "<img src=\"" . Url::link("img/flag/{$country->tld}.png") . "\" /> <a href=\"" . Url::href('reports', 'search', array('country_id' => $crash->country_id)) . "\">{$country->country} (" . strtoupper($country->tld) . ")</a> " . Bootstrap::label($country->total)->color('red')));
     }
     // provider
     if ($crash->provider_id !== null) {
         $e = Provider::fetchOne($crash->provider_id);
         $table->row(array('id' => 'internet provider', 'value' => "{$this->getSearchLink('provider_id', $e->id, $e->name)} {$this->getLabel($e->total)}"));
     }
     $metas = $crash->getMetas();
     $toTabs = array();
     foreach ($metas as $key => $value) {
         if ($key != 'stack_trace') {
             if (strpos(trim($value), "\n") === false) {
                 $table->row(array('id' => str_replace('_', ' ', $key), 'value' => trim($value) == '' ? '<em>empty</em>' : $value));
             } else {
                 $toTabs[] = $key;
             }
         }
     }
     $toTabsUnknown = array();
     $unknownMetas = $crash->getUnknownMetas();
     foreach ($unknownMetas as $key => $value) {
         if (strpos(trim($value), "\n") === false) {
             $table->row(array('id' => str_replace('_', ' ', $key), 'value' => (trim($value) == '' ? '<em>empty</em>' : $value) . ' ' . Bootstrap::label('unknown meta')->color('lightblue')));
         } else {
             $toTabsUnknown[] = $key;
         }
     }
     if ($crash->stack_trace_id !== null) {
         $table->row(array('id' => 'find reports with this stack trace', 'value' => Bootstrap::anchor(\Bootstrap::icon('search'), Url::href('reports', 'search', array('stack_trace_id' => $crash->stack_trace_id)))->asButton()->color('red')->size('xs')));
     }
     $panel->content($table);
     $content[] = Bootstrap::row()->add(12, $panel);
     $tabs = Bootstrap::nav();
     if (isset($metas['stack_trace'])) {
         $tabs->addLink('stack trace', "<pre class=\"text-danger\">{$metas['stack_trace']}</pre>");
     } else {
         if ($crash->stack_trace_id !== null) {
             $stackTrace = Stack\Trace::fetchOne($crash->stack_trace_id);
             $tabs->addLink('stack trace summary', "<pre class=\"text-danger\">{$stackTrace->summary}</pre>");
         }
     }
     if (sizeof($toTabs) > 0) {
         foreach ($toTabs as $key) {
             $tabs->addLink(str_replace('_', ' ', $key), "<pre>{$metas[$key]}</pre>");
         }
     }
     if (sizeof($toTabsUnknown) > 0) {
         foreach ($toTabsUnknown as $key) {
             $tabs->addLink(str_replace('_', ' ', $key) . ' ' . Bootstrap::label('?')->color('lightblue'), "<pre>{$unknownMetas[$key]}</pre>");
         }
     }
     if ($tabs->count() > 0) {
         $content[] = Bootstrap::row()->add(12, $tabs);
     }
     if ($crash->stack_trace_id !== null) {
         $content[] = Bootstrap::row()->add(12, Bootstrap::panel('This error count per day', new Chart\StackTracesPerDay($crash->stack_trace_id, 30))->color('red'));
     }
     return View::create('base')->with('title', $title)->with('content', $content);
 }