Created
June 25, 2015 20:41
-
-
Save rjcorwin/a085222d5970c42d8be7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @file | |
| * Code for the recipe feature. | |
| */ | |
| include_once 'recipe.features.inc'; | |
| function recipe_menu() { | |
| $items = array(); | |
| $items['api/recipe'] = array( | |
| 'page callback' => 'recipe_recipe_api', | |
| 'type' => MENU_CALLBACK, | |
| 'access callback' => TRUE | |
| ); | |
| $items['api/recipe/%'] = array( | |
| 'page callback' => 'recipe_recipe_api', | |
| 'type' => MENU_CALLBACK, | |
| 'page arguments' => array(2), | |
| 'access callback' => TRUE | |
| ); | |
| $items['api/query/recipes'] = array( | |
| 'page callback' => 'recipe_api_query_recipes', | |
| 'type' => MENU_CALLBACK, | |
| 'access callback' => TRUE | |
| ); | |
| $items['api/whoami'] = array( | |
| 'page callback' => 'recipe_api_whoami', | |
| 'type' => MENU_CALLBACK, | |
| 'access callback' => TRUE | |
| ); | |
| $items['api/user/token/request'] = array( | |
| 'page callback' => 'recipe_api_user_token_request', | |
| 'type' => MENU_CALLBACK, | |
| 'access callback' => TRUE | |
| ); | |
| $items['api/user/token/use'] = array( | |
| 'page callback' => 'recipe_api_user_token_use', | |
| 'type' => MENU_CALLBACK, | |
| 'access callback' => TRUE | |
| ); | |
| $items['api/user/account/%'] = array( | |
| 'page callback' => 'recipe_api_user_account', | |
| 'type' => MENU_CALLBACK, | |
| 'page arguments' => array(3), | |
| 'access callback' => TRUE | |
| ); | |
| return $items; | |
| } | |
| function recipe_api_user_account($uid) { | |
| $data = json_decode(file_get_contents("php://input"), true); | |
| $account = user_load($uid); | |
| if ($account == FALSE) { | |
| return drupal_json_output(array('User not found.')); | |
| } | |
| switch($_SERVER['REQUEST_METHOD']) { | |
| case 'GET': | |
| $account->pass = ''; | |
| drupal_json_output($account); | |
| break; | |
| case 'POST': | |
| break; | |
| case 'PUT': | |
| $pass = $account->pass; | |
| foreach ($data as $key => $value) { | |
| $account->{$key} = $value; | |
| } | |
| // Don't overrite pass ever | |
| $account->pass = $pass; | |
| user_save($account); | |
| $account->pass = ''; | |
| drupal_json_output($account); | |
| break; | |
| case 'DELETE': | |
| user_delete($uid); | |
| break; | |
| } | |
| } | |
| function recipe_api_whoami() { | |
| global $user; | |
| drupal_json_output($user); | |
| } | |
| function recipe_api_user_token_request() { | |
| //simple_mail_send('rj@rjsteinert.com', 'rj@rjsteinert.com', 'hi', 'test'); | |
| $data = json_decode(file_get_contents("php://input"), true); | |
| $account = user_load_by_mail($data['mail']); | |
| if ($account == FALSE) { | |
| //This will generate a random password, you could set your own here | |
| $password = user_password(8); | |
| //set up the user fields | |
| $fields = array( | |
| 'name' => drupal_random_key(), | |
| 'mail' => $data['mail'], | |
| 'pass' => $password, | |
| 'status' => 1, | |
| 'init' => 'email address', | |
| 'roles' => array( | |
| DRUPAL_AUTHENTICATED_RID => 'authenticated user', | |
| ), | |
| ); | |
| //the first parameter is left blank so a new user is created | |
| $account = user_save('', $fields); | |
| } | |
| $token = drupal_random_key(); | |
| variable_set('user_token_' . $account->uid, $token); | |
| simple_mail_send('rj@rjsteinert.com', $account->mail, 'Robot Recipes log in link', 'http://' . $_SERVER['HTTP_HOST'] . '/api/user/token/use?&token=' . $token . '&mail=' . $account->mail); | |
| drupal_json_output(array('status'=>'ok')); | |
| } | |
| function recipe_api_user_token_use() { | |
| global $user; | |
| $account = user_load_by_mail($_GET['mail']); | |
| $current_token = variable_get('user_token_' . $account->uid); | |
| if($current_token == $_GET['token']) { | |
| variable_del('user_token_' . $account->uid); | |
| // http://drupal.stackexchange.com/questions/33785/user-login-using-a-token-or-id-no-password | |
| $user = $account; | |
| $login_array = array ('name' => $user->name); | |
| user_login_finalize($login_array); | |
| } | |
| // @todo use a redirect param | |
| header('Location: http://openpipekit.github.io/Recipe-App/'); | |
| } | |
| function recipe_recipe_api($nid) { | |
| switch($_SERVER['REQUEST_METHOD']) { | |
| case 'GET': | |
| $output = _recipe_get_recipe_output($nid); | |
| drupal_json_output($output); | |
| break; | |
| case 'PUT': | |
| global $user; | |
| $data = json_decode(file_get_contents("php://input"), true); | |
| $recipe = new EntityDrupalWrapper('node', $nid); | |
| $recipe->field_code->set($data['field_code']); | |
| $recipe->field_readme->set($data['field_readme']); | |
| $recipe->field_statement->set($data['field_statement']); | |
| $terms = array(); | |
| foreach($data['field_tags'] as $tag_name) { | |
| $results = taxonomy_get_term_by_name($tag_name, 'tags'); | |
| if(count($results) == 0) { | |
| $term = new stdClass(); | |
| $term->vid = 1; | |
| $term->name = $tag_name; | |
| $term->vocabulary_machine_name = 'tags'; | |
| taxonomy_term_save($term); | |
| $terms[] = array("tid"=>$term->tid); | |
| } | |
| else { | |
| $terms[] = array("tid"=>array_pop($results)->tid); | |
| } | |
| } | |
| $recipe->field_tags->set($terms); | |
| $recipe->save(); | |
| $output = _recipe_get_recipe_output($recipe->nid->value()); | |
| drupal_json_output($output); | |
| break; | |
| case 'POST': | |
| $data = json_decode(file_get_contents("php://input"), true); | |
| // Entity API Example: http://eosrei.net/articles/2012/11/programmatically-creating-nodes-using-entity-wrapper | |
| global $user; | |
| $entity = entity_create('node', array('type' => 'recipe')); | |
| $entity->uid = $user->uid; | |
| $recipe = entity_metadata_wrapper('node',$entity); | |
| $recipe->title->set('New recipe'); | |
| $recipe->field_code->set($data['field_code']); | |
| $recipe->field_readme->set($data['field_readme']); | |
| $recipe->field_statement->set($data['field_statement']); | |
| $terms = array(); | |
| foreach($data['field_tags'] as $tag_name) { | |
| $results = taxonomy_get_term_by_name($tag_name, 'tags'); | |
| if(count($results) == 0) { | |
| $term = new stdClass(); | |
| $term->vid = 1; | |
| $term->name = $tag_name; | |
| $term->vocabulary_machine_name = 'tags'; | |
| taxonomy_term_save($term); | |
| $terms[] = array("tid"=>$term->tid); | |
| } | |
| else { | |
| $terms[] = array("tid"=>$results[1]->tid); | |
| } | |
| } | |
| $recipe->field_tags = $terms; | |
| $recipe->save(); | |
| $entity = $recipe->value(); | |
| $recipe->title->set('Recipe #' . $entity->nid . ' by ' . $user->name); | |
| $recipe->save(); | |
| $output = _recipe_get_recipe_output($entity->nid); | |
| drupal_json_output($output); | |
| break; | |
| case 'DELETE': | |
| node_delete($nid); | |
| drupal_json_output('ok'); | |
| break; | |
| } | |
| } | |
| function recipe_api_query_recipes() { | |
| $results = db_query('select nid from node where type=:type', array('type'=>'recipe')); | |
| $output = array(); | |
| foreach($results as $result) { | |
| $output[] = _recipe_get_recipe_output($result->nid); | |
| } | |
| drupal_json_output($output); | |
| } | |
| function _recipe_get_recipe_output($nid) { | |
| $node = node_load($nid); | |
| $user = user_load($node->uid); | |
| $output = new stdClass(); | |
| $output->nid = $nid; | |
| $output->author = $user->name; | |
| $output->title = $node->title; | |
| $output->field_code = $node->field_code['und'][0]['value']; | |
| $output->field_readme = $node->field_readme['und'][0]['value']; | |
| $output->field_statement = $node->field_statement['und'][0]['value']; | |
| $tags = array(); | |
| foreach($node->field_tags['und'] as $tag) { | |
| $tag = taxonomy_term_load($tag['tid']); | |
| $tags[] = $tag->name; | |
| } | |
| $output->field_tags = $tags; | |
| return $output; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment