From 0fca349ca45408c2fc06abccb6928a108e8d9f12 Mon Sep 17 00:00:00 2001 From: Joel Pittet Date: Mon, 26 Nov 2012 13:49:16 -0800 Subject: [PATCH 1/5] initial formatting cleanup and notice fixes --- src/shiftplanning.php | 301 ++++++++++++++++++++++-------------------- 1 file changed, 158 insertions(+), 143 deletions(-) diff --git a/src/shiftplanning.php b/src/shiftplanning.php index 9621d2c..9cdab12 100644 --- a/src/shiftplanning.php +++ b/src/shiftplanning.php @@ -9,31 +9,31 @@ /** * Quick Access ShiftPlanning SDK Methods: * doLogin( $array_of_user_data ) - * doLogout( ) - * getMessages( ) + * doLogout() + * getMessages() * getMessageDetails( $message_id ) * createMessage( $array_of_message_data ) * deleteMessage( $message_id ) - * getWallMessages( ) + * getWallMessages() * createWallMessage( $array_of_message_data ) * deleteWallMessage( $message_id, $array_of_other_message_data ) - * getEmployees( ) + * getEmployees() * getEmployeeDetails( $employee_id_number ) * updateEmployee( $employee_id, $array_of_updated_employee_data ) * createEmployee( $array_of_employee_data ) * deleteEmployee( $employee_id ) - * getStaffSkills( ) + * getStaffSkills() * getStaffSkillDetails( $skill_id ) * createStaffSkill( $array_of_skill_data ) * updateStaffSkill( $skill_id, $array_of_skill_data ) * deleteStaffSkill( $skill_id ) * createPing( $array_of_ping_data ) - * getSchedules( ) + * getSchedules() * getScheduleDetails( $schedule_id ) * createSchedule( $array_of_schedule_data ) * updateSchedule( $schedule_id, $array_of_schedule_data ) * deleteSchedule( $schedule_id ) - * getShifts( ) + * getShifts() * getShiftDetails( $shift_id ) * updateShift( $shift_id, $array_of_shift_data ) * createShift( $array_of_shift_data ) @@ -43,20 +43,20 @@ * createVacationSchedule( $array_of_schedule_data ) * updateVacationSchedule( $schedule_id, $array_of_schedule_data ) * deleteVacationSchedule( $schedule_id ) - * getScheduleConflicts( ) - * getAdminSettings( ) + * getScheduleConflicts() + * getAdminSettings() * updateAdminSettings( $array_of_new_settings ) - * getAdminFiles( ) + * getAdminFiles() * getAdminFileDetails( $file_id ) * updateAdminFile( $file_id, $array_of_file_data ) * createAdminFile( $array_of_file_data ) * deleteAdminFile( $file_id ) - * getAdminBackups( ) + * getAdminBackups() * getAdminBackupDetails( $backup_id ) * createAdminBackup( $array_of_backup_data ) * deleteAdminBackup( $backup_id ) - * getAPIConfig( ) - * getAPIMethods( ) + * getAPIConfig() + * getAPIMethods() */ /** @@ -68,7 +68,7 @@ * ) * ) * - * For methods that return multiple objects (as in the case for the getMessages( ) method + * For methods that return multiple objects (as in the case for the getMessages() method * responses will look like this, where the indexes [0], [1] would be replaced with the * message you're looking to display * @@ -88,15 +88,15 @@ */ class shiftplanning -{// +{ private $_key; private $_callback; private $_init; private $_debug; - private $request = array( ); - private $requests = array( ); - private $response = array( ); - private $raw_response = array( ); + private $request = array(); + private $requests = array(); + private $response = array(); + private $raw_response = array(); // constants const session_identifier = 'SP'; @@ -106,55 +106,55 @@ class shiftplanning public function __construct( $config = array() ) {// construct the SDK try - {// + { $this->_debug = false; - $this->startSession( ); + $this->startSession(); // set the developer key $this->setAppKey( $config['key'] ); - if( !function_exists( 'curl_init' ) ) + if ( !function_exists( 'curl_init' ) ) {// curl is not available throw new Exception( $this->internal_errors( 6 ) ); } - if( !function_exists( 'json_decode' ) ) + if ( !function_exists( 'json_decode' ) ) {// json_decode is not available throw new Exception( $this->internal_errors( 7 ) ); } } catch( Exception $e ) - {// - echo $e->getMessage( ); exit; + { + echo $e->getMessage(); exit; } } public function setDebug( $switch = false ) {// turn debug on - if( file_exists('log.txt') ) + if ( file_exists('log.txt') ) {// delete previous log file unlink( 'log.txt' ); } $this->_debug = true; } - protected function startSession( ) + protected function startSession() {// start the session session_name( self::session_identifier ); - session_start( ); + session_start(); } - private function setSession( ) + private function setSession() {// store the user data to this session $_SESSION['token'] = $this->response['token'][0]; $_SESSION['data'] = $this->response['data'][0]; } - private function destroySession( ) + private function destroySession() {// destroy the currently active session $logout = $this->setRequest( array ( 'module' => 'staff.logout', 'method' => 'GET' ) ); - if( $logout['status']['code'] == 1 ) + if ( $logout['status']['code'] == 1 ) {// logout successful, remove local session data unset( $_SESSION['token'] ); unset( $_SESSION['data'] ); @@ -165,9 +165,9 @@ private function destroySession( ) return $logout; } - public function getSession( ) + public function getSession() {// check whether a valid session has been established - if( isset( $_SESSION['token'] ) ) + if ( isset( $_SESSION['token'] ) ) {// user is already authenticated return $_SESSION['data']; } @@ -183,12 +183,12 @@ private function setCallback( $callback ) return $this->_callback; } - public function getRawResponse( ) + public function getRawResponse() {// return the raw response data return $this->raw_response; } - public function getAppKey( ) + public function getAppKey() {// return the developer key return $this->_key; } @@ -199,12 +199,17 @@ public function setAppKey( $key ) return $this->_key; } - public function getAppToken( ) - {// return the token that's currently being used + /** + * Get the token that's currently being used + * @return [type] [description] + */ + public function getAppToken() + { try - {// - if( $this->getSession( ) ) - {// user authenticated, return the token + { + // user authenticated, return the token + if ( $this->getSession() ) + { return $_SESSION['token']; } else @@ -213,12 +218,12 @@ public function getAppToken( ) } } catch( Exception $e ) - {// + { echo $e->getMessage(); } } - public function setRequest( $requests = array( ) ) + public function setRequest( $requests = array() ) {// set the request parameters // clear out previous request data unset( $this->requests ); @@ -230,13 +235,13 @@ public function setRequest( $requests = array( ) ) foreach( $requests as $r => $v ) {// loop through each request array - if( is_array( $v ) ) - {// + if ( is_array( $v ) ) + { $this->requests[] = $v; } else - {// - if( $requests['module'] == 'staff.login' ) + { + if ( $requests['module'] == 'staff.login' ) {// automatically initialize session after this API call $this->_init = 1; } @@ -244,10 +249,10 @@ public function setRequest( $requests = array( ) ) } } - return $this->api( ); + return $this->api(); } - public function getRequest( ) + public function getRequest() {// return the request parameters return array_merge( $this->request, array( 'request' => $this->requests ) ); } @@ -257,40 +262,40 @@ private function setResponse( $response ) // remove previous response data unset( $this->response ); // set new response data - if( !is_array( $response[0] ) ) - {// + if ( ! isset($response[0]) || !is_array( $response[0] ) ) + { $this->response['response'][0] = array( 'code' => $response['status'], - 'text' => $this->getResponseText( &$response['status'] ), - 'error' => $response['error'] + 'text' => $this->getResponseText( $response['status'] ), + 'error' => (isset($response['error']) ? $response['error'] : '') ); $this->response['data'][0] = $response['data']; $this->response['token'][0] = $response['token']; } else - {// + { foreach( $response as $num => $data ) {// loop through each response $this->response['response'][$num] = array( 'code' => $data['status'], - 'text' => $this->getResponseText( &$data['status'] ), - 'error' => $data['error'] + 'text' => $this->getResponseText( $data['status'] ), + 'error' => (isset($data['error']) ? $data['error'] : '') ); - $tmp = array( ); + $tmp = array(); $id = 0; - if( is_array( $data['data'] ) ) + if ( is_array( $data['data'] ) ) {// is there an array returned foreach( $data['data'] as $n => $v ) - {// - if( is_array( $v ) ) - {// + { + if ( is_array( $v ) ) + { foreach( $v as $key => $val ) - {// + { $tmp[$n][$key] = $val; } } else - {// + { $tmp[$n] = $v; } } @@ -305,19 +310,30 @@ private function setResponse( $response ) } } + /** + * Get the API response data to the calling method + * @param integer $call_num Calling method number + * @return array API Response Data + */ public function getResponse( $call_num = 0 ) - {// return the API response data to the calling method + { return array( 'status' => $this->response['response'][$call_num], 'data' => $this->response['data'][$call_num], - 'error' => $this->response['error'][$call_num] + 'error' => (isset($this->response['error'])) ? $this->response['error'][$call_num]: NULL, ); } + /** + * Get a reason text for a response code + * @param int $code [description] + * @return string Response + */ private function getResponseText( &$code ) - {// return a reason text for a response code + { + // select a response code to display switch( $code ) - {// select a response code to display + { case '-3' : $reason = 'Flagged API Key - Pemanently Banned'; break; case '-2' : $reason = 'Flagged API Key - Too Many invalid access attempts - contact us'; break; case '-1' : $reason = 'Flagged API Key - Temporarily Disabled - contact us'; break; @@ -383,13 +399,13 @@ private function internal_errors( $errno ) return $message; exit; } - private function api( ) + private function api() {// create the api call - if( $this->_callback == null ) + if ( $this->_callback == null ) {// method to call after successful api request $this->setCallback( 'getResponse' ); } - if( $this->getSession( ) ) + if ( $this->getSession() ) {// session already established, use token // remove the developer key from the request, since it's not necessary unset( $this->request['key'] ); @@ -399,8 +415,8 @@ private function api( ) else {// session has not been established, use developer key to access API try - {// - if( isset( $this->_key ) ) + { + if ( isset( $this->_key ) ) {// developer key is set $this->request['key'] = $this->_key; } @@ -410,16 +426,16 @@ private function api( ) } } catch( Exception $e ) - {// - echo $e->getMessage( ); + { + echo $e->getMessage(); } } // make the api request - return $this->perform_request( ); + return $this->perform_request(); } private function getFileMimeType( $extension ) - {// + { $mimes = array( "ez" => "application/andrew-inset", "hqx" => "application/mac-binhex40", @@ -558,8 +574,8 @@ private function getFileMimeType( $extension ) "ice" => "x-conference-xcooltalk" ); try - {// - if( $mimes[ $extension ] ) + { + if ( $mimes[ $extension ] ) {// mime found return $mimes[ $extension ]; } @@ -569,25 +585,25 @@ private function getFileMimeType( $extension ) } } catch( Exception $e ) - {// - echo $e->getMessage( ); + { + echo $e->getMessage(); } } private function getFileData( $file ) {// get file details, (data, length, mimetype) try - {// - if( file_exists( $file ) ) + { + if ( file_exists( $file ) ) {// file $file_data['filedata'] = file_get_contents( $file ); $file_data['filelength'] = strlen( $file_data['filedata'] ); - if( function_exists( 'mime_content_type' ) ) + if ( function_exists( 'mime_content_type' ) ) {// mime_content_type function is available $file_data['mimetype'] = mime_content_type( $file ); } else - {// + { $parts = explode( '.', $file ); $extension = strtolower( $parts[ sizeOf( $parts ) - 1 ] ); $file_data['mimetype'] = $this->getFileMimeType( $extension ); @@ -600,37 +616,37 @@ private function getFileData( $file ) ); } else - {// + { throw new Exception( $this->internal_errors( 8 ) ); } } catch( Exception $e ) - {// - echo $e->getMessage( ); exit; + { + echo $e->getMessage(); exit; } } - private function perform_request( ) + private function perform_request() {// perform the api request try - {// + { $ch = curl_init( self::api_endpoint ); $filedata = ''; - if( is_array( $this->requests ) ) - {// + if ( is_array( $this->requests ) ) + { foreach( $this->requests as $key => $request ) - {// - if( $request['filedata'] ) - {// + { + if ( isset($request['filedata']) ) + { $filedata = $request['filedata']; unset( $this->requests[$key]['filedata'] ); } } } - $post = $filedata ? array( 'data'=> json_encode( $this->getRequest( ) ), - 'filedata' => $filedata ) : array( 'data' => json_encode( $this->getRequest( ) ) ); + $post = $filedata ? array( 'data'=> json_encode( $this->getRequest() ), + 'filedata' => $filedata ) : array( 'data' => json_encode( $this->getRequest() ) ); curl_setopt( $ch, CURLOPT_URL, self::api_endpoint ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 ); @@ -643,36 +659,36 @@ private function perform_request( ) $http_response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); curl_close( $ch ); - if( $http_response_code == 200 ) + if ( $http_response_code == 200 ) {// response from API was a successful $temp = json_decode( $response, true ); // decode the response and store each call response in its own array $this->setResponse( $temp ); - if( $this->_init == 1 ) + if ( $this->_init == 1 ) {// initialize a session - $this->setSession( ); + $this->setSession(); } // raw response call $this->raw_response = $temp; - if( $this->_debug == true ) + if ( $this->_debug == true ) {// debug mode is on - $request = json_encode( $this->getRequest( ) ); + $request = json_encode( $this->getRequest() ); $db = fopen('log.txt', 'a'); - $tmp_vals = array( ); - if( is_array( $this->response['data'][0] ) ) - {// + $tmp_vals = array(); + if ( is_array( $this->response['data'][0] ) ) + { foreach( $this->response['data'] as $n => $v ) - {// + { foreach( $v as $key => $val ) - {// + { $tmp_vals[$n][$key] = $val; } } } else - {// + { foreach( $this->response['data'] as $n => $v ) - {// + { $tmp_vals[$n] = $v; } } @@ -683,7 +699,7 @@ private function perform_request( ) fclose( $db ); } // perform the callback method - return $this->{ $this->_callback }( ); + return $this->{ $this->_callback }(); } else {// response from API was unsuccessful @@ -691,7 +707,7 @@ private function perform_request( ) } } catch( Exception $e ) - {// + { echo $e->getMessage(); } } @@ -700,7 +716,7 @@ private function perform_request( ) * User Authentication Methods * */ - public function doLogin( $user = array( ) ) + public function doLogin( $user = array() ) {// perform a login api call return $this->setRequest( array( @@ -712,16 +728,16 @@ public function doLogin( $user = array( ) ) ); } - public function doLogout( ) + public function doLogout() {// erase token and user data from current session - $this->destroySession( ); + $this->destroySession(); } /* * Message Methods * */ - public function getMessages( ) + public function getMessages() {// get messages for the currently logged in user return $this->setRequest( array( @@ -742,7 +758,7 @@ public function getMessageDetails( $id ) ); } - public function createMessage( $message = array( ) ) + public function createMessage( $message = array() ) {// create a new message return $this->setRequest( array_merge( @@ -766,7 +782,7 @@ public function deleteMessage( $id ) ); } - public function getWallMessages( ) + public function getWallMessages() {// get message wall return $this->setRequest( array( @@ -776,7 +792,7 @@ public function getWallMessages( ) ); } - public function createWallMessage( $message = array( ) ) + public function createWallMessage( $message = array() ) {// create a wall message return $this->setRequest( array_merge( @@ -789,7 +805,7 @@ public function createWallMessage( $message = array( ) ) ); } - public function deleteWallMessage( $id, $details = array( ) ) + public function deleteWallMessage( $id, $details = array() ) {// delete a wall message return $this->setRequest( array_merge( @@ -807,7 +823,7 @@ public function deleteWallMessage( $id, $details = array( ) ) * Staff Methods * */ - public function getEmployees( ) + public function getEmployees() {// get a list of employees return $this->setRequest( array( @@ -828,7 +844,7 @@ public function getEmployeeDetails( $id ) ); } - public function updateEmployee( $id, $new_data = array( ) ) + public function updateEmployee( $id, $new_data = array() ) {// update an employee record return $this->setRequest( array_merge( @@ -866,7 +882,7 @@ public function deleteEmployee( $id ) ); } - public function getStaffSkills( ) + public function getStaffSkills() {// get staff skills return $this->setRequest( array( @@ -887,7 +903,7 @@ public function getStaffSkillDetails( $id ) ); } - public function createStaffSkill( $skill_details = array( ) ) + public function createStaffSkill( $skill_details = array() ) {// create staff skill return $this->setRequest( array_merge( @@ -900,7 +916,7 @@ public function createStaffSkill( $skill_details = array( ) ) ); } - public function updateStaffSkill( $id, $skill_details = array( ) ) + public function updateStaffSkill( $id, $skill_details = array() ) {// update staff skill return $this->setRequest( array_merge( @@ -925,7 +941,7 @@ public function deleteStaffSkill( $id ) ); } - public function createPing( $ping_data = array( ) ) + public function createPing( $ping_data = array() ) {// create a ping return $this->setRequest( array_merge( @@ -942,7 +958,7 @@ public function createPing( $ping_data = array( ) ) * Schedule Methods * */ - public function getSchedules( ) + public function getSchedules() {// get schedules return $this->setRequest( array( @@ -963,7 +979,7 @@ public function getScheduleDetails( $id ) ); } - public function createSchedule( $schedule_details = array( ) ) + public function createSchedule( $schedule_details = array() ) {// create a new schedule return $this->setRequest( array_merge( @@ -976,7 +992,7 @@ public function createSchedule( $schedule_details = array( ) ) ); } - public function updateSchedule( $id, $schedule_details = array( ) ) + public function updateSchedule( $id, $schedule_details = array() ) {// update an existing schedule return $this->setRequest( array_merge( @@ -1001,7 +1017,7 @@ public function deleteSchedule( $id ) ); } - public function getShifts( ) + public function getShifts() {// get shifts return $this->setRequest( array( @@ -1022,7 +1038,7 @@ public function getShiftDetails( $id ) ); } - public function updateShift( $id, $shift_details = array( ) ) + public function updateShift( $id, $shift_details = array() ) {// update shift details return $this->setRequest( array_merge( @@ -1036,7 +1052,7 @@ public function updateShift( $id, $shift_details = array( ) ) ); } - public function createShift( $shift_details = array( ) ) + public function createShift( $shift_details = array() ) {// create a new shift return $this->setRequest( array_merge( @@ -1060,7 +1076,7 @@ public function deleteShift( $id ) ); } - public function getVacationSchedules( $time_period = array( ) ) + public function getVacationSchedules( $time_period = array() ) {// get schedule vacations, pass start and end params to get vacations within a certian time-period return $this->setRequest( array_merge( @@ -1084,7 +1100,7 @@ public function getVacationScheduleDetails( $id ) ); } - public function createVacationSchedule( $vacation_details = array( ) ) + public function createVacationSchedule( $vacation_details = array() ) {// create a vacation schedule return $this->setRequest( array_merge( @@ -1097,7 +1113,7 @@ public function createVacationSchedule( $vacation_details = array( ) ) ); } - public function updateVacationSchedule( $id, $vacation_details = array( ) ) + public function updateVacationSchedule( $id, $vacation_details = array() ) {// update a vacation schedule return $this->setRequest( array_merge( @@ -1122,7 +1138,7 @@ public function deleteVacationSchedule( $id ) ); } - public function getScheduleConflicts( $time_period = array( ) ) + public function getScheduleConflicts( $time_period = array() ) {// get schedule conflicts return $this->setRequest( array_merge( @@ -1139,7 +1155,7 @@ public function getScheduleConflicts( $time_period = array( ) ) * Administration Methods * */ - public function getAdminSettings( ) + public function getAdminSettings() {// get admin settings return $this->setRequest( array( @@ -1149,7 +1165,7 @@ public function getAdminSettings( ) ); } - public function updateAdminSettings( $settings = array( ) ) + public function updateAdminSettings( $settings = array() ) {// update admin settings return $this->setRequest( array_merge( @@ -1162,7 +1178,7 @@ public function updateAdminSettings( $settings = array( ) ) ); } - public function getAdminFiles( ) + public function getAdminFiles() {// get administrator file listing return $this->setRequest( array( @@ -1183,7 +1199,7 @@ public function getAdminFileDetails( $id ) ); } - public function updateAdminFile( $id, $details = array( ) ) + public function updateAdminFile( $id, $details = array() ) {// update admin file details return $this->setRequest( array_merge( @@ -1197,7 +1213,7 @@ public function updateAdminFile( $id, $details = array( ) ) ); } - public function createAdminFile( $file_details = array( ) ) + public function createAdminFile( $file_details = array() ) {// create new admin file $file_details = array_merge( $file_details, $this->getFileData( $file_details['filename'] ) ); return $this->setRequest( @@ -1222,7 +1238,7 @@ public function deleteAdminFile( $id ) ); } - public function getAdminBackups( ) + public function getAdminBackups() {// get admin backups return $this->setRequest( array( @@ -1243,7 +1259,7 @@ public function getAdminBackupDetails( $id ) ); } - public function createAdminBackup( $backup_details = array( ) ) + public function createAdminBackup( $backup_details = array() ) {// create an admin backup $backup_details = array_merge( $backup_details, $this->getFileData( $backup_details['filename'] ) ); return $this->setRequest( @@ -1272,7 +1288,7 @@ public function deleteAdminBackup( $id ) * API Methods * */ - public function getAPIConfig( ) + public function getAPIConfig() {// get api config return $this->setRequest( array( @@ -1282,7 +1298,7 @@ public function getAPIConfig( ) ); } - public function getAPIMethods( ) + public function getAPIMethods() {// get all available api methods return $this->setRequest( array( @@ -1292,4 +1308,3 @@ public function getAPIMethods( ) ); } } -?> \ No newline at end of file From e6047943742da46ea1e28712a5344c9f31eeb5d8 Mon Sep 17 00:00:00 2001 From: Joel Pittet Date: Mon, 26 Nov 2012 15:17:10 -0800 Subject: [PATCH 2/5] more cleanup including moving message and config strings into array php files and adding PHPdoc comments to every function --- src/config/mimes.php | 141 ++++ src/messages/internal_error.php | 18 + src/messages/response.php | 28 + src/shiftplanning.php | 1090 ++++++++++++++++++------------- 4 files changed, 817 insertions(+), 460 deletions(-) create mode 100644 src/config/mimes.php create mode 100644 src/messages/internal_error.php create mode 100644 src/messages/response.php diff --git a/src/config/mimes.php b/src/config/mimes.php new file mode 100644 index 0000000..fda283d --- /dev/null +++ b/src/config/mimes.php @@ -0,0 +1,141 @@ + "application/andrew-inset", + "hqx" => "application/mac-binhex40", + "cpt" => "application/mac-compactpro", + "doc" => "application/msword", + "bin" => "application/octet-stream", + "dms" => "application/octet-stream", + "lha" => "application/octet-stream", + "lzh" => "application/octet-stream", + "exe" => "application/octet-stream", + "class" => "application/octet-stream", + "so" => "application/octet-stream", + "dll" => "application/octet-stream", + "oda" => "application/oda", + "pdf" => "application/pdf", + "ai" => "application/postscript", + "eps" => "application/postscript", + "ps" => "application/postscript", + "smi" => "application/smil", + "smil" => "application/smil", + "wbxml" => "application/vnd.wap.wbxml", + "wmlc" => "application/vnd.wap.wmlc", + "wmlsc" => "application/vnd.wap.wmlscriptc", + "bcpio" => "application/x-bcpio", + "vcd" => "application/x-cdlink", + "pgn" => "application/x-chess-pgn", + "cpio" => "application/x-cpio", + "csh" => "application/x-csh", + "dcr" => "application/x-director", + "dir" => "application/x-director", + "dxr" => "application/x-director", + "dvi" => "application/x-dvi", + "spl" => "application/x-futuresplash", + "gtar" => "application/x-gtar", + "hdf" => "application/x-hdf", + "js" => "application/x-javascript", + "skp" => "application/x-koan", + "skd" => "application/x-koan", + "skt" => "application/x-koan", + "skm" => "application/x-koan", + "latex" => "application/x-latex", + "nc" => "application/x-netcdf", + "cdf" => "application/x-netcdf", + "sh" => "application/x-sh", + "shar" => "application/x-shar", + "swf" => "application/x-shockwave-flash", + "sit" => "application/x-stuffit", + "sv4cpio" => "application/x-sv4cpio", + "sv4crc" => "application/x-sv4crc", + "tar" => "application/x-tar", + "tcl" => "application/x-tcl", + "tex" => "application/x-tex", + "texinfo" => "application/x-texinfo", + "texi" => "application/x-texinfo", + "t" => "application/x-troff", + "tr" => "application/x-troff", + "roff" => "application/x-troff", + "man" => "application/x-troff-man", + "me" => "application/x-troff-me", + "ms" => "application/x-troff-ms", + "ustar" => "application/x-ustar", + "src" => "application/x-wais-source", + "xhtml" => "application/xhtml+xml", + "xht" => "application/xhtml+xml", + "zip" => "application/zip", + "au" => "audio/basic", + "snd" => "audio/basic", + "mid" => "audio/midi", + "midi" => "audio/midi", + "kar" => "audio/midi", + "mpga" => "audio/mpeg", + "mp2" => "audio/mpeg", + "mp3" => "audio/mpeg", + "aif" => "audio/x-aiff", + "aiff" => "audio/x-aiff", + "aifc" => "audio/x-aiff", + "m3u" => "audio/x-mpegurl", + "ram" => "audio/x-pn-realaudio", + "rm" => "audio/x-pn-realaudio", + "rpm" => "audio/x-pn-realaudio-plugin", + "ra" => "audio/x-realaudio", + "wav" => "audio/x-wav", + "pdb" => "chemical/x-pdb", + "xyz" => "chemical/x-xyz", + "bmp" => "image/bmp", + "gif" => "image/gif", + "ief" => "image/ief", + "jpeg" => "image/jpeg", + "jpg" => "image/jpeg", + "jpe" => "image/jpeg", + "png" => "image/png", + "tiff" => "image/tiff", + "tif" => "image/tif", + "djvu" => "image/vnd.djvu", + "djv" => "image/vnd.djvu", + "wbmp" => "image/vnd.wap.wbmp", + "ras" => "image/x-cmu-raster", + "pnm" => "image/x-portable-anymap", + "pbm" => "image/x-portable-bitmap", + "pgm" => "image/x-portable-graymap", + "ppm" => "image/x-portable-pixmap", + "rgb" => "image/x-rgb", + "xbm" => "image/x-xbitmap", + "xpm" => "image/x-xpixmap", + "xwd" => "image/x-windowdump", + "igs" => "model/iges", + "iges" => "model/iges", + "msh" => "model/mesh", + "mesh" => "model/mesh", + "silo" => "model/mesh", + "wrl" => "model/vrml", + "vrml" => "model/vrml", + "css" => "text/css", + "html" => "text/html", + "htm" => "text/html", + "asc" => "text/plain", + "txt" => "text/plain", + "rtx" => "text/richtext", + "rtf" => "text/rtf", + "sgml" => "text/sgml", + "sgm" => "text/sgml", + "tsv" => "text/tab-seperated-values", + "wml" => "text/vnd.wap.wml", + "wmls" => "text/vnd.wap.wmlscript", + "etx" => "text/x-setext", + "xml" => "text/xml", + "xsl" => "text/xml", + "mpeg" => "video/mpeg", + "mpg" => "video/mpeg", + "mpe" => "video/mpeg", + "qt" => "video/quicktime", + "mov" => "video/quicktime", + "mxu" => "video/vnd.mpegurl", + "avi" => "video/x-msvideo", + "movie" => "video/x-sgi-movie", + "ice" => "x-conference-xcooltalk", +); diff --git a/src/messages/internal_error.php b/src/messages/internal_error.php new file mode 100644 index 0000000..ca10307 --- /dev/null +++ b/src/messages/internal_error.php @@ -0,0 +1,18 @@ + 'Could not find the requested error message.', + 1 => 'The requested API method was not found in this SDK.', + 2 => 'The ShiftPlanning API is not responding.', + 3 => 'You must use the login method before accessing other modules of this API.', + 4 => 'A session has not yet been established.', + 5 => 'You must specify your Developer Key when using this SDK.', + 6 => 'The ShiftPlanning SDK needs the CURL PHP extension.', + 7 => 'The ShiftPlanning SDK needs the JSON PHP extension.', + 8 => "File doesn't exist.", + 9 => 'Could not find the correct mime for the file supplied.', +); + + \ No newline at end of file diff --git a/src/messages/response.php b/src/messages/response.php new file mode 100644 index 0000000..19b965d --- /dev/null +++ b/src/messages/response.php @@ -0,0 +1,28 @@ + 'Error code not found', + '-3' => 'Flagged API Key - Pemanently Banned', + '-2' => 'Flagged API Key - Too Many invalid access attempts - contact us', + '-1' => 'Flagged API Key - Temporarily Disabled - contact us', + '1' => 'Success -', + '2' => 'Invalid API key - App must be granted a valid key by ShiftPlanning', + '3' => 'Invalid token key - Please re-authenticate', + '4' => 'Invalid Method - No Method with that name exists in our API', + '5' => 'Invalid Module - No Module with that name exists in our API', + '6' => 'Invalid Action - No Action with that name exists in our API', + '7' => 'Authentication Failed - You do not have permissions to access the service', + '8' => 'Missing parameters - Your request is missing a required parameter', + '9' => 'Invalid parameters - Your request has an invalid parameter type', + '10' => 'Extra parameters - Your request has an extra/unallowed parameter type', + '12' => 'Create Failed - Your CREATE request failed', + '13' => 'Update Failed - Your UPDATE request failed', + '14' => 'Delete Failed - Your DELETE request failed', + '20' => 'Incorrect Permissions - You don\'t have the proper permissions to access this', + '90' => 'Suspended API key - Access for your account has been suspended, please contact ShiftPlanning', + '91' => 'Throttle exceeded - You have exceeded the max allowed requests. Try again later.', + '98' => 'Bad API Paramaters - Invalid POST request. See Manual.', + '99' => 'Service Offline - This service is temporarily offline. Try again later.', +); \ No newline at end of file diff --git a/src/shiftplanning.php b/src/shiftplanning.php index 9cdab12..126610e 100644 --- a/src/shiftplanning.php +++ b/src/shiftplanning.php @@ -8,53 +8,53 @@ /** * Quick Access ShiftPlanning SDK Methods: - * doLogin( $array_of_user_data ) + * doLogin($array_of_user_data ) * doLogout() * getMessages() - * getMessageDetails( $message_id ) - * createMessage( $array_of_message_data ) - * deleteMessage( $message_id ) + * getMessageDetails($message_id ) + * createMessage($array_of_message_data ) + * deleteMessage($message_id ) * getWallMessages() - * createWallMessage( $array_of_message_data ) - * deleteWallMessage( $message_id, $array_of_other_message_data ) + * createWallMessage($array_of_message_data ) + * deleteWallMessage($message_id, $array_of_other_message_data ) * getEmployees() - * getEmployeeDetails( $employee_id_number ) - * updateEmployee( $employee_id, $array_of_updated_employee_data ) - * createEmployee( $array_of_employee_data ) - * deleteEmployee( $employee_id ) + * getEmployeeDetails($employee_id_number ) + * updateEmployee($employee_id, $array_of_updated_employee_data ) + * createEmployee($array_of_employee_data ) + * deleteEmployee($employee_id ) * getStaffSkills() - * getStaffSkillDetails( $skill_id ) - * createStaffSkill( $array_of_skill_data ) - * updateStaffSkill( $skill_id, $array_of_skill_data ) - * deleteStaffSkill( $skill_id ) - * createPing( $array_of_ping_data ) + * getStaffSkillDetails($skill_id ) + * createStaffSkill($array_of_skill_data ) + * updateStaffSkill($skill_id, $array_of_skill_data ) + * deleteStaffSkill($skill_id ) + * createPing($array_of_ping_data ) * getSchedules() - * getScheduleDetails( $schedule_id ) - * createSchedule( $array_of_schedule_data ) - * updateSchedule( $schedule_id, $array_of_schedule_data ) - * deleteSchedule( $schedule_id ) + * getScheduleDetails($schedule_id ) + * createSchedule($array_of_schedule_data ) + * updateSchedule($schedule_id, $array_of_schedule_data ) + * deleteSchedule($schedule_id ) * getShifts() - * getShiftDetails( $shift_id ) - * updateShift( $shift_id, $array_of_shift_data ) - * createShift( $array_of_shift_data ) - * deleteShift( $shift_id ) - * getVacationSchedules( $time_period_array ) // e.g. getVacationSchedules( array( 'start' => '', 'end' => '' ) ); - * getVacationScheduleDetails( $schedule_id ) - * createVacationSchedule( $array_of_schedule_data ) - * updateVacationSchedule( $schedule_id, $array_of_schedule_data ) - * deleteVacationSchedule( $schedule_id ) + * getShiftDetails($shift_id ) + * updateShift($shift_id, $array_of_shift_data ) + * createShift($array_of_shift_data ) + * deleteShift($shift_id ) + * getVacationSchedules($time_period_array ) // e.g. getVacationSchedules( array( 'start' => '', 'end' => '' )); + * getVacationScheduleDetails($schedule_id ) + * createVacationSchedule($array_of_schedule_data ) + * updateVacationSchedule($schedule_id, $array_of_schedule_data ) + * deleteVacationSchedule($schedule_id ) * getScheduleConflicts() * getAdminSettings() - * updateAdminSettings( $array_of_new_settings ) + * updateAdminSettings($array_of_new_settings ) * getAdminFiles() - * getAdminFileDetails( $file_id ) - * updateAdminFile( $file_id, $array_of_file_data ) - * createAdminFile( $array_of_file_data ) - * deleteAdminFile( $file_id ) + * getAdminFileDetails($file_id ) + * updateAdminFile($file_id, $array_of_file_data ) + * createAdminFile($array_of_file_data ) + * deleteAdminFile($file_id ) * getAdminBackups() - * getAdminBackupDetails( $backup_id ) - * createAdminBackup( $array_of_backup_data ) - * deleteAdminBackup( $backup_id ) + * getAdminBackupDetails($backup_id ) + * createAdminBackup($array_of_backup_data ) + * deleteAdminBackup($backup_id ) * getAPIConfig() * getAPIMethods() */ @@ -87,7 +87,7 @@ * ) */ -class shiftplanning +class Shiftplanning { private $_key; private $_callback; @@ -99,26 +99,34 @@ class shiftplanning private $raw_response = array(); // constants - const session_identifier = 'SP'; - const api_endpoint = 'http://www.shiftplanning.com/api/'; - const output_type = 'json'; + const SESSION_IDENTIFIER = 'SP'; + const API_ENDPOINT = 'http://www.shiftplanning.com/api/'; + const OUTPUT_TYPE = 'json'; - public function __construct( $config = array() ) - {// construct the SDK + + /** + * Construct the SDK + * @param array $config [description] + */ + public function __construct($config = array()) + { try { - $this->_debug = false; + $this->_debug = FALSE; $this->startSession(); // set the developer key - $this->setAppKey( $config['key'] ); + $this->setAppKey($config['key'] ); - if ( !function_exists( 'curl_init' ) ) - {// curl is not available - throw new Exception( $this->internal_errors( 6 ) ); + if (!function_exists( 'curl_init' )) + { + // curl is not available + throw new Exception($this->internal_errors( 6 )); } - if ( !function_exists( 'json_decode' ) ) - {// json_decode is not available - throw new Exception( $this->internal_errors( 7 ) ); + + if (!function_exists( 'json_decode' )) + { + // json_decode is not available + throw new Exception($this->internal_errors( 7 )); } } catch( Exception $e ) @@ -127,37 +135,47 @@ public function __construct( $config = array() ) } } - public function setDebug( $switch = false ) - {// turn debug on - if ( file_exists('log.txt') ) - {// delete previous log file + /** + * turn debug on + * @param boolean $switch [description] + */ + public function setDebug($switch = FALSE ) + { + // delete previous log file + if (file_exists('log.txt')) + { unlink( 'log.txt' ); } - $this->_debug = true; + $this->_debug = TRUE; } + // start the session protected function startSession() - {// start the session - session_name( self::session_identifier ); + { + session_name( self::SESSION_IDENTIFIER ); session_start(); } + // store the user data to this session private function setSession() - {// store the user data to this session + { $_SESSION['token'] = $this->response['token'][0]; $_SESSION['data'] = $this->response['data'][0]; } + // destroy the currently active session private function destroySession() - {// destroy the currently active session + { $logout = $this->setRequest( array ( 'module' => 'staff.logout', 'method' => 'GET' - ) ); - if ( $logout['status']['code'] == 1 ) - {// logout successful, remove local session data - unset( $_SESSION['token'] ); - unset( $_SESSION['data'] ); + )); + + // logout successful, remove local session data + if ($logout['status']['code'] == 1 ) + { + unset($_SESSION['token'] ); + unset($_SESSION['data'] ); # REMOVE COOKIE setcookie("mobile_token", $this->response['token'][0], time()-(30*86400), "/", ".shiftplanning.com"); @@ -165,36 +183,58 @@ private function destroySession() return $logout; } + /** + * check whether a valid session has been established + * @return [type] [description] + */ public function getSession() - {// check whether a valid session has been established - if ( isset( $_SESSION['token'] ) ) - {// user is already authenticated + { + // user is already authenticated + if (isset($_SESSION['token'] )) + { return $_SESSION['data']; } else - {// user has not authenticated - return false; + { + // user has not authenticated + return FALSE; } } - private function setCallback( $callback ) - {// set the method to call after successful api call + /** + * set the method to call after successful api call + * @param [type] $callback [description] + */ + private function setCallback($callback ) + { $this->_callback = $callback; return $this->_callback; } + /** + * return the raw response data + * @return [type] [description] + */ public function getRawResponse() - {// return the raw response data + { return $this->raw_response; } + /** + * return the developer key + * @return [type] [description] + */ public function getAppKey() - {// return the developer key + { return $this->_key; } - public function setAppKey( $key ) - {// set the developer key to use + /** + * Set the developer key to use + * @param [type] $key [description] + */ + public function setAppKey($key ) + { $this->_key = $key; return $this->_key; } @@ -208,13 +248,14 @@ public function getAppToken() try { // user authenticated, return the token - if ( $this->getSession() ) + if ($this->getSession()) { return $_SESSION['token']; } else - {// user not authenticated, return an error - throw new Exception( $this->internal_errors( 4 ) ); + { + // user not authenticated, return an error + throw new Exception($this->internal_errors( 4 )); } } catch( Exception $e ) @@ -223,26 +264,32 @@ public function getAppToken() } } - public function setRequest( $requests = array() ) - {// set the request parameters + /** + * set the request parameters + * @param array $requests [description] + */ + public function setRequest($requests = array()) + { // clear out previous request data - unset( $this->requests ); + unset($this->requests ); // set the default response type of JSON - $this->request['output'] = self::output_type; + $this->request['output'] = self::OUTPUT_TYPE; $this->_init = 0; - foreach( $requests as $r => $v ) - {// loop through each request array - if ( is_array( $v ) ) + foreach($requests as $r => $v ) + { + // loop through each request array + if (is_array($v )) { $this->requests[] = $v; } else { - if ( $requests['module'] == 'staff.login' ) - {// automatically initialize session after this API call + // automatically initialize session after this API call + if ($requests['module'] == 'staff.login' ) + { $this->_init = 1; } $this->requests[] = $requests; break; @@ -252,21 +299,29 @@ public function setRequest( $requests = array() ) return $this->api(); } + /** + * return the request parameters + * @return [type] [description] + */ public function getRequest() - {// return the request parameters - return array_merge( $this->request, array( 'request' => $this->requests ) ); + { + return array_merge($this->request, array( 'request' => $this->requests )); } - private function setResponse( $response ) - {// set the response data + /** + * set the response data + * @param [type] $response [description] + */ + private function setResponse($response ) + { // remove previous response data - unset( $this->response ); + unset($this->response ); // set new response data - if ( ! isset($response[0]) || !is_array( $response[0] ) ) + if (! isset($response[0]) || !is_array($response[0] )) { $this->response['response'][0] = array( 'code' => $response['status'], - 'text' => $this->getResponseText( $response['status'] ), + 'text' => $this->getResponseText($response['status'] ), 'error' => (isset($response['error']) ? $response['error'] : '') ); $this->response['data'][0] = $response['data']; @@ -274,22 +329,26 @@ private function setResponse( $response ) } else { - foreach( $response as $num => $data ) - {// loop through each response + // loop through each response + foreach($response as $num => $data ) + { + $this->response['response'][$num] = array( 'code' => $data['status'], - 'text' => $this->getResponseText( $data['status'] ), + 'text' => $this->getResponseText($data['status'] ), 'error' => (isset($data['error']) ? $data['error'] : '') ); $tmp = array(); $id = 0; - if ( is_array( $data['data'] ) ) - {// is there an array returned - foreach( $data['data'] as $n => $v ) + + // is there an array returned + if (is_array($data['data'] )) + { + foreach($data['data'] as $n => $v ) { - if ( is_array( $v ) ) + if (is_array($v )) { - foreach( $v as $key => $val ) + foreach($v as $key => $val ) { $tmp[$n][$key] = $val; } @@ -303,7 +362,8 @@ private function setResponse( $response ) $this->response['data'][$num] = $tmp; } else - {// the data response is text + { + // the data response is text $this->response['data'][$num] = $data['data']; } } @@ -315,7 +375,7 @@ private function setResponse( $response ) * @param integer $call_num Calling method number * @return array API Response Data */ - public function getResponse( $call_num = 0 ) + public function getResponse($call_num = 0) { return array( 'status' => $this->response['response'][$call_num], @@ -329,100 +389,79 @@ public function getResponse( $call_num = 0 ) * @param int $code [description] * @return string Response */ - private function getResponseText( &$code ) + private function getResponseText(&$code) { + // Load response messages array + $response_messages = include 'messages/response.php'; + // select a response code to display - switch( $code ) + if (array_key_exists($code, $response_messages)) + { + $message = $response_messages[$code]; + } + else { - case '-3' : $reason = 'Flagged API Key - Pemanently Banned'; break; - case '-2' : $reason = 'Flagged API Key - Too Many invalid access attempts - contact us'; break; - case '-1' : $reason = 'Flagged API Key - Temporarily Disabled - contact us'; break; - case '1' : $reason = 'Success -'; break; - case '2' : $reason = 'Invalid API key - App must be granted a valid key by ShiftPlanning'; break; - case '3' : $reason = 'Invalid token key - Please re-authenticate'; break; - case '4' : $reason = 'Invalid Method - No Method with that name exists in our API'; break; - case '5' : $reason = 'Invalid Module - No Module with that name exists in our API'; break; - case '6' : $reason = 'Invalid Action - No Action with that name exists in our API'; break; - case '7' : $reason = 'Authentication Failed - You do not have permissions to access the service'; break; - case '8' : $reason = 'Missing parameters - Your request is missing a required parameter'; break; - case '9' : $reason = 'Invalid parameters - Your request has an invalid parameter type'; break; - case '10' : $reason = 'Extra parameters - Your request has an extra/unallowed parameter type'; break; - case '12' : $reason = 'Create Failed - Your CREATE request failed'; break; - case '13' : $reason = 'Update Failed - Your UPDATE request failed'; break; - case '14' : $reason = 'Delete Failed - Your DELETE request failed'; break; - case '20' : $reason = 'Incorrect Permissions - You don\'t have the proper permissions to access this'; break; - case '90' : $reason = 'Suspended API key - Access for your account has been suspended, please contact ShiftPlanning'; break; - case '91' : $reason = 'Throttle exceeded - You have exceeded the max allowed requests. Try again later.'; break; - case '98' : $reason = 'Bad API Paramaters - Invalid POST request. See Manual.'; break; - case '99' : $reason = 'Service Offline - This service is temporarily offline. Try again later.'; break; - default : $reason = 'Error code not found'; break; + $message = $response_messages[0]; } - // return the reason text - return $reason; - } - - private function internal_errors( $errno ) - {// errors internal to the ShiftPlanning SDK - switch( $errno ) - {// internal error messages - case 1 : - $message = 'The requested API method was not found in this SDK.'; - break; - case 2 : - $message = 'The ShiftPlanning API is not responding.'; - break; - case 3 : - $message = 'You must use the login method before accessing other modules of this API.'; - break; - case 4 : - $message = 'A session has not yet been established.'; - break; - case 5 : - $message = 'You must specify your Developer Key when using this SDK.'; - break; - case 6 : - $message = 'The ShiftPlanning SDK needs the CURL PHP extension.'; - break; - case 7 : - $message = 'The ShiftPlanning SDK needs the JSON PHP extension.'; - break; - case 8 : - $message = 'File doesn\'t exist.'; - break; - case 9 : - $message = 'Could not find the correct mime for the file supplied.'; - break; - default : - $message = 'Could not find the requested error message.'; - break; + + return $message; + } + + /** + * Errors internal to the ShiftPlanning SDK + * @param [type] $errno [description] + * @return [type] [description] + */ + private function internal_errors($errno = 0) + { + // internal error messages + $internal_error_messages = include 'messages/internal_error.php'; + if (array_key_exists($errno, $internal_error_messages)) + { + $message = $internal_error_messages[$errno]; } + else + { + $message = $internal_error_messages[0]; + } + return $message; exit; } + /** + * create the api call + * @return [type] [description] + */ private function api() - {// create the api call - if ( $this->_callback == null ) - {// method to call after successful api request + { + if ($this->_callback == null ) + { + // method to call after successful api request $this->setCallback( 'getResponse' ); } - if ( $this->getSession() ) - {// session already established, use token + + // session already established, use token + if ($this->getSession()) + { // remove the developer key from the request, since it's not necessary - unset( $this->request['key'] ); + unset($this->request['key'] ); // set the token for this request, since the user is already authenticated $this->request['token'] = $_SESSION['token']; } else - {// session has not been established, use developer key to access API + { + // session has not been established, use developer key to access API try { - if ( isset( $this->_key ) ) - {// developer key is set + if (isset($this->_key )) + { + // developer key is set $this->request['key'] = $this->_key; } else - {// developer key is not set - throw new Exception( $this->internal_errors( 5 ) ); + { + // developer key is not set + throw new Exception($this->internal_errors( 5 )); } } catch( Exception $e ) @@ -434,153 +473,24 @@ private function api() return $this->perform_request(); } - private function getFileMimeType( $extension ) + /** + * Get the mime type + * @param [type] $extension [description] + * @return [type] [description] + */ + private function getFileMimeType($extension) { - $mimes = array( - "ez" => "application/andrew-inset", - "hqx" => "application/mac-binhex40", - "cpt" => "application/mac-compactpro", - "doc" => "application/msword", - "bin" => "application/octet-stream", - "dms" => "application/octet-stream", - "lha" => "application/octet-stream", - "lzh" => "application/octet-stream", - "exe" => "application/octet-stream", - "class" => "application/octet-stream", - "so" => "application/octet-stream", - "dll" => "application/octet-stream", - "oda" => "application/oda", - "pdf" => "application/pdf", - "ai" => "application/postscript", - "eps" => "application/postscript", - "ps" => "application/postscript", - "smi" => "application/smil", - "smil" => "application/smil", - "wbxml" => "application/vnd.wap.wbxml", - "wmlc" => "application/vnd.wap.wmlc", - "wmlsc" => "application/vnd.wap.wmlscriptc", - "bcpio" => "application/x-bcpio", - "vcd" => "application/x-cdlink", - "pgn" => "application/x-chess-pgn", - "cpio" => "application/x-cpio", - "csh" => "application/x-csh", - "dcr" => "application/x-director", - "dir" => "application/x-director", - "dxr" => "application/x-director", - "dvi" => "application/x-dvi", - "spl" => "application/x-futuresplash", - "gtar" => "application/x-gtar", - "hdf" => "application/x-hdf", - "js" => "application/x-javascript", - "skp" => "application/x-koan", - "skd" => "application/x-koan", - "skt" => "application/x-koan", - "skm" => "application/x-koan", - "latex" => "application/x-latex", - "nc" => "application/x-netcdf", - "cdf" => "application/x-netcdf", - "sh" => "application/x-sh", - "shar" => "application/x-shar", - "swf" => "application/x-shockwave-flash", - "sit" => "application/x-stuffit", - "sv4cpio" => "application/x-sv4cpio", - "sv4crc" => "application/x-sv4crc", - "tar" => "application/x-tar", - "tcl" => "application/x-tcl", - "tex" => "application/x-tex", - "texinfo" => "application/x-texinfo", - "texi" => "application/x-texinfo", - "t" => "application/x-troff", - "tr" => "application/x-troff", - "roff" => "application/x-troff", - "man" => "application/x-troff-man", - "me" => "application/x-troff-me", - "ms" => "application/x-troff-ms", - "ustar" => "application/x-ustar", - "src" => "application/x-wais-source", - "xhtml" => "application/xhtml+xml", - "xht" => "application/xhtml+xml", - "zip" => "application/zip", - "au" => "audio/basic", - "snd" => "audio/basic", - "mid" => "audio/midi", - "midi" => "audio/midi", - "kar" => "audio/midi", - "mpga" => "audio/mpeg", - "mp2" => "audio/mpeg", - "mp3" => "audio/mpeg", - "aif" => "audio/x-aiff", - "aiff" => "audio/x-aiff", - "aifc" => "audio/x-aiff", - "m3u" => "audio/x-mpegurl", - "ram" => "audio/x-pn-realaudio", - "rm" => "audio/x-pn-realaudio", - "rpm" => "audio/x-pn-realaudio-plugin", - "ra" => "audio/x-realaudio", - "wav" => "audio/x-wav", - "pdb" => "chemical/x-pdb", - "xyz" => "chemical/x-xyz", - "bmp" => "image/bmp", - "gif" => "image/gif", - "ief" => "image/ief", - "jpeg" => "image/jpeg", - "jpg" => "image/jpeg", - "jpe" => "image/jpeg", - "png" => "image/png", - "tiff" => "image/tiff", - "tif" => "image/tif", - "djvu" => "image/vnd.djvu", - "djv" => "image/vnd.djvu", - "wbmp" => "image/vnd.wap.wbmp", - "ras" => "image/x-cmu-raster", - "pnm" => "image/x-portable-anymap", - "pbm" => "image/x-portable-bitmap", - "pgm" => "image/x-portable-graymap", - "ppm" => "image/x-portable-pixmap", - "rgb" => "image/x-rgb", - "xbm" => "image/x-xbitmap", - "xpm" => "image/x-xpixmap", - "xwd" => "image/x-windowdump", - "igs" => "model/iges", - "iges" => "model/iges", - "msh" => "model/mesh", - "mesh" => "model/mesh", - "silo" => "model/mesh", - "wrl" => "model/vrml", - "vrml" => "model/vrml", - "css" => "text/css", - "html" => "text/html", - "htm" => "text/html", - "asc" => "text/plain", - "txt" => "text/plain", - "rtx" => "text/richtext", - "rtf" => "text/rtf", - "sgml" => "text/sgml", - "sgm" => "text/sgml", - "tsv" => "text/tab-seperated-values", - "wml" => "text/vnd.wap.wml", - "wmls" => "text/vnd.wap.wmlscript", - "etx" => "text/x-setext", - "xml" => "text/xml", - "xsl" => "text/xml", - "mpeg" => "video/mpeg", - "mpg" => "video/mpeg", - "mpe" => "video/mpeg", - "qt" => "video/quicktime", - "mov" => "video/quicktime", - "mxu" => "video/vnd.mpegurl", - "avi" => "video/x-msvideo", - "movie" => "video/x-sgi-movie", - "ice" => "x-conference-xcooltalk" - ); try - { - if ( $mimes[ $extension ] ) - {// mime found - return $mimes[ $extension ]; + { + $mimes = include 'config/mimes.php'; + if ($mimes[$extension]) + { + // mime found + return $mimes[$extension]; } else - {// mime not found + { + // mime not found throw new Exception( 'Mime for .' . $extension . ' not found' ); } } @@ -590,23 +500,30 @@ private function getFileMimeType( $extension ) } } - private function getFileData( $file ) - {// get file details, (data, length, mimetype) + /** + * Get file details, (data, length, mimetype) + * @param [type] $file [description] + * @return [type] [description] + */ + private function getFileData($file ) + { try { - if ( file_exists( $file ) ) - {// file - $file_data['filedata'] = file_get_contents( $file ); - $file_data['filelength'] = strlen( $file_data['filedata'] ); - if ( function_exists( 'mime_content_type' ) ) - {// mime_content_type function is available - $file_data['mimetype'] = mime_content_type( $file ); + if (file_exists($file )) + { + // file + $file_data['filedata'] = file_get_contents($file ); + $file_data['filelength'] = strlen($file_data['filedata'] ); + if (function_exists( 'mime_content_type' )) + { + // mime_content_type function is available + $file_data['mimetype'] = mime_content_type($file ); } else { $parts = explode( '.', $file ); - $extension = strtolower( $parts[ sizeOf( $parts ) - 1 ] ); - $file_data['mimetype'] = $this->getFileMimeType( $extension ); + $extension = strtolower($parts[ sizeOf($parts ) - 1 ] ); + $file_data['mimetype'] = $this->getFileMimeType($extension ); } return array( @@ -617,7 +534,7 @@ private function getFileData( $file ) } else { - throw new Exception( $this->internal_errors( 8 ) ); + throw new Exception($this->internal_errors( 8 )); } } catch( Exception $e ) @@ -626,60 +543,69 @@ private function getFileData( $file ) } } + /** + * perform the api request + * @return [type] [description] + */ private function perform_request() - {// perform the api request + { try { - $ch = curl_init( self::api_endpoint ); + $ch = curl_init( self::API_ENDPOINT ); $filedata = ''; - if ( is_array( $this->requests ) ) + if (is_array($this->requests )) { - foreach( $this->requests as $key => $request ) + foreach($this->requests as $key => $request ) { - if ( isset($request['filedata']) ) + if (isset($request['filedata'])) { $filedata = $request['filedata']; - unset( $this->requests[$key]['filedata'] ); + unset($this->requests[$key]['filedata'] ); } } } - $post = $filedata ? array( 'data'=> json_encode( $this->getRequest() ), - 'filedata' => $filedata ) : array( 'data' => json_encode( $this->getRequest() ) ); + $post = $filedata ? array( 'data'=> json_encode($this->getRequest()), + 'filedata' => $filedata ) : array( 'data' => json_encode($this->getRequest()) ); - curl_setopt( $ch, CURLOPT_URL, self::api_endpoint ); - curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); - curl_setopt( $ch, CURLOPT_POST, 1 ); - curl_setopt( $ch, CURLOPT_POSTFIELDS, $post ); + curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // return the response from the api - $response = curl_exec( $ch ); - $http_response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); - curl_close( $ch ); + $response = curl_exec($ch); + $http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); - if ( $http_response_code == 200 ) - {// response from API was a successful - $temp = json_decode( $response, true ); + // response from API was a successful + if ($http_response_code == 200 ) + { + $temp = json_decode($response, TRUE ); // decode the response and store each call response in its own array - $this->setResponse( $temp ); - if ( $this->_init == 1 ) - {// initialize a session + $this->setResponse($temp ); + + // initialize a session + if ($this->_init == 1 ) + { $this->setSession(); } + // raw response call $this->raw_response = $temp; - if ( $this->_debug == true ) - {// debug mode is on - $request = json_encode( $this->getRequest() ); + if ($this->_debug == TRUE ) + { + // debug mode is on + $request = json_encode($this->getRequest()); $db = fopen('log.txt', 'a'); $tmp_vals = array(); - if ( is_array( $this->response['data'][0] ) ) + if (is_array($this->response['data'][0] )) { - foreach( $this->response['data'] as $n => $v ) + foreach($this->response['data'] as $n => $v ) { - foreach( $v as $key => $val ) + foreach($v as $key => $val ) { $tmp_vals[$n][$key] = $val; } @@ -687,23 +613,24 @@ private function perform_request() } else { - foreach( $this->response['data'] as $n => $v ) + foreach($this->response['data'] as $n => $v ) { $tmp_vals[$n] = $v; } } - fwrite( $db, date('m-d-Y h:i a'). "\n" . 'REQUEST: ' . $request . "\n" + fwrite($db, date('m-d-Y h:i a'). "\n" . 'REQUEST: ' . $request . "\n" . 'RESPONSE STATUS: (' . $this->response['response'][0]['code'] . ') ' . $this->response['response'][0]['text'] . " -- " . $this->response['response'][0]['error'] . "\n" - . 'RESPONSE DATA: ' . json_encode( $tmp_vals ) . "\n\n" ); - fclose( $db ); + . 'RESPONSE DATA: ' . json_encode($tmp_vals ) . "\n\n" ); + fclose($db ); } // perform the callback method return $this->{ $this->_callback }(); } else - {// response from API was unsuccessful - throw new Exception( $this->internal_errors( 2 ) ); + { + // response from API was unsuccessful + throw new Exception($this->internal_errors( 2 )); } } catch( Exception $e ) @@ -712,12 +639,17 @@ private function perform_request() } } - /* + /** * User Authentication Methods - * */ - public function doLogin( $user = array() ) - {// perform a login api call + + /** + * Perform a login api call + * @param array $user [description] + * @return [type] [description] + */ + public function doLogin($user = array()) + { return $this->setRequest( array( 'module' => 'staff.login', @@ -728,17 +660,24 @@ public function doLogin( $user = array() ) ); } + /** + * Erase token and user data from current session + */ public function doLogout() - {// erase token and user data from current session + { $this->destroySession(); } - /* + /** * Message Methods - * + */ + + /** + * Get messages for the currently logged in user + * @return [type] [description] */ public function getMessages() - {// get messages for the currently logged in user + { return $this->setRequest( array( 'module' => 'messaging.messages', @@ -747,8 +686,13 @@ public function getMessages() ); } - public function getMessageDetails( $id ) - {// get message details for a specific message + /** + * Get message details for a specific message + * @param [type] $id [description] + * @return [type] [description] + */ + public function getMessageDetails($id) + { return $this->setRequest( array( 'module' => 'messaging.message', @@ -758,8 +702,13 @@ public function getMessageDetails( $id ) ); } - public function createMessage( $message = array() ) - {// create a new message + /** + * create a new message + * @param array $message [description] + * @return [type] [description] + */ + public function createMessage($message = array()) + { return $this->setRequest( array_merge( array( @@ -771,8 +720,13 @@ public function createMessage( $message = array() ) ); } - public function deleteMessage( $id ) - {// delete a message + /** + * delete a message + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteMessage($id) + { return $this->setRequest( array( 'module' => 'messaging.message', @@ -782,8 +736,12 @@ public function deleteMessage( $id ) ); } + /** + * Get message wall + * @return [type] [description] + */ public function getWallMessages() - {// get message wall + { return $this->setRequest( array( 'module' => 'messaging.wall', @@ -792,8 +750,13 @@ public function getWallMessages() ); } - public function createWallMessage( $message = array() ) - {// create a wall message + /** + * create a wall message + * @param array $message [description] + * @return [type] [description] + */ + public function createWallMessage($message = array()) + { return $this->setRequest( array_merge( array( @@ -805,8 +768,14 @@ public function createWallMessage( $message = array() ) ); } - public function deleteWallMessage( $id, $details = array() ) - {// delete a wall message + /** + * delete a wall message + * @param [type] $id [description] + * @param array $details [description] + * @return [type] [description] + */ + public function deleteWallMessage($id, $details = array()) + { return $this->setRequest( array_merge( array( @@ -823,8 +792,14 @@ public function deleteWallMessage( $id, $details = array() ) * Staff Methods * */ + + + /** + * Get a list of employees + * @return [type] [description] + */ public function getEmployees() - {// get a list of employees + { return $this->setRequest( array( 'module' => 'staff.employees', @@ -833,8 +808,13 @@ public function getEmployees() ); } - public function getEmployeeDetails( $id ) - {// get details for a specific employee + /** + * Get details for a specific employee + * @param [type] $id [description] + * @return [type] [description] + */ + public function getEmployeeDetails($id) + { return $this->setRequest( array( 'module' => 'staff.employee', @@ -844,8 +824,14 @@ public function getEmployeeDetails( $id ) ); } - public function updateEmployee( $id, $new_data = array() ) - {// update an employee record + /** + * update an employee record + * @param [type] $id [description] + * @param array $new_data [description] + * @return [type] [description] + */ + public function updateEmployee($id, $new_data = array()) + { return $this->setRequest( array_merge( array( @@ -858,8 +844,13 @@ public function updateEmployee( $id, $new_data = array() ) ); } - public function createEmployee( $data ) - {// create a new employee record + /** + * create a new employee record + * @param [type] $data [description] + * @return [type] [description] + */ + public function createEmployee($data) + { return $this->setRequest( array_merge( array( @@ -871,8 +862,13 @@ public function createEmployee( $data ) ); } - public function deleteEmployee( $id ) - {// delete an employee + /** + * delete an employee + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteEmployee($id) + { return $this->setRequest( array( 'module' => 'staff.employee', @@ -882,8 +878,12 @@ public function deleteEmployee( $id ) ); } + /** + * Get staff skills + * @return [type] [description] + */ public function getStaffSkills() - {// get staff skills + { return $this->setRequest( array( 'module' => 'staff.skills', @@ -892,8 +892,13 @@ public function getStaffSkills() ); } - public function getStaffSkillDetails( $id ) - {// get staff skill details + /** + * Get staff skill details + * @param [type] $id [description] + * @return [type] [description] + */ + public function getStaffSkillDetails($id) + { return $this->setRequest( array( 'module' => 'staff.skill', @@ -903,8 +908,13 @@ public function getStaffSkillDetails( $id ) ); } - public function createStaffSkill( $skill_details = array() ) - {// create staff skill + /** + * create staff skill + * @param array $skill_details [description] + * @return [type] [description] + */ + public function createStaffSkill($skill_details = array()) + { return $this->setRequest( array_merge( array( @@ -916,8 +926,14 @@ public function createStaffSkill( $skill_details = array() ) ); } - public function updateStaffSkill( $id, $skill_details = array() ) - {// update staff skill + /** + * update staff skill + * @param [type] $id [description] + * @param array $skill_details [description] + * @return [type] [description] + */ + public function updateStaffSkill($id, $skill_details = array()) + { return $this->setRequest( array_merge( array( @@ -930,8 +946,13 @@ public function updateStaffSkill( $id, $skill_details = array() ) ); } - public function deleteStaffSkill( $id ) - {// delete staff skill + /** + * delete staff skill + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteStaffSkill($id) + { return $this->setRequest( array( 'module' => 'staff.skill', @@ -941,8 +962,13 @@ public function deleteStaffSkill( $id ) ); } - public function createPing( $ping_data = array() ) - {// create a ping + /** + * create a ping + * @param array $ping_data [description] + * @return [type] [description] + */ + public function createPing($ping_data = array()) + { return $this->setRequest( array_merge( array( @@ -954,12 +980,16 @@ public function createPing( $ping_data = array() ) ); } - /* + /** * Schedule Methods - * + */ + + /** + * Get schedules + * @return [type] [description] */ public function getSchedules() - {// get schedules + { return $this->setRequest( array( 'module' => 'schedule.schedules', @@ -968,8 +998,13 @@ public function getSchedules() ); } - public function getScheduleDetails( $id ) - {// get schedule details + /** + * Get schedule details + * @param [type] $id [description] + * @return [type] [description] + */ + public function getScheduleDetails($id) + { return $this->setRequest( array( 'module' => 'schedule.schedule', @@ -979,8 +1014,13 @@ public function getScheduleDetails( $id ) ); } - public function createSchedule( $schedule_details = array() ) - {// create a new schedule + /** + * Create a new schedule + * @param array $schedule_details [description] + * @return [type] [description] + */ + public function createSchedule($schedule_details = array()) + { return $this->setRequest( array_merge( array( @@ -992,8 +1032,14 @@ public function createSchedule( $schedule_details = array() ) ); } - public function updateSchedule( $id, $schedule_details = array() ) - {// update an existing schedule + /** + * update an existing schedule + * @param [type] $id [description] + * @param array $schedule_details [description] + * @return [type] [description] + */ + public function updateSchedule($id, $schedule_details = array()) + { return $this->setRequest( array_merge( array( @@ -1006,8 +1052,13 @@ public function updateSchedule( $id, $schedule_details = array() ) ); } - public function deleteSchedule( $id ) - {// delete an existing schedule + /** + * delete an existing schedule + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteSchedule($id) + { return $this->setRequest( array( 'module' => 'schedule.schedule', @@ -1017,8 +1068,12 @@ public function deleteSchedule( $id ) ); } + /** + * Get shifts + * @return [type] [description] + */ public function getShifts() - {// get shifts + { return $this->setRequest( array( 'module' => 'schedule.shifts', @@ -1027,8 +1082,13 @@ public function getShifts() ); } - public function getShiftDetails( $id ) - {// get shift details + /** + * Get shift details + * @param [type] $id [description] + * @return [type] [description] + */ + public function getShiftDetails($id) + { return $this->setRequest( array( 'module' => 'schedule.shift', @@ -1038,8 +1098,14 @@ public function getShiftDetails( $id ) ); } - public function updateShift( $id, $shift_details = array() ) - {// update shift details + /** + * update shift details + * @param [type] $id [description] + * @param array $shift_details [description] + * @return [type] [description] + */ + public function updateShift($id, $shift_details = array()) + { return $this->setRequest( array_merge( array( @@ -1052,8 +1118,13 @@ public function updateShift( $id, $shift_details = array() ) ); } - public function createShift( $shift_details = array() ) - {// create a new shift + /** + * create a new shift + * @param array $shift_details [description] + * @return [type] [description] + */ + public function createShift($shift_details = array()) + { return $this->setRequest( array_merge( array( @@ -1065,8 +1136,13 @@ public function createShift( $shift_details = array() ) ); } - public function deleteShift( $id ) - {// delete a shift + /** + * delete a shift + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteShift($id) + { return $this->setRequest( array( 'module' => 'schedule.shift', @@ -1076,8 +1152,13 @@ public function deleteShift( $id ) ); } - public function getVacationSchedules( $time_period = array() ) - {// get schedule vacations, pass start and end params to get vacations within a certian time-period + /** + * Get schedule vacations, pass start and end params to get vacations within a certian time-period + * @param array $time_period [description] + * @return [type] [description] + */ + public function getVacationSchedules($time_period = array()) + { return $this->setRequest( array_merge( array( @@ -1089,8 +1170,13 @@ public function getVacationSchedules( $time_period = array() ) ); } - public function getVacationScheduleDetails( $id ) - {// get vacation schedule details + /** + * Get vacation schedule details + * @param [type] $id [description] + * @return [type] [description] + */ + public function getVacationScheduleDetails($id) + { return $this->setRequest( array( 'module' => 'schedule.vacation', @@ -1100,8 +1186,13 @@ public function getVacationScheduleDetails( $id ) ); } - public function createVacationSchedule( $vacation_details = array() ) - {// create a vacation schedule + /** + * create a vacation schedule + * @param array $vacation_details [description] + * @return [type] [description] + */ + public function createVacationSchedule($vacation_details = array()) + { return $this->setRequest( array_merge( array( @@ -1113,8 +1204,14 @@ public function createVacationSchedule( $vacation_details = array() ) ); } - public function updateVacationSchedule( $id, $vacation_details = array() ) - {// update a vacation schedule + /** + * update a vacation schedule + * @param [type] $id [description] + * @param array $vacation_details [description] + * @return [type] [description] + */ + public function updateVacationSchedule($id, $vacation_details = array()) + { return $this->setRequest( array_merge( array( @@ -1127,8 +1224,13 @@ public function updateVacationSchedule( $id, $vacation_details = array() ) ); } - public function deleteVacationSchedule( $id ) - {// delete a vacation schedule + /** + * delete a vacation schedule + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteVacationSchedule($id) + { return $this->setRequest( array( 'module' => 'schedule.vacation', @@ -1138,8 +1240,13 @@ public function deleteVacationSchedule( $id ) ); } - public function getScheduleConflicts( $time_period = array() ) - {// get schedule conflicts + /** + * Get schedule conflicts + * @param array $time_period [description] + * @return [type] [description] + */ + public function getScheduleConflicts($time_period = array()) + { return $this->setRequest( array_merge( array( @@ -1155,8 +1262,13 @@ public function getScheduleConflicts( $time_period = array() ) * Administration Methods * */ + + /** + * Get admin settings + * @return [type] [description] + */ public function getAdminSettings() - {// get admin settings + { return $this->setRequest( array( 'module' => 'admin.settings', @@ -1165,8 +1277,13 @@ public function getAdminSettings() ); } - public function updateAdminSettings( $settings = array() ) - {// update admin settings + /** + * Update admin settings + * @param array $settings [description] + * @return [type] [description] + */ + public function updateAdminSettings($settings = array()) + { return $this->setRequest( array_merge( array( @@ -1178,8 +1295,12 @@ public function updateAdminSettings( $settings = array() ) ); } + /** + * Get administrator file listing + * @return [type] [description] + */ public function getAdminFiles() - {// get administrator file listing + { return $this->setRequest( array( 'module' => 'admin.files', @@ -1188,8 +1309,13 @@ public function getAdminFiles() ); } - public function getAdminFileDetails( $id ) - {// get admin file details + /** + * Get admin file details + * @param [type] $id [description] + * @return [type] [description] + */ + public function getAdminFileDetails($id) + { return $this->setRequest( array( 'module' => 'admin.file', @@ -1199,8 +1325,14 @@ public function getAdminFileDetails( $id ) ); } - public function updateAdminFile( $id, $details = array() ) - {// update admin file details + /** + * update admin file details + * @param [type] $id [description] + * @param array $details [description] + * @return [type] [description] + */ + public function updateAdminFile($id, $details = array()) + { return $this->setRequest( array_merge( array( @@ -1213,9 +1345,14 @@ public function updateAdminFile( $id, $details = array() ) ); } - public function createAdminFile( $file_details = array() ) - {// create new admin file - $file_details = array_merge( $file_details, $this->getFileData( $file_details['filename'] ) ); + /** + * create new admin file + * @param array $file_details [description] + * @return [type] [description] + */ + public function createAdminFile($file_details = array()) + { + $file_details = array_merge($file_details, $this->getFileData($file_details['filename'] )); return $this->setRequest( array_merge( array( @@ -1227,8 +1364,13 @@ public function createAdminFile( $file_details = array() ) ); } - public function deleteAdminFile( $id ) - {// delete admin file + /** + * delete admin file + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteAdminFile($id) + { return $this->setRequest( array( 'module' => 'admin.file', @@ -1238,8 +1380,12 @@ public function deleteAdminFile( $id ) ); } + /** + * Get admin backups + * @return [type] [description] + */ public function getAdminBackups() - {// get admin backups + { return $this->setRequest( array( 'module' => 'admin.backups', @@ -1248,8 +1394,13 @@ public function getAdminBackups() ); } - public function getAdminBackupDetails( $id ) - {// get admin backup details + /** + * Get admin backup details + * @param [type] $id [description] + * @return [type] [description] + */ + public function getAdminBackupDetails($id) + { return $this->setRequest( array( 'module' => 'admin.backup', @@ -1259,9 +1410,14 @@ public function getAdminBackupDetails( $id ) ); } - public function createAdminBackup( $backup_details = array() ) - {// create an admin backup - $backup_details = array_merge( $backup_details, $this->getFileData( $backup_details['filename'] ) ); + /** + * create an admin backup + * @param array $backup_details [description] + * @return [type] [description] + */ + public function createAdminBackup($backup_details = array() ) + { + $backup_details = array_merge($backup_details, $this->getFileData($backup_details['filename'] )); return $this->setRequest( array_merge( array( @@ -1273,8 +1429,13 @@ public function createAdminBackup( $backup_details = array() ) ); } - public function deleteAdminBackup( $id ) - {// delete an admin backup + /** + * delete an admin backup + * @param [type] $id [description] + * @return [type] [description] + */ + public function deleteAdminBackup($id) + { return $this->setRequest( array( 'module' => 'admin.backup', @@ -1288,8 +1449,13 @@ public function deleteAdminBackup( $id ) * API Methods * */ + + /** + * Get api config + * @return [type] [description] + */ public function getAPIConfig() - {// get api config + { return $this->setRequest( array( 'module' => 'api.config', @@ -1298,8 +1464,12 @@ public function getAPIConfig() ); } + /** + * Get all available api methods + * @return [type] [description] + */ public function getAPIMethods() - {// get all available api methods + { return $this->setRequest( array( 'module' => 'api.methods', From 2ae01cea6acf0af32bbbbdbcb54446756f009204 Mon Sep 17 00:00:00 2001 From: Joel Pittet Date: Mon, 26 Nov 2012 15:22:02 -0800 Subject: [PATCH 3/5] remove ending ?>, change class name and some cleanup, set the correct src path --- examples/examples.php | 51 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/examples/examples.php b/examples/examples.php index f61fc34..ee5be6f 100644 --- a/examples/examples.php +++ b/examples/examples.php @@ -1,10 +1,10 @@ 'XXXXXXXXXXXXXXXXXX' // enter your developer key ) @@ -57,31 +57,31 @@ /** * Quick Access ShiftPlanning SDK Methods: * doLogin( $array_of_user_data ) - * doLogout( ) - * getMessages( ) + * doLogout() + * getMessages() * getMessageDetails( $message_id ) * createMessage( $array_of_message_data ) * deleteMessage( $message_id ) - * getWallMessages( ) + * getWallMessages() * createWallMessage( $array_of_message_data ) * deleteWallMessage( $message_id, $array_of_other_message_data ) - * getEmployees( ) + * getEmployees() * getEmployeeDetails( $employee_id_number ) * updateEmployee( $employee_id, $array_of_updated_employee_data ) * createEmployee( $array_of_employee_data ) * deleteEmployee( $employee_id ) - * getStaffSkills( ) + * getStaffSkills() * getStaffSkillDetails( $skill_id ) * createStaffSkill( $array_of_skill_data ) * updateStaffSkill( $skill_id, $array_of_skill_data ) * deleteStaffSkill( $skill_id ) * createPing( $array_of_ping_data ) - * getSchedules( ) + * getSchedules() * getScheduleDetails( $schedule_id ) * createSchedule( $array_of_schedule_data ) * updateSchedule( $schedule_id, $array_of_schedule_data ) * deleteSchedule( $schedule_id ) - * getShifts( ) + * getShifts() * getShiftDetails( $shift_id ) * updateShift( $shift_id, $array_of_shift_data ) * createShift( $array_of_shift_data ) @@ -91,20 +91,20 @@ * createVacationSchedule( $array_of_schedule_data ) * updateVacationSchedule( $schedule_id, $array_of_schedule_data ) * deleteVacationSchedule( $schedule_id ) - * getScheduleConflicts( ) - * getAdminSettings( ) + * getScheduleConflicts() + * getAdminSettings() * updateAdminSettings( $array_of_new_settings ) - * getAdminFiles( ) + * getAdminFiles() * getAdminFileDetails( $file_id ) * updateAdminFile( $file_id, $array_of_file_data ) * createAdminFile( $array_of_file_data ) * deleteAdminFile( $file_id ) - * getAdminBackups( ) + * getAdminBackups() * getAdminBackupDetails( $backup_id ) * createAdminBackup( $array_of_backup_data ) * deleteAdminBackup( $backup_id ) - * getAPIConfig( ) - * getAPIMethods( ) + * getAPIConfig() + * getAPIMethods() */ /** @@ -116,7 +116,7 @@ * ) * ) * - * For methods that return multiple objects (as in the case for the getMessages( ) method + * For methods that return multiple objects (as in the case for the getMessages() method * responses will look like this, where the indexes [0], [1] would be replaced with the * message you're looking to display * @@ -136,7 +136,7 @@ */ // Quick-Access Methods, to perform API calls more easily - $employees = $shiftplanning->getEmployees( ); // returns all employees + $employees = $shiftplanning->getEmployees(); // returns all employees $employee_1_wage = $employees['data'][0]['wage']; echo "Employee 1 Wage: $" . $employee_1_wage . "
"; echo "Employee 1 Nick Name: " . $employees['data'][0]['nick_name'] . "
"; @@ -149,7 +149,7 @@ ) ); - if( $update_employee_record_response['status']['code'] == 1 ) + if ( $update_employee_record_response['status']['code'] == 1 ) {// employee update successful echo "Employee record updated.
"; } @@ -216,16 +216,17 @@ } - if( $get_schedules['status']['code'] == 1 ) - {// schedules were retrieved successfully + // schedules were retrieved successfully + if ( $get_schedules['status']['code'] == 1 ) + { print_r( $get_schedules['data'][0] ); // print data from first schedule returned } - if( $get_settings['status']['code'] == 1 ) - {// admin settings retrieved successfully + // admin settings retrieved successfully + if ( $get_settings['status']['code'] == 1 ) + { print_r( $get_settings ); } - //$shiftplanning->doLogout(); -} -?> \ No newline at end of file + $shiftplanning->doLogout(); +} \ No newline at end of file From ff0ddc8fc05d387296bf4953a6a6934cf7bae442 Mon Sep 17 00:00:00 2001 From: Joel Pittet Date: Mon, 26 Nov 2012 15:55:39 -0800 Subject: [PATCH 4/5] more cleanup --- README.md | 2 +- examples/examples.php | 6 +- src/messages/internal_error.php | 2 - src/messages/response.php | 4 +- src/shiftplanning.php | 361 ++++++++++++++++++-------------- 5 files changed, 211 insertions(+), 164 deletions(-) diff --git a/README.md b/README.md index da6e558..ea98b99 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ have is: require './shiftplanning.php'; - $shiftplanning = new shiftplanning( + $shiftplanning = new Shiftplanning( array( 'key' => 'XXXXXXXXXXXXXXXXXX' // enter your developer key ) diff --git a/examples/examples.php b/examples/examples.php index ee5be6f..54a1384 100644 --- a/examples/examples.php +++ b/examples/examples.php @@ -22,12 +22,12 @@ // check for a current active session // if a session exists, $session will now hold the user's information -$session = $shiftplanning->getSession( ); +$session = $shiftplanning->getSession(); echo "appKey: " . $shiftplanning->getAppKey( ) . "
"; // returns the developer key currently set echo "appToken: " . $shiftplanning->getAppToken( ) . "
"; // returns the token for the current session, error if not yet set -if( !$session ) +if ( !$session ) {// if a session hasn't been started, create one // perform a single API call to authenticate a user @@ -38,7 +38,7 @@ ) ); - if( $response['status']['code'] == 1 ) + if ( $response['status']['code'] == 1 ) {// check to make sure that login was successful $session = $shiftplanning->getSession( ); // return the session data after successful login echo "Hi, " . $session['employee']['name'] . "
"; diff --git a/src/messages/internal_error.php b/src/messages/internal_error.php index ca10307..8ec81f2 100644 --- a/src/messages/internal_error.php +++ b/src/messages/internal_error.php @@ -14,5 +14,3 @@ 8 => "File doesn't exist.", 9 => 'Could not find the correct mime for the file supplied.', ); - - \ No newline at end of file diff --git a/src/messages/response.php b/src/messages/response.php index 19b965d..85f7da6 100644 --- a/src/messages/response.php +++ b/src/messages/response.php @@ -7,7 +7,7 @@ '-3' => 'Flagged API Key - Pemanently Banned', '-2' => 'Flagged API Key - Too Many invalid access attempts - contact us', '-1' => 'Flagged API Key - Temporarily Disabled - contact us', - '1' => 'Success -', + '1' => 'Success', '2' => 'Invalid API key - App must be granted a valid key by ShiftPlanning', '3' => 'Invalid token key - Please re-authenticate', '4' => 'Invalid Method - No Method with that name exists in our API', @@ -25,4 +25,4 @@ '91' => 'Throttle exceeded - You have exceeded the max allowed requests. Try again later.', '98' => 'Bad API Paramaters - Invalid POST request. See Manual.', '99' => 'Service Offline - This service is temporarily offline. Try again later.', -); \ No newline at end of file +); diff --git a/src/shiftplanning.php b/src/shiftplanning.php index 126610e..0916be9 100644 --- a/src/shiftplanning.php +++ b/src/shiftplanning.php @@ -8,53 +8,53 @@ /** * Quick Access ShiftPlanning SDK Methods: - * doLogin($array_of_user_data ) + * doLogin($array_of_user_data) * doLogout() * getMessages() - * getMessageDetails($message_id ) - * createMessage($array_of_message_data ) - * deleteMessage($message_id ) + * getMessageDetails($message_id) + * createMessage($array_of_message_data) + * deleteMessage($message_id) * getWallMessages() - * createWallMessage($array_of_message_data ) - * deleteWallMessage($message_id, $array_of_other_message_data ) + * createWallMessage($array_of_message_data) + * deleteWallMessage($message_id, $array_of_other_message_data) * getEmployees() - * getEmployeeDetails($employee_id_number ) - * updateEmployee($employee_id, $array_of_updated_employee_data ) - * createEmployee($array_of_employee_data ) - * deleteEmployee($employee_id ) + * getEmployeeDetails($employee_id_number) + * updateEmployee($employee_id, $array_of_updated_employee_data) + * createEmployee($array_of_employee_data) + * deleteEmployee($employee_id) * getStaffSkills() - * getStaffSkillDetails($skill_id ) - * createStaffSkill($array_of_skill_data ) - * updateStaffSkill($skill_id, $array_of_skill_data ) - * deleteStaffSkill($skill_id ) - * createPing($array_of_ping_data ) + * getStaffSkillDetails($skill_id) + * createStaffSkill($array_of_skill_data) + * updateStaffSkill($skill_id, $array_of_skill_data) + * deleteStaffSkill($skill_id) + * createPing($array_of_ping_data) * getSchedules() - * getScheduleDetails($schedule_id ) - * createSchedule($array_of_schedule_data ) - * updateSchedule($schedule_id, $array_of_schedule_data ) - * deleteSchedule($schedule_id ) + * getScheduleDetails($schedule_id) + * createSchedule($array_of_schedule_data) + * updateSchedule($schedule_id, $array_of_schedule_data) + * deleteSchedule($schedule_id) * getShifts() - * getShiftDetails($shift_id ) - * updateShift($shift_id, $array_of_shift_data ) - * createShift($array_of_shift_data ) - * deleteShift($shift_id ) - * getVacationSchedules($time_period_array ) // e.g. getVacationSchedules( array( 'start' => '', 'end' => '' )); - * getVacationScheduleDetails($schedule_id ) - * createVacationSchedule($array_of_schedule_data ) - * updateVacationSchedule($schedule_id, $array_of_schedule_data ) - * deleteVacationSchedule($schedule_id ) + * getShiftDetails($shift_id) + * updateShift($shift_id, $array_of_shift_data) + * createShift($array_of_shift_data) + * deleteShift($shift_id) + * getVacationSchedules($time_period_array) // e.g. getVacationSchedules( array( 'start' => '', 'end' => '')); + * getVacationScheduleDetails($schedule_id) + * createVacationSchedule($array_of_schedule_data) + * updateVacationSchedule($schedule_id, $array_of_schedule_data) + * deleteVacationSchedule($schedule_id) * getScheduleConflicts() * getAdminSettings() - * updateAdminSettings($array_of_new_settings ) + * updateAdminSettings($array_of_new_settings) * getAdminFiles() - * getAdminFileDetails($file_id ) - * updateAdminFile($file_id, $array_of_file_data ) - * createAdminFile($array_of_file_data ) - * deleteAdminFile($file_id ) + * getAdminFileDetails($file_id) + * updateAdminFile($file_id, $array_of_file_data) + * createAdminFile($array_of_file_data) + * deleteAdminFile($file_id) * getAdminBackups() - * getAdminBackupDetails($backup_id ) - * createAdminBackup($array_of_backup_data ) - * deleteAdminBackup($backup_id ) + * getAdminBackupDetails($backup_id) + * createAdminBackup($array_of_backup_data) + * deleteAdminBackup($backup_id) * getAPIConfig() * getAPIMethods() */ @@ -62,7 +62,7 @@ /** * All Quick-Access methods return a response like this: * array( - * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any' ), + * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), * 'data' => array( * 'field_name' => 'value' * ) @@ -73,7 +73,7 @@ * message you're looking to display * * array( - * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any' ), + * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), * 'data' => array( * [0] => array ( * 'id' => 1, @@ -90,14 +90,47 @@ class Shiftplanning { private $_key; + private $_callback; + private $_init; - private $_debug; + + /** + * @var boolean In debug mode + */ + private $_debug = FALSE; + + /** + * @var array + */ private $request = array(); + + /** + * @var array + */ private $requests = array(); + + /** + * @var array + */ private $response = array(); + + /** + * @var array + */ private $raw_response = array(); + /** + * @var array cached storage of internal error messages + */ + private $internal_errors = array(); + + /** + * @var array cached storage of response messages + */ + private $response_messages = array(); + + // constants const SESSION_IDENTIFIER = 'SP'; const API_ENDPOINT = 'http://www.shiftplanning.com/api/'; @@ -112,39 +145,38 @@ public function __construct($config = array()) { try { - $this->_debug = FALSE; $this->startSession(); // set the developer key - $this->setAppKey($config['key'] ); + $this->setAppKey($config['key']); - if (!function_exists( 'curl_init' )) + if (!function_exists( 'curl_init')) { // curl is not available - throw new Exception($this->internal_errors( 6 )); + throw new Exception($this->internal_errors(6)); } - if (!function_exists( 'json_decode' )) + if (!function_exists( 'json_decode')) { // json_decode is not available - throw new Exception($this->internal_errors( 7 )); + throw new Exception($this->internal_errors(7)); } } - catch( Exception $e ) + catch( Exception $e) { echo $e->getMessage(); exit; } } /** - * turn debug on + * Turn debug on * @param boolean $switch [description] */ - public function setDebug($switch = FALSE ) + public function setDebug($switch = FALSE) { // delete previous log file if (file_exists('log.txt')) { - unlink( 'log.txt' ); + unlink( 'log.txt'); } $this->_debug = TRUE; } @@ -152,7 +184,7 @@ public function setDebug($switch = FALSE ) // start the session protected function startSession() { - session_name( self::SESSION_IDENTIFIER ); + session_name( self::SESSION_IDENTIFIER); session_start(); } @@ -172,10 +204,10 @@ private function destroySession() )); // logout successful, remove local session data - if ($logout['status']['code'] == 1 ) + if ($logout['status']['code'] == 1) { - unset($_SESSION['token'] ); - unset($_SESSION['data'] ); + unset($_SESSION['token']); + unset($_SESSION['data']); # REMOVE COOKIE setcookie("mobile_token", $this->response['token'][0], time()-(30*86400), "/", ".shiftplanning.com"); @@ -184,13 +216,13 @@ private function destroySession() } /** - * check whether a valid session has been established + * Check whether a valid session has been established * @return [type] [description] */ public function getSession() { // user is already authenticated - if (isset($_SESSION['token'] )) + if (isset($_SESSION['token'])) { return $_SESSION['data']; } @@ -202,17 +234,17 @@ public function getSession() } /** - * set the method to call after successful api call + * Set the method to call after successful api call * @param [type] $callback [description] */ - private function setCallback($callback ) + private function setCallback($callback) { $this->_callback = $callback; return $this->_callback; } /** - * return the raw response data + * Return the raw response data * @return [type] [description] */ public function getRawResponse() @@ -221,7 +253,7 @@ public function getRawResponse() } /** - * return the developer key + * Return the developer key * @return [type] [description] */ public function getAppKey() @@ -233,7 +265,7 @@ public function getAppKey() * Set the developer key to use * @param [type] $key [description] */ - public function setAppKey($key ) + public function setAppKey($key) { $this->_key = $key; return $this->_key; @@ -255,40 +287,40 @@ public function getAppToken() else { // user not authenticated, return an error - throw new Exception($this->internal_errors( 4 )); + throw new Exception($this->internal_errors(4)); } } - catch( Exception $e ) + catch( Exception $e) { echo $e->getMessage(); } } /** - * set the request parameters + * Set the request parameters * @param array $requests [description] */ public function setRequest($requests = array()) { // clear out previous request data - unset($this->requests ); + unset($this->requests); // set the default response type of JSON $this->request['output'] = self::OUTPUT_TYPE; $this->_init = 0; - foreach($requests as $r => $v ) + foreach($requests as $r => $v) { // loop through each request array - if (is_array($v )) + if (is_array($v)) { $this->requests[] = $v; } else { // automatically initialize session after this API call - if ($requests['module'] == 'staff.login' ) + if ($requests['module'] == 'staff.login') { $this->_init = 1; } @@ -300,28 +332,28 @@ public function setRequest($requests = array()) } /** - * return the request parameters + * Return the request parameters * @return [type] [description] */ public function getRequest() { - return array_merge($this->request, array( 'request' => $this->requests )); + return array_merge($this->request, array( 'request' => $this->requests)); } /** - * set the response data + * Set the response data * @param [type] $response [description] */ - private function setResponse($response ) + private function setResponse($response) { // remove previous response data - unset($this->response ); + unset($this->response); // set new response data - if (! isset($response[0]) || !is_array($response[0] )) + if (! isset($response[0]) || !is_array($response[0])) { $this->response['response'][0] = array( 'code' => $response['status'], - 'text' => $this->getResponseText($response['status'] ), + 'text' => $this->getResponseText($response['status']), 'error' => (isset($response['error']) ? $response['error'] : '') ); $this->response['data'][0] = $response['data']; @@ -330,25 +362,25 @@ private function setResponse($response ) else { // loop through each response - foreach($response as $num => $data ) + foreach($response as $num => $data) { $this->response['response'][$num] = array( 'code' => $data['status'], - 'text' => $this->getResponseText($data['status'] ), + 'text' => $this->getResponseText($data['status']), 'error' => (isset($data['error']) ? $data['error'] : '') ); $tmp = array(); $id = 0; // is there an array returned - if (is_array($data['data'] )) + if (is_array($data['data'])) { - foreach($data['data'] as $n => $v ) + foreach($data['data'] as $n => $v) { - if (is_array($v )) + if (is_array($v)) { - foreach($v as $key => $val ) + foreach($v as $key => $val) { $tmp[$n][$key] = $val; } @@ -391,8 +423,16 @@ public function getResponse($call_num = 0) */ private function getResponseText(&$code) { - // Load response messages array - $response_messages = include 'messages/response.php'; + // Check internal cache or get array from file + if (empty($this->response_messages)) + { + // Load response messages array + $response_messages = include 'messages/response.php'; + } + else + { + $response_messages = $this->response_messages; + } // select a response code to display if (array_key_exists($code, $response_messages)) @@ -414,8 +454,17 @@ private function getResponseText(&$code) */ private function internal_errors($errno = 0) { - // internal error messages - $internal_error_messages = include 'messages/internal_error.php'; + // Check internal cache or get array from file + if (empty($this->internal_errors)) + { + $internal_error_messages = include 'messages/internal_error.php'; + } + else + { + $internal_error_messages = $this->internal_errors; + } + + // internal error message if (array_key_exists($errno, $internal_error_messages)) { $message = $internal_error_messages[$errno]; @@ -429,22 +478,22 @@ private function internal_errors($errno = 0) } /** - * create the api call + * Create the api call * @return [type] [description] */ private function api() { - if ($this->_callback == null ) + if ($this->_callback == null) { // method to call after successful api request - $this->setCallback( 'getResponse' ); + $this->setCallback( 'getResponse'); } // session already established, use token if ($this->getSession()) { // remove the developer key from the request, since it's not necessary - unset($this->request['key'] ); + unset($this->request['key']); // set the token for this request, since the user is already authenticated $this->request['token'] = $_SESSION['token']; } @@ -453,7 +502,7 @@ private function api() // session has not been established, use developer key to access API try { - if (isset($this->_key )) + if (isset($this->_key)) { // developer key is set $this->request['key'] = $this->_key; @@ -461,10 +510,10 @@ private function api() else { // developer key is not set - throw new Exception($this->internal_errors( 5 )); + throw new Exception($this->internal_errors(5)); } } - catch( Exception $e ) + catch( Exception $e) { echo $e->getMessage(); } @@ -474,9 +523,9 @@ private function api() } /** - * Get the mime type - * @param [type] $extension [description] - * @return [type] [description] + * Get the mime type by file extension + * @param [type] $extension File extension without leading dot + * @return string Mime Type */ private function getFileMimeType($extension) { @@ -485,16 +534,14 @@ private function getFileMimeType($extension) $mimes = include 'config/mimes.php'; if ($mimes[$extension]) { - // mime found return $mimes[$extension]; } else - { - // mime not found - throw new Exception( 'Mime for .' . $extension . ' not found' ); + { + throw new Exception('Mime for .' . $extension . ' not found'); } } - catch( Exception $e ) + catch( Exception $e) { echo $e->getMessage(); } @@ -505,25 +552,25 @@ private function getFileMimeType($extension) * @param [type] $file [description] * @return [type] [description] */ - private function getFileData($file ) + private function getFileData($file) { try { - if (file_exists($file )) + if (file_exists($file)) { // file - $file_data['filedata'] = file_get_contents($file ); - $file_data['filelength'] = strlen($file_data['filedata'] ); - if (function_exists( 'mime_content_type' )) + $file_data['filedata'] = file_get_contents($file); + $file_data['filelength'] = strlen($file_data['filedata']); + if (function_exists( 'mime_content_type')) { // mime_content_type function is available - $file_data['mimetype'] = mime_content_type($file ); + $file_data['mimetype'] = mime_content_type($file); } else { - $parts = explode( '.', $file ); - $extension = strtolower($parts[ sizeOf($parts ) - 1 ] ); - $file_data['mimetype'] = $this->getFileMimeType($extension ); + $parts = explode( '.', $file); + $extension = strtolower($parts[ sizeOf($parts) - 1 ]); + $file_data['mimetype'] = $this->getFileMimeType($extension); } return array( @@ -534,40 +581,40 @@ private function getFileData($file ) } else { - throw new Exception($this->internal_errors( 8 )); + throw new Exception($this->internal_errors(8)); } } - catch( Exception $e ) + catch( Exception $e) { echo $e->getMessage(); exit; } } /** - * perform the api request + * Perform the api request * @return [type] [description] */ private function perform_request() { try { - $ch = curl_init( self::API_ENDPOINT ); + $ch = curl_init(self::API_ENDPOINT); $filedata = ''; - if (is_array($this->requests )) + if (is_array($this->requests)) { - foreach($this->requests as $key => $request ) + foreach($this->requests as $key => $request) { if (isset($request['filedata'])) { $filedata = $request['filedata']; - unset($this->requests[$key]['filedata'] ); + unset($this->requests[$key]['filedata']); } } } $post = $filedata ? array( 'data'=> json_encode($this->getRequest()), - 'filedata' => $filedata ) : array( 'data' => json_encode($this->getRequest()) ); + 'filedata' => $filedata) : array( 'data' => json_encode($this->getRequest())); curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); @@ -581,31 +628,31 @@ private function perform_request() curl_close($ch); // response from API was a successful - if ($http_response_code == 200 ) + if ($http_response_code == 200) { - $temp = json_decode($response, TRUE ); + $temp = json_decode($response, TRUE); // decode the response and store each call response in its own array - $this->setResponse($temp ); + $this->setResponse($temp); // initialize a session - if ($this->_init == 1 ) + if ($this->_init == 1) { $this->setSession(); } // raw response call $this->raw_response = $temp; - if ($this->_debug == TRUE ) + if ($this->_debug == TRUE) { // debug mode is on $request = json_encode($this->getRequest()); $db = fopen('log.txt', 'a'); $tmp_vals = array(); - if (is_array($this->response['data'][0] )) + if (is_array($this->response['data'][0])) { - foreach($this->response['data'] as $n => $v ) + foreach($this->response['data'] as $n => $v) { - foreach($v as $key => $val ) + foreach($v as $key => $val) { $tmp_vals[$n][$key] = $val; } @@ -613,7 +660,7 @@ private function perform_request() } else { - foreach($this->response['data'] as $n => $v ) + foreach($this->response['data'] as $n => $v) { $tmp_vals[$n] = $v; } @@ -621,19 +668,20 @@ private function perform_request() fwrite($db, date('m-d-Y h:i a'). "\n" . 'REQUEST: ' . $request . "\n" . 'RESPONSE STATUS: (' . $this->response['response'][0]['code'] . ') ' . $this->response['response'][0]['text'] . " -- " . $this->response['response'][0]['error'] . "\n" - . 'RESPONSE DATA: ' . json_encode($tmp_vals ) . "\n\n" ); - fclose($db ); + . 'RESPONSE DATA: ' . json_encode($tmp_vals) . "\n\n"); + fclose($db); } + // perform the callback method return $this->{ $this->_callback }(); } else { // response from API was unsuccessful - throw new Exception($this->internal_errors( 2 )); + throw new Exception($this->internal_errors(2)); } } - catch( Exception $e ) + catch( Exception $e) { echo $e->getMessage(); } @@ -668,6 +716,7 @@ public function doLogout() $this->destroySession(); } + /** * Message Methods */ @@ -703,7 +752,7 @@ public function getMessageDetails($id) } /** - * create a new message + * Create a new message * @param array $message [description] * @return [type] [description] */ @@ -721,7 +770,7 @@ public function createMessage($message = array()) } /** - * delete a message + * Delete a message * @param [type] $id [description] * @return [type] [description] */ @@ -751,7 +800,7 @@ public function getWallMessages() } /** - * create a wall message + * Create a wall message * @param array $message [description] * @return [type] [description] */ @@ -769,7 +818,7 @@ public function createWallMessage($message = array()) } /** - * delete a wall message + * Delete a wall message * @param [type] $id [description] * @param array $details [description] * @return [type] [description] @@ -825,7 +874,7 @@ public function getEmployeeDetails($id) } /** - * update an employee record + * Update an employee record * @param [type] $id [description] * @param array $new_data [description] * @return [type] [description] @@ -845,7 +894,7 @@ public function updateEmployee($id, $new_data = array()) } /** - * create a new employee record + * Create a new employee record * @param [type] $data [description] * @return [type] [description] */ @@ -863,7 +912,7 @@ public function createEmployee($data) } /** - * delete an employee + * Delete an employee * @param [type] $id [description] * @return [type] [description] */ @@ -909,7 +958,7 @@ public function getStaffSkillDetails($id) } /** - * create staff skill + * Create staff skill * @param array $skill_details [description] * @return [type] [description] */ @@ -927,7 +976,7 @@ public function createStaffSkill($skill_details = array()) } /** - * update staff skill + * Update staff skill * @param [type] $id [description] * @param array $skill_details [description] * @return [type] [description] @@ -947,7 +996,7 @@ public function updateStaffSkill($id, $skill_details = array()) } /** - * delete staff skill + * Delete staff skill * @param [type] $id [description] * @return [type] [description] */ @@ -963,7 +1012,7 @@ public function deleteStaffSkill($id) } /** - * create a ping + * Create a ping * @param array $ping_data [description] * @return [type] [description] */ @@ -1033,7 +1082,7 @@ public function createSchedule($schedule_details = array()) } /** - * update an existing schedule + * Update an existing schedule * @param [type] $id [description] * @param array $schedule_details [description] * @return [type] [description] @@ -1053,7 +1102,7 @@ public function updateSchedule($id, $schedule_details = array()) } /** - * delete an existing schedule + * Delete an existing schedule * @param [type] $id [description] * @return [type] [description] */ @@ -1099,7 +1148,7 @@ public function getShiftDetails($id) } /** - * update shift details + * Update shift details * @param [type] $id [description] * @param array $shift_details [description] * @return [type] [description] @@ -1119,7 +1168,7 @@ public function updateShift($id, $shift_details = array()) } /** - * create a new shift + * Create a new shift * @param array $shift_details [description] * @return [type] [description] */ @@ -1137,7 +1186,7 @@ public function createShift($shift_details = array()) } /** - * delete a shift + * Delete a shift * @param [type] $id [description] * @return [type] [description] */ @@ -1187,7 +1236,7 @@ public function getVacationScheduleDetails($id) } /** - * create a vacation schedule + * Create a vacation schedule * @param array $vacation_details [description] * @return [type] [description] */ @@ -1205,7 +1254,7 @@ public function createVacationSchedule($vacation_details = array()) } /** - * update a vacation schedule + * Update a vacation schedule * @param [type] $id [description] * @param array $vacation_details [description] * @return [type] [description] @@ -1225,7 +1274,7 @@ public function updateVacationSchedule($id, $vacation_details = array()) } /** - * delete a vacation schedule + * Delete a vacation schedule * @param [type] $id [description] * @return [type] [description] */ @@ -1326,7 +1375,7 @@ public function getAdminFileDetails($id) } /** - * update admin file details + * Update admin file details * @param [type] $id [description] * @param array $details [description] * @return [type] [description] @@ -1346,13 +1395,13 @@ public function updateAdminFile($id, $details = array()) } /** - * create new admin file + * Create new admin file * @param array $file_details [description] * @return [type] [description] */ public function createAdminFile($file_details = array()) { - $file_details = array_merge($file_details, $this->getFileData($file_details['filename'] )); + $file_details = array_merge($file_details, $this->getFileData($file_details['filename'])); return $this->setRequest( array_merge( array( @@ -1365,7 +1414,7 @@ public function createAdminFile($file_details = array()) } /** - * delete admin file + * Delete admin file * @param [type] $id [description] * @return [type] [description] */ @@ -1411,13 +1460,13 @@ public function getAdminBackupDetails($id) } /** - * create an admin backup + * Create an admin backup * @param array $backup_details [description] * @return [type] [description] */ - public function createAdminBackup($backup_details = array() ) + public function createAdminBackup($backup_details = array() ) { - $backup_details = array_merge($backup_details, $this->getFileData($backup_details['filename'] )); + $backup_details = array_merge($backup_details, $this->getFileData($backup_details['filename'])); return $this->setRequest( array_merge( array( @@ -1430,7 +1479,7 @@ public function createAdminBackup($backup_details = array() ) } /** - * delete an admin backup + * Delete an admin backup * @param [type] $id [description] * @return [type] [description] */ From 45fdc663f3eddaeedebffcaebe030aa99527377c Mon Sep 17 00:00:00 2001 From: Joel Pittet Date: Mon, 26 Nov 2012 16:12:33 -0800 Subject: [PATCH 5/5] more cleanup --- examples/examples.php | 98 ++++++++++++++++++++++--------------------- src/shiftplanning.php | 58 ++++++++++++++----------- 2 files changed, 83 insertions(+), 73 deletions(-) diff --git a/examples/examples.php b/examples/examples.php index 54a1384..1e3b97f 100644 --- a/examples/examples.php +++ b/examples/examples.php @@ -11,7 +11,7 @@ ); // or set the developer key using the setAppKey method -//$shiftplanning->setAppKey( 'XXXXXXXXXXXXXXXXXX' ); +//$shiftplanning->setAppKey('XXXXXXXXXXXXXXXXXX'); /** * @@ -27,8 +27,9 @@ echo "appKey: " . $shiftplanning->getAppKey( ) . "
"; // returns the developer key currently set echo "appToken: " . $shiftplanning->getAppToken( ) . "
"; // returns the token for the current session, error if not yet set -if ( !$session ) -{// if a session hasn't been started, create one +if (!$session) +{ + // if a session hasn't been started, create one // perform a single API call to authenticate a user $response = $shiftplanning->doLogin( @@ -38,7 +39,7 @@ ) ); - if ( $response['status']['code'] == 1 ) + if ($response['status']['code'] == 1) {// check to make sure that login was successful $session = $shiftplanning->getSession( ); // return the session data after successful login echo "Hi, " . $session['employee']['name'] . "
"; @@ -49,60 +50,61 @@ } } else -{// session has been established +{ + // session has been established // the $session variable now holds the currently logged in user's data echo "Hi, " . $session['employee']['name'] . "
"; /** * Quick Access ShiftPlanning SDK Methods: - * doLogin( $array_of_user_data ) + * doLogin($array_of_user_data) * doLogout() * getMessages() - * getMessageDetails( $message_id ) - * createMessage( $array_of_message_data ) - * deleteMessage( $message_id ) + * getMessageDetails($message_id) + * createMessage($array_of_message_data) + * deleteMessage($message_id) * getWallMessages() - * createWallMessage( $array_of_message_data ) - * deleteWallMessage( $message_id, $array_of_other_message_data ) + * createWallMessage($array_of_message_data) + * deleteWallMessage($message_id, $array_of_other_message_data) * getEmployees() - * getEmployeeDetails( $employee_id_number ) - * updateEmployee( $employee_id, $array_of_updated_employee_data ) - * createEmployee( $array_of_employee_data ) - * deleteEmployee( $employee_id ) + * getEmployeeDetails($employee_id_number) + * updateEmployee($employee_id, $array_of_updated_employee_data) + * createEmployee($array_of_employee_data) + * deleteEmployee($employee_id) * getStaffSkills() - * getStaffSkillDetails( $skill_id ) - * createStaffSkill( $array_of_skill_data ) - * updateStaffSkill( $skill_id, $array_of_skill_data ) - * deleteStaffSkill( $skill_id ) - * createPing( $array_of_ping_data ) + * getStaffSkillDetails($skill_id) + * createStaffSkill($array_of_skill_data) + * updateStaffSkill($skill_id, $array_of_skill_data) + * deleteStaffSkill($skill_id) + * createPing($array_of_ping_data) * getSchedules() - * getScheduleDetails( $schedule_id ) - * createSchedule( $array_of_schedule_data ) - * updateSchedule( $schedule_id, $array_of_schedule_data ) - * deleteSchedule( $schedule_id ) + * getScheduleDetails($schedule_id) + * createSchedule($array_of_schedule_data) + * updateSchedule($schedule_id, $array_of_schedule_data) + * deleteSchedule($schedule_id) * getShifts() - * getShiftDetails( $shift_id ) - * updateShift( $shift_id, $array_of_shift_data ) - * createShift( $array_of_shift_data ) - * deleteShift( $shift_id ) - * getVacationSchedules( $time_period_array ) // e.g. getVacationSchedules( array( 'start' => '', 'end' => '' ) ); - * getVacationScheduleDetails( $schedule_id ) - * createVacationSchedule( $array_of_schedule_data ) - * updateVacationSchedule( $schedule_id, $array_of_schedule_data ) - * deleteVacationSchedule( $schedule_id ) + * getShiftDetails($shift_id) + * updateShift($shift_id, $array_of_shift_data) + * createShift($array_of_shift_data) + * deleteShift($shift_id) + * getVacationSchedules($time_period_array) // e.g. getVacationSchedules(array('start' => '', 'end' => '')); + * getVacationScheduleDetails($schedule_id) + * createVacationSchedule($array_of_schedule_data) + * updateVacationSchedule($schedule_id, $array_of_schedule_data) + * deleteVacationSchedule($schedule_id) * getScheduleConflicts() * getAdminSettings() - * updateAdminSettings( $array_of_new_settings ) + * updateAdminSettings($array_of_new_settings) * getAdminFiles() - * getAdminFileDetails( $file_id ) - * updateAdminFile( $file_id, $array_of_file_data ) - * createAdminFile( $array_of_file_data ) - * deleteAdminFile( $file_id ) + * getAdminFileDetails($file_id) + * updateAdminFile($file_id, $array_of_file_data) + * createAdminFile($array_of_file_data) + * deleteAdminFile($file_id) * getAdminBackups() - * getAdminBackupDetails( $backup_id ) - * createAdminBackup( $array_of_backup_data ) - * deleteAdminBackup( $backup_id ) + * getAdminBackupDetails($backup_id) + * createAdminBackup($array_of_backup_data) + * deleteAdminBackup($backup_id) * getAPIConfig() * getAPIMethods() */ @@ -110,7 +112,7 @@ /** * All Quick-Access methods return a response like this: * array( - * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any' ), + * 'status' => array('code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), * 'data' => array( * 'field_name' => 'value' * ) @@ -121,7 +123,7 @@ * message you're looking to display * * array( - * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any' ), + * 'status' => array('code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), * 'data' => array( * [0] => array ( * 'id' => 1, @@ -149,7 +151,7 @@ ) ); - if ( $update_employee_record_response['status']['code'] == 1 ) + if ($update_employee_record_response['status']['code'] == 1) {// employee update successful echo "Employee record updated.
"; } @@ -217,15 +219,15 @@ } // schedules were retrieved successfully - if ( $get_schedules['status']['code'] == 1 ) + if ($get_schedules['status']['code'] == 1) { - print_r( $get_schedules['data'][0] ); // print data from first schedule returned + print_r($get_schedules['data'][0]); // print data from first schedule returned } // admin settings retrieved successfully - if ( $get_settings['status']['code'] == 1 ) + if ($get_settings['status']['code'] == 1) { - print_r( $get_settings ); + print_r($get_settings); } $shiftplanning->doLogout(); diff --git a/src/shiftplanning.php b/src/shiftplanning.php index 0916be9..d0abd98 100644 --- a/src/shiftplanning.php +++ b/src/shiftplanning.php @@ -38,7 +38,7 @@ * updateShift($shift_id, $array_of_shift_data) * createShift($array_of_shift_data) * deleteShift($shift_id) - * getVacationSchedules($time_period_array) // e.g. getVacationSchedules( array( 'start' => '', 'end' => '')); + * getVacationSchedules($time_period_array) // e.g. getVacationSchedules(array('start' => '', 'end' => '')); * getVacationScheduleDetails($schedule_id) * createVacationSchedule($array_of_schedule_data) * updateVacationSchedule($schedule_id, $array_of_schedule_data) @@ -62,7 +62,7 @@ /** * All Quick-Access methods return a response like this: * array( - * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), + * 'status' => array('code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), * 'data' => array( * 'field_name' => 'value' * ) @@ -73,7 +73,7 @@ * message you're looking to display * * array( - * 'status' => array( 'code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), + * 'status' => array('code' => '1', 'text' => 'Success', 'error' => 'Error message if any'), * 'data' => array( * [0] => array ( * 'id' => 1, @@ -89,10 +89,20 @@ class Shiftplanning { + + /** + * @var string AppKey + */ private $_key; + /** + * @var string Callback Method + */ private $_callback; + /** + * @var integer session initalization + */ private $_init; /** @@ -149,19 +159,19 @@ public function __construct($config = array()) // set the developer key $this->setAppKey($config['key']); - if (!function_exists( 'curl_init')) + if (!function_exists('curl_init')) { // curl is not available throw new Exception($this->internal_errors(6)); } - if (!function_exists( 'json_decode')) + if (!function_exists('json_decode')) { // json_decode is not available throw new Exception($this->internal_errors(7)); } } - catch( Exception $e) + catch(Exception $e) { echo $e->getMessage(); exit; } @@ -176,7 +186,7 @@ public function setDebug($switch = FALSE) // delete previous log file if (file_exists('log.txt')) { - unlink( 'log.txt'); + unlink('log.txt'); } $this->_debug = TRUE; } @@ -184,7 +194,7 @@ public function setDebug($switch = FALSE) // start the session protected function startSession() { - session_name( self::SESSION_IDENTIFIER); + session_name(self::SESSION_IDENTIFIER); session_start(); } @@ -198,7 +208,7 @@ private function setSession() // destroy the currently active session private function destroySession() { - $logout = $this->setRequest( array ( + $logout = $this->setRequest(array( 'module' => 'staff.logout', 'method' => 'GET' )); @@ -290,7 +300,7 @@ public function getAppToken() throw new Exception($this->internal_errors(4)); } } - catch( Exception $e) + catch(Exception $e) { echo $e->getMessage(); } @@ -337,7 +347,7 @@ public function setRequest($requests = array()) */ public function getRequest() { - return array_merge($this->request, array( 'request' => $this->requests)); + return array_merge($this->request, array('request' => $this->requests)); } /** @@ -486,7 +496,7 @@ private function api() if ($this->_callback == null) { // method to call after successful api request - $this->setCallback( 'getResponse'); + $this->setCallback('getResponse'); } // session already established, use token @@ -513,7 +523,7 @@ private function api() throw new Exception($this->internal_errors(5)); } } - catch( Exception $e) + catch(Exception $e) { echo $e->getMessage(); } @@ -541,7 +551,7 @@ private function getFileMimeType($extension) throw new Exception('Mime for .' . $extension . ' not found'); } } - catch( Exception $e) + catch(Exception $e) { echo $e->getMessage(); } @@ -561,14 +571,14 @@ private function getFileData($file) // file $file_data['filedata'] = file_get_contents($file); $file_data['filelength'] = strlen($file_data['filedata']); - if (function_exists( 'mime_content_type')) + if (function_exists('mime_content_type')) { // mime_content_type function is available $file_data['mimetype'] = mime_content_type($file); } else { - $parts = explode( '.', $file); + $parts = explode('.', $file); $extension = strtolower($parts[ sizeOf($parts) - 1 ]); $file_data['mimetype'] = $this->getFileMimeType($extension); } @@ -584,7 +594,7 @@ private function getFileData($file) throw new Exception($this->internal_errors(8)); } } - catch( Exception $e) + catch(Exception $e) { echo $e->getMessage(); exit; } @@ -613,8 +623,8 @@ private function perform_request() } } - $post = $filedata ? array( 'data'=> json_encode($this->getRequest()), - 'filedata' => $filedata) : array( 'data' => json_encode($this->getRequest())); + $post = $filedata ? array('data'=> json_encode($this->getRequest()), + 'filedata' => $filedata) : array('data' => json_encode($this->getRequest())); curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); @@ -681,7 +691,7 @@ private function perform_request() throw new Exception($this->internal_errors(2)); } } - catch( Exception $e) + catch(Exception $e) { echo $e->getMessage(); } @@ -1307,9 +1317,8 @@ public function getScheduleConflicts($time_period = array()) ); } - /* + /** * Administration Methods - * */ /** @@ -1464,7 +1473,7 @@ public function getAdminBackupDetails($id) * @param array $backup_details [description] * @return [type] [description] */ - public function createAdminBackup($backup_details = array() ) + public function createAdminBackup($backup_details = array()) { $backup_details = array_merge($backup_details, $this->getFileData($backup_details['filename'])); return $this->setRequest( @@ -1494,9 +1503,8 @@ public function deleteAdminBackup($id) ); } - /* + /** * API Methods - * */ /**