Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once($modx->getOption('core_path').'components/simplx/mygit/simplx.mygit.php');
- $action = isset($action) ? $action : '';
- $username = isset($username) ? $username : '';
- $password = isset($password) ? $password : '';
- $object = isset($object) ? $object : '';
- $objectid = isset($objectid) ? $objectid : '';
- switch($action){
- case 'mygit.getgist':
- if($objectid != ''){
- $mygit = new simplx_mygit(new GitAPIClient());
- $mygit->login($username,$password);
- $gist = $mygit->getGist($objectid);
- if($gist){
- return json_encode($gist);
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.getgist() Exception: gist not found.');
- }
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.getgist() Exception: parameter "$objectid" was not sent.');
- }
- break;
- case 'mygit.addgist':
- if($object != ''){
- $object = json_decode($object,true);
- if(!is_array($object)){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.addgist() Exception: parameter "$object" is not a valid php array()');
- return false;
- }
- $mygit = new simplx_mygit(new GitAPIClient());
- $mygit->login($username,$password);
- /*
- When creating a new instance of "simplx_gist", you dont yet add anything to
- Github. This is done by calling the "saveGist" method of the "simplx.mygit" class.
- */
- $new_gist = new simplx_gist($object);
- /*
- You add a file to the gist by either,
- - Create a new "simplx_gist_file" object
- - Create an array to represent the file object
- Now use the "addFile" method of the gist object to actually add the file.
- */
- $mygit->addGist($new_gist);
- return json_encode($mygit->getGist($new_gist->id));
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.addgist() Exception: parameter "$object" was not sent.');
- }
- break;
- case 'mygit.savegist':
- if($object != ''){
- $object = json_decode($object,true);
- if(!is_array($object)){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.savegist() Exception: parameter "$object" is not a valid php array()');
- return false;
- }
- $mygit = new simplx_mygit(new GitAPIClient());
- $mygit->login($username,$password);
- $gist = new simplx_gist($object);
- $mygit->saveGist($gist);
- /*
- Alternative method:
- $new_gist_file = new simplx_gist_file(array('filename'=>'test.txt','description'=>'My new gist file','content'=>'blabla'));
- $new_gist->addFile(array('filename'=>'test.txt','description'=>'My new gist file','content'=>'blabla'));
- */
- return json_encode($gist);
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.savegist() Exception: parameter "$object" was not sent.');
- }
- break;
- case 'mygit.gist.addfile':
- if($object != ''){
- if($objectid == ''){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$objectid" was not passed.');
- return false;
- }
- $object = json_decode($object,true);
- if(!is_array($object)){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" is not a valid php array()');
- return false;
- }
- $mygit = new simplx_mygit(new GitAPIClient());
- $mygit->login($username,$password);
- $gist = $mygit->getGist($objectid);
- if($gist){
- $gist->addFile($object);
- $mygit->saveGist($gist);
- return json_encode($mygit->getGist($gist->id));
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: "$mygit->getGist()" did not return a valid object.');
- }
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" was not sent.');
- }
- break;
- case 'mygit.gist.savefile':
- if($object != ''){
- if($objectid == ''){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: parameter "$objectid" was not passed.');
- return false;
- }
- $object = json_decode($object,true);
- if(!is_array($object)){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: parameter "$object" is not a valid php array()');
- return false;
- }
- $mygit = new simplx_mygit(new GitAPIClient());
- $mygit->login($username,$password);
- $gist = $mygit->getGist($objectid);
- if($gist){
- $gist->files[$object['filename']] = $object;
- $mygit->saveGist($gist);
- return json_encode($gist);
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: "$mygit->getGist()" did not return a valid object.');
- }
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: parameter "$object" was not sent.');
- }
- break;
- case 'mygit.gist.addfile':
- if($object != ''){
- if($objectid == ''){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$objectid" was not passed.');
- return false;
- }
- $object = json_decode($object,true);
- if(!is_array($object)){
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" is not a valid php array()');
- return false;
- }
- $mygit = new simplx_mygit(new GitAPIClient());
- $mygit->login($username,$password);
- $gist = $mygit->getGist($objectid);
- if($gist){
- $gist->addFile($object);
- $mygit->saveGist($gist);
- return json_encode($gist);
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: "$mygit->getGist()" did not return a valid object.');
- }
- }else{
- $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" was not sent.');
- }
- break;
- default:
- }
Add Comment
Please, Sign In to add comment