angularjsexample-8.x-1.x-dev/angular_js.module
angular_js.module
<?php
/**
* @file
* This module provide angular js example.
*/
/**
* Implements hook_help().
*/
function angular_js_help($path, $arg) {
switch ($path) {
case 'admin/help#angular_js_example':
$output = t("This is simple example of angular js.");
$output .= t("How we can add records in db and can show through angular js");
return $output;
}
}
/**
* Implements hook_permission().
*/
function angular_js_permission() {
return array(
'administer books' => array(
'title' => t('Administer books'),
),
);
}
/**
* Implements hook_block_info().
*/
function angular_js_block_info() {
$blocks['angular_js_form'] = array(
'info' => t('Angular JS Example: Book add'),
);
return $blocks;
}
/**
* Implements hook_menu().
*/
function angular_js_menu() {
$items['json/list'] = array(
'title' => 'Save form data',
'description' => 'Save form data',
'page callback' => 'angular_form_list',
'type' => MENU_CALLBACK,
'access arguments' => array('administer books'),
);
$items['json/save_form'] = array(
'title' => 'Save form data',
'description' => 'Save form data',
'page callback' => 'angular_js_form_data_save',
'delivery callback' => 'drupal_json_output',
'type' => MENU_CALLBACK,
'access arguments' => array('administer books'),
);
$items['json/get_books_json'] = array(
'title' => 'Get books',
'description' => 'Get books data',
'page callback' => 'angular_js_form_data_get',
'delivery callback' => 'drupal_json_output',
'type' => MENU_CALLBACK,
'access arguments' => array('administer books'),
);
return $items;
}
/**
* Function for get data of books for angular js.
*/
function angular_form_list() {
// We are going to output the results in a table with a nice header.
$header = array(
// The header gives the table the information it needs in order to make
// the query calls for ordering. TableSort uses the field information
// to know what database column to sort by.
array('data' => t('tittle'), 'field' => 'b.bid'),
array('data' => t('Letters'), 'field' => 'b.bookname'),
array('data' => t('Mixture'), 'field' => 'b.bookprice'),
array('data' => t('Mixture'), 'field' => 'b.authorid'),
);
$rows = array();
$result = db_select('angular_js_books', 'b')
->fields('b')
->execute()
->fetchAll();
/*foreach ($result as $key => $value) {
$rows[$key]->bid = $value->bid;
$rows[$key]->bookname = check_plain($value->bookname);
$rows[$key]->bookprice = check_plain($value->bookprice);
$rows[$key]->authorid = check_plain($value->authorid);
}
return $rows;*/
$rows = array();
foreach ($result as $row) {
// Normally we would add some nice formatting to our rows
// but for our purpose we are simply going to add our row
// to the array.
$rows[] = array('data' => (array) $row);
}
// Build the table for the nice output.
$build['tablesort_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $build;
}
/**
* Function for get data of books for angular js.
*/
function angular_js_form_data_get() {
$rows = array();
$result = db_select('angular_js_books', 'b')
->fields('b')
->execute()
->fetchAll();
foreach ($result as $key => $value) {
$rows[$key]->bid = $value->bid;
$rows[$key]->bookname = check_plain($value->bookname);
$rows[$key]->bookprice = check_plain($value->bookprice);
$rows[$key]->authorid = check_plain($value->authorid);
}
return $rows;
}
/**
* Function for save data of books for angular js.
*/
function angular_js_form_data_save() {
if (!empty($_POST['tokenid']) && drupal_valid_token($_POST['tokenid'], 'angularjs')) {
db_insert('angular_js_example_books')
->fields(array(
'bookname' => $_POST['bookname'],
'bookprice' => $_POST['bookprice'],
'authorid' => $_POST['authorid'],
))
->execute();
return angular_js_form_data_get();
}
}
/**
* Implements hook_block_view().
*/
/*
function angular_js_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'angular_js_form':
$block['subject'] = t('Book Form');
$block['content'] = array(
'#theme' => 'angular_js_form',
'#attached' => array(
'js' => array(
drupal_get_path('module', 'angular_js') . '/js/angular.min.js',
drupal_get_path('module', 'angular_js') . '/js/angular_js.js',
array(
'data' => array(
'angular_js' => array(
'url_base' => url('/', array('absolute' => TRUE)),
'angularjsexample_csrf_token' => drupal_get_token('angularjs'),
),
),
'type' => 'setting',
),
),
),
);
break;
}
return $block;
}*/
/**
* Implements hook_theme().
*/
function angular_js_theme() {
return array(
'angularjs_form' => array(
'template' => 'angularjs-form',
'variables' => array(),
),
);
}
