|
@@ -295,7 +295,42 @@ class StatTreeController extends Controller
|
|
|
return json_encode($nodes);
|
|
|
}
|
|
|
|
|
|
+ private function cleanupClause($clauseText)
|
|
|
+ {
|
|
|
+ //Dont include empty clauses, i.e ()
|
|
|
+ preg_match('#\((.*?)\)#', $clauseText, $match);
|
|
|
+ $content = @$match[1];
|
|
|
+ if (!$content || empty($content)) return null;
|
|
|
+ return $content;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function applyStatTreeLineQueryClauses(StatTreeLine $statTreeLine)
|
|
|
+ {
|
|
|
+ $model = $statTreeLine->statTree->model;
|
|
|
+ $clauses = [];
|
|
|
+ foreach ($statTreeLine->lineClauses as $lineClause) {
|
|
|
+ $clauseText = $lineClause->clause->clause_text;
|
|
|
+ $isValid = $this->cleanupClause($clauseText);
|
|
|
+ if ($isValid) {
|
|
|
+ array_push($clauses, $clauseText);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $query = 'SELECT COUNT(*) FROM '.$model.' WHERE '. implode(" AND ", $clauses);
|
|
|
+ return DB::select($query);
|
|
|
+ }
|
|
|
+
|
|
|
public function linesJSON(Request $request, StatTree $statTree) {
|
|
|
+
|
|
|
+ // refresh counts
|
|
|
+ $lines = $statTree->lines;
|
|
|
+ foreach ($lines as $line) {
|
|
|
+ $query = $this->applyStatTreeLineQueryClauses($line);
|
|
|
+ if ($query) {
|
|
|
+ $line->last_refresh_count = $query[0]->count;
|
|
|
+ $line->save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
$nodes = [];
|
|
|
foreach ($statTree->rootLines as $rootLine) {
|
|
|
$nodes[] = $this->lineObject($rootLine);
|
|
@@ -502,4 +537,23 @@ class StatTreeController extends Controller
|
|
|
|
|
|
return TRUE;
|
|
|
}
|
|
|
+
|
|
|
+ public function refreshTreeCountQueries(Request $request)
|
|
|
+ {
|
|
|
+ $statTreeID = $request->get('statTreeID');
|
|
|
+ if (!$statTreeID) return $this->fail('No specified stat tree!');
|
|
|
+
|
|
|
+ $statTree = StatTree::where('id', $statTreeID)->first();
|
|
|
+ if (!$statTree) return $this->fail('Invalid stat tree!');
|
|
|
+ $lines = $statTree->lines;
|
|
|
+ foreach ($lines as $line) {
|
|
|
+ $query = $this->applyStatTreeLineQueryClauses($line);
|
|
|
+ if ($query) {
|
|
|
+ $line->last_refresh_count = $query[0]->count;
|
|
|
+ $line->save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->pass();
|
|
|
+ }
|
|
|
}
|