/**
 * This is just for example
 * The `onUpload` hook for an AsynFileUploader to do database operation
 * regarding to the uploaded files
 *
 * @param  array $file The array of the uploaded file information:
 *
 *     array(
 *       'name'             => 'Name of the input element',
 *       'fileName'         => 'The uploaded file name',
 *       'originalFileName' => 'The original file name user selected',
 *       'extension'        => 'The selected and uploaded file extension',
 *       'dir'              => 'The uploaded directory',
 *     )
 *
 * @param  array $post The POSTed information
 *
 *     array(
 *       '{name}'            => Array of the file names uploaded and saved in drive
 *       '{name}-id'         => The database value ID (if a file have previously been uploaded)
 *       '{name}-{fieldName} => Optional hidden values
 *     )
 *
 * @return integer
 */
function example_docUpload($file, $post)
{
    # Save new file names in db
    db_insert('document', array('docFileName' => $file['fileName']), $useSlug = false);
    return db_insertId();
}
 /**
  * Handy MYSQL insert operation
  *
  * @param string $table The table name without prefix
  * @param array $data The array of data field names and values
  *
  *      array(
  *          'fieldNameToSlug' => $valueToSlug, # if $lc_useDBAutoFields is enabled
  *          'fieldName1' => $fieldValue1,
  *          'fieldName2' => $fieldValue2
  *      )
  *
  * @param boolean $useSlug True to include the slug field or False to not exclude it
  * @return mixed Returns inserted id on success or FALSE on failure
  */
 function db_insert($table, $data = array(), $useSlug = true)
 {
     if (count($data) == 0) {
         return;
     }
     global $_DB;
     global $_conn;
     global $lc_useDBAutoFields;
     $table = ltrim($table, db_prefix());
     $table = db_prefix() . $table;
     # Invoke the hook db_insert_[table_name] if any
     $hook = 'db_insert_' . strtolower($table);
     if (function_exists($hook)) {
         return call_user_func_array($hook, array($table, $data, $useSlug));
     }
     # if slug is already provided in the data array, use it
     if (array_key_exists('slug', $data)) {
         $slug = db_escapeString($data['slug']);
         $slug = _slug($slug);
         $data['slug'] = $slug;
         session_set('lastInsertSlug', $slug);
         $useSlug = false;
     }
     $dsm = $_DB->schemaManager;
     if (is_object($dsm) && $dsm->isLoaded()) {
         foreach ($data as $field => $value) {
             $fieldType = $_DB->schemaManager->getFieldType($table, $field);
             if (is_array($value) && $fieldType == 'array') {
                 $data[$field] = serialize($value);
             } elseif (is_array($value) && $fieldType == 'json') {
                 $jsonValue = json_encode($value);
                 $data[$field] = $jsonValue ? $jsonValue : null;
             } elseif ($fieldType == 'boolean') {
                 $data[$field] = $value ? 1 : 0;
             }
         }
     }
     $fields = array_keys($data);
     $dataValues = array_values($data);
     if (db_tableHasSlug($table, $useSlug)) {
         $fields[] = 'slug';
     }
     if (db_tableHasTimestamps($table)) {
         $fields[] = 'created';
         $fields[] = 'updated';
     }
     $sqlFields = implode(', ', $fields);
     $values = array();
     $i = 0;
     # escape the data
     foreach ($dataValues as $val) {
         if ($i == 0 && $useSlug) {
             $slug = db_escapeString($val);
         }
         if (is_null($val)) {
             $values[] = 'NULL';
         } else {
             $values[] = '"' . db_escapeString($val) . '"';
         }
         $i++;
     }
     if (db_tableHasSlug($table, $useSlug)) {
         $slug = _slug($slug, $table);
         session_set('lastInsertSlug', $slug);
         $values[] = '"' . $slug . '"';
     }
     if (db_tableHasTimestamps($table)) {
         $values[] = '"' . date('Y-m-d H:i:s') . '"';
         $values[] = '"' . date('Y-m-d H:i:s') . '"';
     }
     $sqlValues = implode(', ', $values);
     $sql = 'INSERT INTO ' . $table . ' (' . $sqlFields . ')
             VALUES ( ' . $sqlValues . ' )';
     return db_query($sql) ? db_insertId() : false;
 }