5989b17c by Joshua Tundag

add examples

1 parent 48468437
...@@ -113,3 +113,5 @@ flag('mode'); ...@@ -113,3 +113,5 @@ flag('mode');
113 flag('source'); 113 flag('source');
114 flag('pmt', 'rec'); 114 flag('pmt', 'rec');
115 ``` 115 ```
116
117 [See example for more snippets.](/examples)
...\ No newline at end of file ...\ No newline at end of file
......
1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Api extends MY_Controller{
4 public function __construct() {
5 parent::__construct();
6 $this->authenticate_admin_user(false);
7 $admin_data = admin_data();
8 if(!$this->authenticate_administrator_role($admin_data)){
9 /*** Guest redirect ***/
10 $this->admin_user_guest($admin_data);
11 /*** Support redirect ***/
12 $this->admin_user_support($admin_data);
13 }
14 $this->load->library('form_validation');
15 }
16
17 public function get_flags_for_table(){
18 $tableOptions = $this->input->get();
19 $flagManager = new \BTFlags\FlagManager();
20
21 $table = array();
22 $tableOptions['sortBy'] = 'DESC';
23 if($tableOptions['sortOrder'] && strpos($tableOptions['sortOrder'], '+') === 0){
24 $tableOptions['sortOrder'] = substr($tableOptions['sortOrder'], 1);
25 $tableOptions['sortBy'] = 'ASC';
26 }
27 $tableOptions['filter'] = isset($tableOptions['filter']) ? $tableOptions['filter'] : '';
28 $tableOptions['mode'] = isset($_COOKIE['mode']) ? $_COOKIE['mode'] : 'live';
29 $table['data'] = $flagManager->getAllForTable($tableOptions);
30 $scope = $this;
31
32 $table['total'] = $flagManager->getAllCountForTable($tableOptions);
33 $table['per_page'] = (int)$tableOptions['perPage'];
34 $table['current_page'] = (int)$tableOptions['page'];
35 $table['sort'] = $tableOptions['sortOrder'];
36 $table['last_page'] = ceil($table['total'] / $table['per_page']);
37 $table['next_page_url'] = 'api/get_permissions_for_table?page=' . ($tableOptions['page'] + 1);
38 $table['prev_page_url'] = null;
39 $table['from'] = (((int)$tableOptions['page'] - 1) * $table['per_page']) + 1;
40 $table['to'] = ((((int)$tableOptions['page']) * $table['per_page']) < $table['total']) ? ((int)$tableOptions['page']) * $table['per_page'] : $table['total'];
41 $this->output->set_content_type('application/json');
42 echo json_encode($table);
43 die;
44 }
45
46 public function create_flag(){
47 $requestData = json_decode(file_get_contents('php://input'), true);
48
49 $this->form_validation->set_data($requestData);
50 $this->form_validation->set_rules('name', 'Flag Name', 'required');
51 $this->form_validation->set_rules('encrypted_name', 'Encrypted Name', 'required');
52
53 if($this->form_validation->run() == false){
54 echo json_encode(['success' => false, 'errors' => array_values($this->form_validation->error_array())]);
55 die;
56 }
57
58 $flagManager = new \BTFlags\FlagManager();
59 $created = $flagManager->create($requestData);
60 if(!$created) die(json_encode(['success' => false, 'errors' => $flagManager->errors()]));
61
62 $log_data = array(
63 'date_created' => ph_time(),
64 'event' => 'Create Flag',
65 'description' => 'Flag Name: ' . $requestData['name'],
66 );
67 $add_log = insert_admin_log($log_data);
68
69 echo json_encode(['success' => true, 'message' => 'Flag successfully created!']);
70 die;
71 }
72
73 /**
74 * Update a flag
75 * @return void
76 */
77 public function update_flag(){
78 $requestData =json_decode(file_get_contents('php://input'), true);
79 $this->form_validation->set_data($requestData);
80 $this->form_validation->set_rules('name', 'Flag Name', 'required');
81 $this->form_validation->set_rules('encrypted_name', 'Encrypted Name', 'required');
82
83 if($this->form_validation->run() == false){
84 echo json_encode(['success' => false, 'errors' => array_values($this->form_validation->error_array())]);
85 die;
86 }
87 $flagManager = new \BTFlags\FlagManager();
88 $updated = $flagManager->update($requestData);
89 if(!$updated) die(json_encode(['success' => false, 'errors' => $flagManager->errors()]));
90
91
92 $log_data = array(
93 'date_created' => ph_time(),
94 'event' => 'Update Flag',
95 'description' => 'Flag Name: ' . $requestData['name'],
96 );
97 $add_log = insert_admin_log($log_data);
98
99 echo json_encode(['success' => true, 'message' => 'Flag successfully updated!']);
100 die;
101 }
102
103 /**
104 * Delete a flag
105 * @return void
106 */
107 public function delete_flag(){
108 $requestData = json_decode(file_get_contents('php://input'), true);
109
110 $flagManager = new \BTFlags\FlagManager();
111 $deleted = $flagManager->delete($requestData);
112 if(!$deleted) die(json_encode(['success' => false, 'errors' => $flagManager->errors()]));
113
114 $log_data = array(
115 'date_created' => ph_time(),
116 'event' => 'Delete Flag',
117 'description' => 'Flag Name: ' . $requestData['name'],
118 );
119 $add_log = insert_admin_log($log_data);
120
121 echo json_encode(['success' => true, 'message' => 'Flag successfully deleted!']);
122 die;
123 }
124
125 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2
3 class Flag{
4 public function register(){
5 $flagManager = new \LawFormsUSA\Flags\FlagManager();
6 $flagManager->register();
7 }
8 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2 defined('BASEPATH') OR exit('No direct script access allowed');
3
4 /*
5 | -------------------------------------------------------------------------
6 | Hooks
7 | -------------------------------------------------------------------------
8 | This file lets you define "hooks" to extend CI without hacking the core
9 | files. Please see the user guide for info:
10 |
11 | https://codeigniter.com/user_guide/general/hooks.html
12 |
13 */
14 $hook['post_controller_constructor'][] = array(
15 'class' => 'Flag',
16 'function' => 'register',
17 'filename' => 'Flag.php',
18 'filepath' => 'hooks',
19 );
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2
3 if (!function_exists('flag')){
4 function flag($flag, $default = null, $encrypted = false){
5 return \LawFormsUSA\Flags\Flags::get($flag, $encrypted, $default);
6 }
7 }
...\ No newline at end of file ...\ 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!