/** * Transform a cake response to extjs structure (associated models are not supported!) * otherwise just return the original response. * See also https://github.com/Bancha/Bancha/wiki/Supported-Controller-Method-Results * * @param $modelName The model name of the current request * @param $response The input request from Bancha * @param $controller The used controller * @return extjs formated data array */ public static function transformDataStructureToExt($modelName, $response) { // understand primitive responses if ($response === true || $response === false) { // this was an un-/successfull operation, return that to ext return array('success' => $response); } // expect a successfull operation, but check $success = isset($response['success']) ? !!$response['success'] : true; if (isset($response[$modelName])) { // this is standard cake single element structure $response = array('success' => $success, 'data' => $response[$modelName]); } else { if (isset($response['0'][$modelName])) { // this is standard cake multiple element structure $data = array(); foreach ($response as $record) { array_push($data, $record[$modelName]); } $response = array('success' => $success, 'data' => $data); } else { if (isset($response['records']) && (isset($response['records']['0'][$modelName]) || is_array($response['records']) && isset($response['count']) && $response['count'] == 0)) { // pagination with zero records // this is a paging response // the records have standard cake structure, so get them $data = BanchaResponseTransformer::transformDataStructureToExt($modelName, $response['records']); // create response including the total number of records $response = array('success' => $success, 'data' => isset($data['data']) ? $data['data'] : $data, 'total' => $response['count']); } else { if (is_array($response) && count($response) === 0) { // this is an empty array, so expect that this is just a request without any found records return array('success' => true, 'data' => array()); } } } } // else the structure could not be recognized as any transformable data // so output it as it is return $response; }