public static function respondCRUD($payload, $count = 'singular', $verb = '', $additional = array(), $responseID = false) { // auto-generate response ID from noun and verb if (!$responseID) { $responseID = static::getResponseID($count, $verb); } return static::respond($responseID, array_merge(array('success' => true, 'data' => $payload, 'total' => DB::foundRows())), $additional); }
public static function handleBrowseRequest(array $options = [], array $conditions = [], $responseId = null, array $responseData = []) { $className = static::$recordClass; if (!static::checkBrowseAccess(func_get_args())) { return static::throwUnauthorizedError(); } if (static::$browseConditions) { if (!is_array(static::$browseConditions)) { static::$browseConditions = [static::$browseConditions]; } $conditions = array_merge(static::$browseConditions, $conditions); } $limit = isset($_REQUEST['limit']) && ctype_digit($_REQUEST['limit']) ? (int) $_REQUEST['limit'] : static::$browseLimitDefault; $offset = isset($_REQUEST['offset']) && ctype_digit($_REQUEST['offset']) ? (int) $_REQUEST['offset'] : false; if (!empty($_REQUEST['sort'])) { $dir = empty($_REQUEST['dir']) || $_REQUEST['dir'] == 'ASC' ? 'ASC' : 'DESC'; if ($className::sorterExists($_REQUEST['sort'])) { $order = call_user_func($className::getSorter($_REQUEST['sort']), $dir, $_REQUEST['sort']); } elseif ($className::fieldExists($_REQUEST['sort'])) { $order = [$_REQUEST['sort'] => $dir]; } else { return static::throwError('Invalid sort field'); } } else { $order = static::$browseOrder; } $options = array_merge(['limit' => $limit, 'offset' => $offset, 'order' => $order, 'calcFoundRows' => static::$browseCalcFoundRows], $options); // handle query search if (!empty($_REQUEST['q']) && $className::$searchConditions) { return static::handleQueryRequest($_REQUEST['q'], $conditions, ['limit' => $limit, 'offset' => $offset], $responseId, $responseData); } // get results $results = iterator_to_array($className::getAllByWhere($conditions, $options)); $resultsTotal = static::$browseCalcFoundRows ? DB::foundRows() : count($results); // embed tables if (!empty($_GET['relatedTable'])) { $relatedTables = is_array($_GET['relatedTable']) ? $_GET['relatedTable'] : explode(',', $_GET['relatedTable']); $related = []; foreach ($results as $result) { foreach ($relatedTables as $relName) { if (!$result::relationshipExists($relName)) { continue; } $relConfig = $result::getStackedConfig('relationships', $relName); if (!$relConfig || $relConfig['type'] != 'one-one') { continue; } $relatedInstance = $result->{$relName}; if (!$relatedInstance) { continue; } if (empty($related[$relName]) || !in_array($relatedInstance, $related[$relName])) { $related[$relName][] = $relatedInstance; } } } $responseData['related'] = $related; } // generate response return static::respond(isset($responseId) ? $responseId : static::getTemplateName($className::$pluralNoun), array_merge($responseData, ['success' => true, 'data' => $results, 'conditions' => $conditions, 'total' => $resultsTotal, 'limit' => $options['limit'], 'offset' => $options['offset']])); }
if (in_array($validHour, $hours)) { $where[] = "Site{$validHour} = 1"; } } } // add features filter if (!empty($_GET['features'])) { $validFeatures = ['TransportationProvided']; $features = is_array($_GET['features']) ? $_GET['features'] : [$_GET['features']]; foreach ($validFeatures as $validFeature) { if (in_array($validFeature, $features)) { $where[] = "Site{$validFeature} = 1"; } } } // calculate limit params if (!empty($_GET['limit']) && ctype_digit($_GET['limit'])) { $limit = (int) $_GET['limit']; } else { $limit = 25; } if (!empty($_GET['offset']) && ctype_digit($_GET['offset'])) { $offset = (int) $_GET['offset']; } else { $offset = 0; } // execute query and print JSON response #Debug::dumpVar($_GET, false, 'get'); #Debug::dumpVar($where, true, '$where'); JSON::respond(['results' => DB::allRecords('SELECT SQL_CALC_FOUND_ROWS ' . implode(',', $selectFields) . ' FROM Site' . ' WHERE SiteApproved = 1 AND (' . (count($where) ? implode(') AND (', $where) : '1') . ')' . " ORDER BY {$order}" . " LIMIT {$offset}, {$limit}"), 'limit' => $limit, 'offset' => $offset, 'total' => (int) DB::foundRows()]);