private function create($id = null)
 {
     $pdo = DatabaseFactory::getFactory()->getConnection();
     $sql = "SHOW FULL COLUMNS FROM " . $this->table;
     $fields = $pdo->prepare($sql);
     $fields->execute();
     $results = $fields->fetchAll();
     //check to get the fields we want form our $selectcolumns, if needed
     if (count($this->fields) < 0) {
         //need to mlogic here, so its disabled (allow/disallow editing based on auth
         foreach ($results as $values) {
             if (in_array($values->Field, $this->fields)) {
                 $columns[] = $values;
             }
         }
     } else {
         //or else just get all of them..
         $columns = $results;
     }
     //if we're given a key-value, then we are looking for a specific row.
     // Get the values of the row and insert them
     $data = null;
     //initialize
     if (isset($id)) {
         //if we're not given specific fields, then we're getting every field
         $pdo = DatabaseFactory::getFactory()->getConnection();
         $sql = "SELECT * FROM " . $this->table . " WHERE id = " . $id;
         $query = $pdo->prepare($sql);
         $query->execute();
         $data = $query->fetchObject();
     }
     $title = ucwords(str_replace('_', ' ', $this->table));
     //splits table name, replaces the underscore (naming convention) and caps the first letter of each resulting word
     $t = '
     <div class="col-lg-5" />
     <h3>' . $title . '</h3>
     <form id="crud_' . $this->table . '_form" class="crudform " method="post" action="?action=update">
     <input type="hidden" name="table" value="' . $this->table . '" readonly />';
     foreach ($columns as $col) {
         $colname = $col->Field;
         $value = null;
         // this should set the field values and choose the fields
         if (!empty($data)) {
             $value = $data->{$colname};
         }
         if ($col->Key == 'PRI') {
             $t .= Fields::hidden($colname, $value, $colname);
         } else {
             if (isset($this->select) && $colname == $this->select) {
                 $list = array();
                 $options = FormModel::getrecord($this->optiontable);
                 foreach ($options as $k => $v) {
                     $list[$v->id] = $v->name;
                 }
                 $t .= Fields::select($colname, null, $list, $value);
             } else {
                 if ($colname == 'user_editor') {
                     $t .= Fields::hidden('user_editor', Session::get('user_id'));
                 } else {
                     if (strpos($colname, '_ID')) {
                         $tbl = str_replace('_ID', '', $colname);
                         $tbl = str_replace('FK', '', $tbl);
                         $tbl = lcfirst($tbl);
                         $t .= Fields::selectgen($colname, $col->Comment, $tbl, true, $value);
                     } else {
                         $type = $col->Type;
                         switch ($type) {
                             case strpos($type, 'int'):
                             case strpos($type, 'smallint'):
                             case strpos($type, 'bigint'):
                                 $t .= Fields::number($colname, $value, ucfirst($colname), $col->Comment);
                                 break;
                             case strpos($type, 'tinyint'):
                                 $t .= Fields::bool('active', $col->Comment, 'Active', 'Inactive', $value);
                                 break;
                             case strpos($type, 'char'):
                             case strpos($type, 'varchar'):
                             case strpos($type, 'tinytext'):
                             case strpos($type, 'text'):
                                 $t .= Fields::text($colname, $value, ucfirst($colname), $col->Comment);
                                 break;
                             case strpos($type, 'longtext'):
                             case strpos($type, 'mediumtext'):
                                 $t .= Fields::textarea($colname, $value, ucfirst($colname), $col->Comment);
                                 break;
                             case 'date':
                                 $t .= Fields::date($colname, $value, ucfirst($colname), $col->Comment);
                                 break;
                             case strpos($type, 'timestamp'):
                                 if ($colname == 'updated') {
                                     $t .= Fields::hidden($colname, date('Y-m-d H:i:s'));
                                 } else {
                                     $t .= '';
                                     //do not edit time stamp
                                 }
                                 break;
                             case strpos($type, 'time'):
                                 $t .= Fields::date($colname, $value, ucfirst($colname), ucfirst($colname));
                             case strpos($type, 'datetime'):
                                 $t .= Fields::date($colname, $value, ucfirst($colname), ucfirst($colname));
                                 break;
                             default:
                                 $t .= Fields::text($colname);
                         }
                     }
                 }
             }
         }
     }
     $t .= '<br/><br/><input type="submit" class="btn btn-success" /> <a href="?action=index" class="btn btn-info" style="float: right" >Home</a>';
     $t .= '</form>
         </div>';
     return $t;
 }
Beispiel #2
0
    private function makeform($id)
    {
        $results = [];
        foreach (get_object_vars($this->activeclass) as $key) {
            $results[] = $key;
        }
        $pdo = DatabaseFactory::getFactory()->getConnection();
        $sql = "SHOW FULL COLUMNS FROM " . $this->table;
        $fields = $pdo->prepare($sql);
        $fields->execute();
        //$results = $fields->fetchAll();
        //check to get the fields we want form our $selectcolumns, if needed
        if (count($this->fields) < 0) {
            //need to logic here, so its disabled (allow/disallow editing based on auth
            foreach ($results as $values) {
                if (in_array($values->name, $this->fields)) {
                    $columns[] = $values;
                }
            }
        } else {
            //or else just get all of them..
            $columns = $results;
        }
        $data = null;
        //initialize
        if (isset($id)) {
            //if we're not given specific fields, then we're getting every field
            $pdo = DatabaseFactory::getFactory()->getConnection();
            $sql = "SELECT * FROM " . $this->table . " WHERE id = " . $id;
            $query = $pdo->prepare($sql);
            $query->execute();
            $data = $query->fetchObject();
        }
        $title = ucwords(str_replace('_', ' ', $this->table));
        //splits table name, replaces the underscore (naming convention) and caps the first letter of each resulting word
        //form layouts:
        $cl = count($columns);
        $colswanted = $cl < 12 ? 2 : 3;
        $colslegth = $cl / $colswanted;
        $t = '

        <h3>' . $title . '</h3>
        <hr/>
        <div class="row">

        <form id="crud_' . $this->table . '_form" class="crudform " method="post" action="?action=update&table=' . $this->table . '">
        <input type="hidden" name="table" value="' . $this->table . '" readonly />
        <div class="col-md-4" >';
        $count = 0;
        //counter for the form columns
        foreach ($columns as $col) {
            //$colname = $col->Field;
            $colname = $col->name;
            $value = null;
            // this should set the field values and choose the fields
            if (!empty($data)) {
                $value = $data->{$colname};
            }
            //begin printing fields
            if ($col->name == 'id') {
                $t .= Fields::hidden($colname, $value, $colname);
            } else {
                if (isset($this->select) && $colname == $this->select) {
                    $list = array();
                    $options = FormModel::getrecord($this->optiontable);
                    foreach ($options as $k => $v) {
                        $list[$v->id] = $v->name;
                    }
                    $t .= Fields::select($colname, null, $list, $value);
                } else {
                    if ($colname == 'editor' || $colname == 'created' || $colname == 'updated') {
                        $t .= '';
                    } else {
                        if (strpos($colname, '_type')) {
                            $t .= Fields::selectgen($colname, $col->label, $colname, true, $value);
                        } else {
                            if (strpos($colname, 'users')) {
                                $t .= '';
                            } else {
                                if (strpos($colname, '_id')) {
                                    $tbl = str_replace('_id', '', $colname);
                                    $tbl = lcfirst($tbl);
                                    $t .= Fields::selectgen($colname, $col->label, $tbl, true, $value);
                                } else {
                                    $type = $col->type;
                                    switch ($type) {
                                        case strpos($type, 'int'):
                                        case strpos($type, 'smallint'):
                                        case strpos($type, 'bigint'):
                                            $t .= Fields::number($colname, $value, ucfirst($colname), $col->label);
                                            break;
                                        case strpos($type, 'tinyint'):
                                            $t .= Fields::bool('active', $col->label, 'Active', 'Inactive', $value);
                                            break;
                                        case strpos($type, 'char'):
                                        case strpos($type, 'varchar'):
                                        case strpos($type, 'tinytext'):
                                        case strpos($type, 'text'):
                                            $t .= Fields::text($colname, $value, ucfirst($colname), $col->label);
                                            break;
                                        case strpos($type, 'longtext'):
                                        case strpos($type, 'mediumtext'):
                                            $t .= Fields::textarea($colname, $value, ucfirst($colname), $col->label);
                                            break;
                                        case strpos($type, 'timestamp'):
                                            if ($colname == 'updated') {
                                                $t .= Fields::hidden($colname, date('Y-m-d H:i:s'));
                                            } else {
                                                $t .= '';
                                                //do not edit time stamp
                                            }
                                            break;
                                        case strpos($type, 'time'):
                                        case strpos($type, 'date'):
                                            $t .= Fields::date($colname, $value, ucfirst($colname), $col->label);
                                            break;
                                        case strpos($type, 'year'):
                                            $t .= Fields::year($colname, $value, ucfirst($colname), $col->label);
                                            break;
                                        case strpos($type, 'datetime'):
                                            $t .= Fields::date($colname, $value, ucfirst($colname), ucfirst($colname));
                                            break;
                                        default:
                                            $t .= Fields::text($colname);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $count++;
            if ($count > $colslegth) {
                $t .= '</div><div class="col-md-4" >';
                $count = 0;
            }
        }
        $t .= '<br/><input type="submit" class="btn btn-success" /> <a href="?action=index&id=' . $id . '&table=' . $this->table . ' " class="btn btn-info" style="float: right" >Home</a></div></div>';
        $t .= '</form>
            ';
        return $t;
    }