5989b17c by Joshua Tundag

add examples

1 parent 48468437
......@@ -113,3 +113,5 @@ flag('mode');
flag('source');
flag('pmt', 'rec');
```
[See example for more snippets.](/examples)
\ No newline at end of file
......
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Api extends MY_Controller{
public function __construct() {
parent::__construct();
$this->authenticate_admin_user(false);
$admin_data = admin_data();
if(!$this->authenticate_administrator_role($admin_data)){
/*** Guest redirect ***/
$this->admin_user_guest($admin_data);
/*** Support redirect ***/
$this->admin_user_support($admin_data);
}
$this->load->library('form_validation');
}
public function get_flags_for_table(){
$tableOptions = $this->input->get();
$flagManager = new \BTFlags\FlagManager();
$table = array();
$tableOptions['sortBy'] = 'DESC';
if($tableOptions['sortOrder'] && strpos($tableOptions['sortOrder'], '+') === 0){
$tableOptions['sortOrder'] = substr($tableOptions['sortOrder'], 1);
$tableOptions['sortBy'] = 'ASC';
}
$tableOptions['filter'] = isset($tableOptions['filter']) ? $tableOptions['filter'] : '';
$tableOptions['mode'] = isset($_COOKIE['mode']) ? $_COOKIE['mode'] : 'live';
$table['data'] = $flagManager->getAllForTable($tableOptions);
$scope = $this;
$table['total'] = $flagManager->getAllCountForTable($tableOptions);
$table['per_page'] = (int)$tableOptions['perPage'];
$table['current_page'] = (int)$tableOptions['page'];
$table['sort'] = $tableOptions['sortOrder'];
$table['last_page'] = ceil($table['total'] / $table['per_page']);
$table['next_page_url'] = 'api/get_permissions_for_table?page=' . ($tableOptions['page'] + 1);
$table['prev_page_url'] = null;
$table['from'] = (((int)$tableOptions['page'] - 1) * $table['per_page']) + 1;
$table['to'] = ((((int)$tableOptions['page']) * $table['per_page']) < $table['total']) ? ((int)$tableOptions['page']) * $table['per_page'] : $table['total'];
$this->output->set_content_type('application/json');
echo json_encode($table);
die;
}
public function create_flag(){
$requestData = json_decode(file_get_contents('php://input'), true);
$this->form_validation->set_data($requestData);
$this->form_validation->set_rules('name', 'Flag Name', 'required');
$this->form_validation->set_rules('encrypted_name', 'Encrypted Name', 'required');
if($this->form_validation->run() == false){
echo json_encode(['success' => false, 'errors' => array_values($this->form_validation->error_array())]);
die;
}
$flagManager = new \BTFlags\FlagManager();
$created = $flagManager->create($requestData);
if(!$created) die(json_encode(['success' => false, 'errors' => $flagManager->errors()]));
$log_data = array(
'date_created' => ph_time(),
'event' => 'Create Flag',
'description' => 'Flag Name: ' . $requestData['name'],
);
$add_log = insert_admin_log($log_data);
echo json_encode(['success' => true, 'message' => 'Flag successfully created!']);
die;
}
/**
* Update a flag
* @return void
*/
public function update_flag(){
$requestData =json_decode(file_get_contents('php://input'), true);
$this->form_validation->set_data($requestData);
$this->form_validation->set_rules('name', 'Flag Name', 'required');
$this->form_validation->set_rules('encrypted_name', 'Encrypted Name', 'required');
if($this->form_validation->run() == false){
echo json_encode(['success' => false, 'errors' => array_values($this->form_validation->error_array())]);
die;
}
$flagManager = new \BTFlags\FlagManager();
$updated = $flagManager->update($requestData);
if(!$updated) die(json_encode(['success' => false, 'errors' => $flagManager->errors()]));
$log_data = array(
'date_created' => ph_time(),
'event' => 'Update Flag',
'description' => 'Flag Name: ' . $requestData['name'],
);
$add_log = insert_admin_log($log_data);
echo json_encode(['success' => true, 'message' => 'Flag successfully updated!']);
die;
}
/**
* Delete a flag
* @return void
*/
public function delete_flag(){
$requestData = json_decode(file_get_contents('php://input'), true);
$flagManager = new \BTFlags\FlagManager();
$deleted = $flagManager->delete($requestData);
if(!$deleted) die(json_encode(['success' => false, 'errors' => $flagManager->errors()]));
$log_data = array(
'date_created' => ph_time(),
'event' => 'Delete Flag',
'description' => 'Flag Name: ' . $requestData['name'],
);
$add_log = insert_admin_log($log_data);
echo json_encode(['success' => true, 'message' => 'Flag successfully deleted!']);
die;
}
}
\ No newline at end of file
<?php
class Flag{
public function register(){
$flagManager = new \LawFormsUSA\Flags\FlagManager();
$flagManager->register();
}
}
\ No newline at end of file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files. Please see the user guide for info:
|
| https://codeigniter.com/user_guide/general/hooks.html
|
*/
$hook['post_controller_constructor'][] = array(
'class' => 'Flag',
'function' => 'register',
'filename' => 'Flag.php',
'filepath' => 'hooks',
);
\ No newline at end of file
<?php
if (!function_exists('flag')){
function flag($flag, $default = null, $encrypted = false){
return \LawFormsUSA\Flags\Flags::get($flag, $encrypted, $default);
}
}
\ No newline at end of file
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!