2 /***************************************
3 * http://www.program-o.com
6 * FILE: botpersonality.php
7 * AUTHOR: Elizabeth Perreau and Dave Morton
9 * DETAILS: Displays predicate values for the current chatbot
10 ***************************************/
11 # set template section defaults
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);
28 $noRightNav = $template->getSection('NoRightNav');
29 $headerTitle = 'Actions:';
30 $pageTitle = 'My-Program O - Bot Personality';
31 $mainContent = "main content";
35 case 'addBotPersonality' :
37 $mainContent = getBot();
40 $mainContent = $func();
42 $mainTitle = 'Bot Personality Settings for ' . $bot_name;
54 <label for="[row_label]">
55 <span class="label">[row_label]:</span>
57 <input name="[row_label]" id="[row_label]" value="[row_value]" />
63 ' <td style="text-align: center">
64 <label for="newEntryName[cid]">
66 New Entry Name: <input name="newEntryName[cid]" id="newEntryName[cid]" style="width: 98%" />
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]" />
75 $startDiv = ' <td>' . "\n ";
76 $endDiv = "\n </td>\n <br />\n";
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);
90 foreach ($rows as $row)
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);
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);
117 $action = 'Update Data';
123 $action = 'Add New Data';
124 $func = 'addBotPersonality';
129 <form name="botpersonality" action="index.php?page=botpersonality" method="post">
130 <table class="botForm">
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">
150 * Function stripslashes_deep
155 function stripslashes_deep($value)
157 $newValue = stripslashes($value);
158 while ($newValue != $value)
161 $newValue = stripslashes($value);
174 global $bot_id, $bot_name, $post_vars;
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);";
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;
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__);
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');
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);
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.";
239 * Function addBotPersonality
244 function addBotPersonality()
247 $bot_id = $post_vars['bot_id'];
248 $sql = "Insert into `botpersonality` (`id`, `bot_id`, `name`, `value`) values (null, $bot_id, :name, :value);";
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]);
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);
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;
296 $msg = 'Error updating bot personality.';
309 $out = ' <table class="botForm">
313 ' <td><label for="[field]"><span class="label">[uc_field]:</span></label> <span class="formw"><input name="[field]" id="[field]" value="" /></span></td>
318 $blankTD = ' <td> </td>
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> <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> <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> <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>
329 $fields = file(_CONF_PATH_ . 'default_botpersonality_fields.dat');
331 foreach ($fields as $field)
334 $field = trim($field);
335 $tmpRow = str_replace('[field]', $field, $rowTemplate);
336 $tmpRow = str_replace('[uc_field]', ucfirst($field), $tmpRow);
347 $out .= $blankTD . $blankTD;