added some development tools
[windows-sources.git] / developer / Samples / Bot / admin / botpersonality.php
blob2126e542783b09212aa3029777bc6d1c6ca76643
1 <?PHP
2 /***************************************
3 * http://www.program-o.com
4 * PROGRAM O
5 * Version: 2.5.3
6 * FILE: botpersonality.php
7 * AUTHOR: Elizabeth Perreau and Dave Morton
8 * DATE: 05-26-2014
9 * DETAILS: Displays predicate values for the current chatbot
10 ***************************************/
11 # set template section defaults
12 # Build page sections
13 # ordered here in the order that the page is constructed
14 $post_vars = filter_input_array(INPUT_POST);
15 $bot_name = (isset ($_SESSION['poadmin']['bot_name'])) ? $_SESSION['poadmin']['bot_name'] : 'unknown';
16 $func = (isset ($post_vars['func'])) ? $post_vars['func'] : 'getBot';
17 $topNav = $template->getSection('TopNav');
18 $leftNav = $template->getSection('LeftNav');
19 $main = $template->getSection('Main');
21 $navHeader = $template->getSection('NavHeader');
23 $FooterInfo = getFooter();
24 $errMsgClass = (!empty ($msg)) ? "ShowError" : "HideError";
25 $errMsgStyle = $template->getSection($errMsgClass);
26 $noLeftNav = '';
27 $noTopNav = '';
28 $noRightNav = $template->getSection('NoRightNav');
29 $headerTitle = 'Actions:';
30 $pageTitle = 'My-Program O - Bot Personality';
31 $mainContent = "main content";
32 switch ($func)
34 case 'updateBot' :
35 case 'addBotPersonality' :
36 $msg = $func();
37 $mainContent = getBot();
38 break;
39 default :
40 $mainContent = $func();
42 $mainTitle = 'Bot Personality Settings for ' . $bot_name;
44 /**
45 * Function getBot
48 * @return string
50 function getBot()
52 $formCell =
53 ' <td>
54 <label for="[row_label]">
55 <span class="label">[row_label]:</span>
56 <span class="formw">
57 <input name="[row_label]" id="[row_label]" value="[row_value]" />
58 </span>
59 </label>
60 </td>
62 $blankCell =
63 ' <td style="text-align: center">
64 <label for="newEntryName[cid]">
65 <span class="label">
66 New Entry Name: <input name="newEntryName[cid]" id="newEntryName[cid]" style="width: 98%" />
67 </span>
68 </label>&nbsp;
69 <label for="newEntryValue[cid]" style="float: left; padding-left: 3px;">
70 <span class="formw">New Entry Value: </span>
71 <input name="newEntryValue[cid]" id="newEntryValue[cid]" />
72 </label>
73 </td>
75 $startDiv = ' <td>' . "\n ";
76 $endDiv = "\n </td>\n <br />\n";
77 $inputs = "";
78 $row_class = 'row fm-opt';
79 $bot_name = $_SESSION['poadmin']['bot_name'];
80 $bot_id = (isset ($_SESSION['poadmin']['bot_id'])) ? $_SESSION['poadmin']['bot_id'] : 0;
81 $bot_id = ($bot_id == 'new') ? 0 : $bot_id;
82 //get the current bot's personality table from the db
83 $sql = "SELECT * FROM `botpersonality` where `bot_id` = $bot_id";
84 $rows = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
85 $rowCount = count($rows);
86 if ($rowCount > 0)
88 $left = true;
89 $colCount = 0;
90 foreach ($rows as $row)
92 $rid = $row['id'];
93 $label = $row['name'];
94 $value = stripslashes_deep($row['value']);
95 $tmpRow = str_replace('[row_class]', $row_class, $formCell);
96 $tmpRow = str_replace('[row_id]', $rid, $tmpRow);
97 $tmpRow = str_replace('[row_label]', $label, $tmpRow);
98 $tmpRow = str_replace('[row_value]', $value, $tmpRow);
99 $inputs .= $tmpRow;
100 $colCount++;
101 if ($colCount >= 3)
103 $inputs .= ' </tr>
104 <tr>' . PHP_EOL;
105 $colCount = 0;
108 $inputs .= "<!-- colCount = $colCount -->\n";
109 if (($colCount > 0) and ($colCount < 3))
111 for ($n = 0; $n < (3 - $colCount); $n++)
113 $addCell = str_replace('[cid]', "[$n]", $blankCell);
114 $inputs .= $addCell;
117 $action = 'Update Data';
118 $func = 'updateBot';
120 else
122 $inputs = newForm();
123 $action = 'Add New Data';
124 $func = 'addBotPersonality';
126 if (empty ($func))
127 $func = 'getBot';
128 $form = <<<endForm2
129 <form name="botpersonality" action="index.php?page=botpersonality" method="post">
130 <table class="botForm">
131 <tr>
132 $inputs
133 </tr>
134 <tr>
135 <td colspan="3">
136 <input type="hidden" id="bot_id" name="bot_id" value="$bot_id">
137 <input type="hidden" id="func" name="func" value="$func">
138 <input type="submit" name="action" id="action" value="$action">
139 </td>
140 </tr>
141 </table>
142 </form>
143 <!-- fieldset>
144 </fieldset -->
145 endForm2;
146 return $form;
150 * Function stripslashes_deep
152 * * @param $value
153 * @return string
155 function stripslashes_deep($value)
157 $newValue = stripslashes($value);
158 while ($newValue != $value)
160 $value = $newValue;
161 $newValue = stripslashes($value);
163 return $newValue;
167 * Function updateBot
170 * @return string
172 function updateBot()
174 global $bot_id, $bot_name, $post_vars;
175 $msg = "";
176 if (!empty ($post_vars['newEntryName']))
178 $newEntryNames = $post_vars['newEntryName'];
179 $newEntryValues = $post_vars['newEntryValue'];
180 $sql = "Insert into `botpersonality` (`id`, `bot_id`, `name`, `value`) values (null, $bot_id, :name, :value);";
181 $params = array();
182 foreach ($newEntryNames as $index => $key)
184 $value = $newEntryValues[$index];
185 if (empty ($value)) continue;
186 $params[] = array(':name' => $key, ':value' => $value);
188 $rowsAffected = db_write($sql, $params, true, __FILE__, __FUNCTION__, __LINE__);
189 if ($rowsAffected > 0)
191 $msg = (empty ($msg)) ? "Bot personality added. \n" : $msg;
193 else
195 $msg = 'Error updating bot personality.';
198 $sql = "SELECT * FROM `botpersonality` where `bot_id` = $bot_id;";
199 $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
200 $rows = array();
201 $insertParams = array();
202 $updateParams = array();
203 foreach ($result as $row)
205 $name = $row['name'];
206 $value = $row['value'];
207 $rows[$name] = array('id' => $row['id'], 'value' => $value);
209 $insertSQL = "Insert into `botpersonality` (`id`, `bot_id`, `name`, `value`) values (null, $bot_id, :name, :value);";
210 $updateSQL = "update `botpersonality` set `value` = :value where `id` = :id;";
211 $exclude = array('bot_id', 'func', 'action', 'newEntryName', 'newEntryValue');
212 $values = '';
213 foreach ($post_vars as $key => $value)
215 if (in_array($key, $exclude)) continue;
216 if (!isset($rows[$key]))
218 $insertParams[] = array(':name' => $key, ':value' => $value);
220 else
222 $oldValue = $rows[$key]['value'];
223 if ($value != $oldValue)
225 $curId = $rows[$key]['id'];
226 $updateParams[] = array(':value' => $value, ':id' => $curId);
230 if (empty($insertParams) && empty($updateParams)) return 'No changes found.';
231 $affectedRows = (!empty($updateParams)) ? db_write($updateSQL, $updateParams, true, __FILE__, __FUNCTION__, __LINE__) : 0;
232 $affectedRows += (!empty($updateParams)) ? db_write($insertSQL, $insertParams, true, __FILE__, __FUNCTION__, __LINE__) : 0;
233 if ($affectedRows > 0) $msg = 'Bot Personality Updated.';
234 else $msg = "Something went wrong! Affected rows = $affectedRows.";
235 return $msg;
239 * Function addBotPersonality
242 * @return string
244 function addBotPersonality()
246 global $post_vars;
247 $bot_id = $post_vars['bot_id'];
248 $sql = "Insert into `botpersonality` (`id`, `bot_id`, `name`, `value`) values (null, $bot_id, :name, :value);";
249 $msg = "";
250 $params = array();
251 $newEntryNames = (isset ($post_vars['newEntryName'])) ? $post_vars['newEntryName'] : '';
252 $newEntryValues = (isset ($post_vars['newEntryValue'])) ? $post_vars['newEntryValue'] : '';
253 if (!empty ($newEntryNames))
255 if (is_string($newEntryNames))
257 $newEntryNames = array(0 => $newEntryNames);
259 foreach ($newEntryNames as $index => $key)
261 $value = trim($newEntryValues[$index]);
262 if (!empty ($value))
264 $params[] = array(':name' => $key, ':value' => $value);
268 $skipKeys = array('bot_id', 'action', 'func', 'newEntryName', 'newEntryValue');
269 $sqlParams = array();
270 foreach ($post_vars as $key => $value)
272 if (in_array($key, $skipKeys)) continue;
273 if (is_array($value))
275 foreach ($value as $index => $fieldValue)
277 $field = $key[$fieldValue];
278 $fieldValue = trim($fieldValue);
279 $params[] = array(':name' => $field, ':value' => $fieldValue);
281 continue;
283 else
285 $value = trim($value);
286 $params[] = array(':name' => $key, ':value' => $value);
289 $rowsAffected = db_write($sql, $params, true, __FILE__, __FUNCTION__, __LINE__);
290 if ($rowsAffected > 0)
292 $msg = (empty ($msg)) ? "Bot personality added. \n" : $msg;
294 else
296 $msg = 'Error updating bot personality.';
298 return $msg;
302 * Function newForm
305 * @return string
307 function newForm()
309 $out = ' <table class="botForm">
310 <tr>
312 $rowTemplate =
313 ' <td><label for="[field]"><span class="label">[uc_field]:</span></label> <span class="formw"><input name="[field]" id="[field]" value="" /></span></td>
315 $tr = ' </tr>
316 <tr>
318 $blankTD = ' <td>&nbsp;</td>
320 $lastBit =
321 ' </tr>
322 <tr>
323 <td style="text-align: center"><label for="newEntryName[0]"><span class="label">New Entry Name: <input name="newEntryName[0]" id="newEntryName[0]" style="width: 98%" /></label></span>&nbsp;<span class="formw"><label for="newEntryValue[0]" style="float: left; padding-left: 3px;">New Entry Value: </label><input name="newEntryValue[0]" id="newEntryValue[0]" /></span></td>
324 <td style="text-align: center"><label for="newEntryName[1]"><span class="label">New Entry Name: <input name="newEntryName[1]" id="newEntryName[1]" style="width: 98%" /></label></span>&nbsp;<span class="formw"><label for="newEntryValue[1]" style="float: left; padding-left: 3px;">New Entry Value: </label><input name="newEntryValue[1]" id="newEntryValue[1]" /></span></td>
325 <td style="text-align: center"><label for="newEntryName[2]"><span class="label">New Entry Name: <input name="newEntryName[2]" id="newEntryName[2]" style="width: 98%" /></label></span>&nbsp;<span class="formw"><label for="newEntryValue[2]" style="float: left; padding-left: 3px;">New Entry Value: </label><input name="newEntryValue[2]" id="newEntryValue[2]" /></span></td>
326 </tr>
327 </table>
329 $fields = file(_CONF_PATH_ . 'default_botpersonality_fields.dat');
330 $count = 0;
331 foreach ($fields as $field)
333 $count++;
334 $field = trim($field);
335 $tmpRow = str_replace('[field]', $field, $rowTemplate);
336 $tmpRow = str_replace('[uc_field]', ucfirst($field), $tmpRow);
337 $out .= $tmpRow;
338 if ($count % 3 == 0)
339 $out .= $tr;
341 switch ($count % 3)
343 case 1 :
344 $out .= $blankTD;
345 break;
346 case 2 :
347 $out .= $blankTD . $blankTD;
349 $out .= $lastBit;
350 return $out;