Example #1
0
 /**
  * Constructor for Maggy
  * 
  * There needs to be a Ruth Object DEB for the database declared using the Debby Class
  * The path is relative to your web folder.
  * 
  * @param String $migrationPath relative path to your web folder
  * @param String $delim A delimiter to say how your SQL is run
  */
 function __construct($migrationPath = "migration", $delim = ";", $runMigrations = false)
 {
     if (empty(Ruth::getOBJECT("DEB"))) {
         die("You need to declare a database connection using Debby and assign Ruth a DEB object");
     } else {
         $this->DEB = Ruth::getOBJECT("DEB");
     }
     $this->delim = $delim;
     $database = $this->DEB->getDatabase();
     $this->migrationPath = $migrationPath;
     if (empty($database["TINA4_MAGGY"]) && empty($database["tina4_maggy"])) {
         echo (new Cody())->bootStrapAlert("success", $caption = "Success", "Maggy: I need to create a migration table because it doesn't exist \n");
         $this->DEB->exec("create table tina4_maggy (" . "migration_id varchar (14) not null," . "description varchar (1000) default ''," . "content blob," . "passed integer default 0," . "primary key (migration_id))");
         $this->DEB->commit();
     }
     if ($runMigrations && file_exists($this->migrationPath)) {
         //Run the migration
         $this->doMigration();
     }
 }
Example #2
0
 /**
  * @param string $testDir
  * @param string $browser
  * @param string $testServer
  */
 function __construct($testDir = "test", $browser = "firefox", $testServer = "http://localhost:12346/")
 {
     global $TESSA;
     $TESSA = $this;
     if (!file_exists(dirname(__FILE__) . "/../selenium/PHPWebDriver/__init__.php")) {
         die("Please download the selenium driver for PHP and deploy in the web_root folder");
     }
     require_once dirname(__FILE__) . "/../selenium/PHPWebDriver/__init__.php";
     if (empty(Ruth::getOBJECT("DEB"))) {
         die("You need to declare a database connection using Debby and assign Ruth a DEB object");
     } else {
         $this->DEB = Ruth::getOBJECT("DEB");
     }
     $this->testDir = $testDir;
     $this->testServer = $testServer;
     if (file_exists($this->testDir)) {
         $this->testDir = $testDir;
         $this->browser = $browser;
         //Run the migration
         $this->doTesting();
     }
 }
Example #3
0
 /**
  * The constructor for Cody requires a Debby database connection
  * @param Debby $DEB
  */
 function __construct($DEB = "")
 {
     //check to see if we can get a database handle
     if (empty($DEB) && !empty(Ruth::getOBJECT("DEB"))) {
         $DEB = Ruth::getOBJECT("DEB");
     }
     $this->DEB = $DEB;
     Ruth::addRoute(RUTH_POST, "/cody/form/{action}", function ($action) {
         $object = json_decode(rawurldecode(Ruth::getREQUEST("object")));
         $record = json_decode(rawurldecode(Ruth::getREQUEST("record")));
         if ($action !== "delete") {
             echo $this->createTableForm($action, $object, $record, Ruth::getREQUEST("db"));
         } else {
             if ($action === "delete") {
                 $sql = $object[0];
                 if (!empty($object[5])) {
                     $name = $object[5];
                 } else {
                     $name = "grid";
                 }
                 if (!empty($object[6])) {
                     $tableInfo = $object[6];
                     $keyName = strtoupper($tableInfo->primarykey);
                     $tableName = $tableInfo->table;
                 } else {
                     $keyName = key($record);
                     //this is just a quess
                     $sql = explode("from", $sql);
                     $sql = explode($sql[1], " ");
                     $tableName = $sql[0];
                 }
                 $DEB = Ruth::getOBJECT(Ruth::getREQUEST("db"));
                 $DEB->delete($tableName, [$keyName => $record->{$keyName}]);
                 if (!empty(ONDELETE)) {
                     $params = ["action" => $action, "table" => $tableName, $keyName => $record->{$keyName}, "session" => Ruth::getSESSION(), "request" => Ruth::getREQUEST()];
                     @call_user_func_array(ONINSERT, $params);
                 }
                 echo $this->bootStrapAlert("success", $caption = "Success", "Record deleted");
                 echo script('$table' . $name . '.bootstrapTable("refresh");');
             } else {
                 echo $this->bootStrapAlert("danger", $caption = "Failed {$action}", "This action is unknown!");
             }
         }
     });
     Ruth::addRoute(RUTH_POST, "/cody/form/{action}/post", function ($action) {
         $DEB = Ruth::getOBJECT(Ruth::getREQUEST("db"));
         $object = json_decode(rawurldecode(Ruth::getREQUEST("object")));
         $record = json_decode(rawurldecode(Ruth::getREQUEST("record")));
         $sql = $object[0];
         $fieldInfo = $DEB->getFieldInfo($sql);
         $dateFields = [];
         //TODO: determine the password and date fields
         foreach ($fieldInfo as $fid => $field) {
             if ($field["type"] === "DATETIME" || $field["type"] === "DATE") {
                 $dateFields[] = $field["name"];
             }
         }
         $passwordFields = "passwd,password";
         $dateFields = join(",", $dateFields);
         if (!empty($object[5])) {
             $name = $object[5];
         } else {
             $name = "grid";
         }
         if (!empty($object[6])) {
             $tableInfo = $object[6];
             $keyName = strtoupper($tableInfo->primarykey);
             $tableName = $tableInfo->table;
         } else {
             $keyName = key($record);
             //this is just a quess
             $sql = explode("from", $sql);
             $sql = explode($sql[1], " ");
             $tableName = $sql[0];
         }
         switch ($action) {
             case "insert":
                 $sqlInsert = $DEB->getInsertSQL("txt", $tableName, $keyName, true, "{$action}{$name}", $passwordFields, $dateFields, true);
                 if ($sqlInsert) {
                     echo $this->bootStrapAlert("success", $caption = "Success", "Record was updated successfully");
                     echo script('$table' . $name . '.bootstrapTable("refresh");');
                 } else {
                     echo $this->bootStrapAlert("danger", $caption = "Failure", "Record could not be updated");
                 }
                 if (!empty(ONINSERT)) {
                     $params = ["action" => $action, "table" => $tableName, $keyName => $_REQUEST["{$action}{$name}"], "session" => Ruth::getSESSION(), "request" => Ruth::getREQUEST()];
                     @call_user_func_array(ONINSERT, $params);
                 }
                 break;
             case "update":
                 $sqlUpdate = $DEB->getUpdateSQL("txt", $tableName, $keyName, $record->{$keyName}, "{$action}{$name}", $passwordFields, $dateFields, true);
                 if ($sqlUpdate) {
                     echo $this->bootStrapAlert("success", $caption = "Success", "Record was updated successfully");
                     echo script('$table' . $name . '.bootstrapTable("refresh");');
                 } else {
                     echo $this->bootStrapAlert("danger", $caption = "Failure", "Record could not be updated");
                 }
                 if (!empty(ONUPDATE)) {
                     $params = ["action" => $action, "table" => $tableName, $keyName => $record->{$keyName}, "session" => Ruth::getSESSION(), "request" => Ruth::getREQUEST()];
                     @call_user_func_array(ONUPDATE, $params);
                 }
                 break;
             default:
                 echo $this->bootStrapAlert("danger", $caption = "Failed {$action}", "This action is unknown!");
                 break;
         }
     });
     //Add the route for getting the ajax data
     Ruth::addRoute(RUTH_POST, "/cody/data/ajax/{db}", function ($db) {
         if (empty($db)) {
             $db = Ruth::getOBJECT("KIM");
         }
         $DEB = Ruth::getOBJECT($db);
         $postData = json_decode(Ruth::getPOST_DATA());
         if (empty($postData)) {
             exit;
         }
         $object = json_decode(rawurldecode($postData->object));
         if (!empty($object[5])) {
             $name = $object[5];
         } else {
             $name = "grid";
         }
         if (!empty($postData->limit)) {
             $limit = $postData->limit;
         } else {
             $limit = $object[4];
         }
         if (!empty($postData->offset)) {
             $offSet = $postData->offset;
         } else {
             $offSet = 0;
         }
         if (!empty($postData->sort)) {
             $sort = $postData->sort;
         } else {
             $sort = "";
         }
         if (!empty($postData->order)) {
             $order = $postData->order;
         } else {
             $order = "";
         }
         if (!empty($postData->search)) {
             $search = $postData->search;
         } else {
             $search = "";
         }
         $orderBy = "";
         if (!empty($sort)) {
             $orderBy = "order by upper({$sort}) {$order}";
         }
         $sql = $object[0];
         $buttons = $object[1];
         if (!is_object($buttons)) {
             $tempButtons = explode(",", strtolower($buttons));
             if (is_array($tempButtons)) {
                 $buttons = script("var a{recordid}record = '{record}';");
                 foreach ($tempButtons as $bid => $button) {
                     switch ($button) {
                         case "insert":
                             $buttons .= (new Cody())->bootStrapButton("btnInsert", "Add", "call{$name}Ajax('/cody/form/insert','{$name}Target', {object : a{$name}object, record: a{recordid}record, db: '{$db}' })", "btn btn-success", "", true);
                             break;
                         case "update":
                             $buttons .= (new Cody())->bootStrapButton("btnEdit", "Edit", "call{$name}Ajax('/cody/form/update','{$name}Target', {object : a{$name}object, record: a{recordid}record, db: '{$db}' })", "btn btn-primary", "", true);
                             break;
                         case "delete":
                             $buttons .= (new Cody())->bootStrapButton("btnDelete", "Del", "if (confirm('Are you sure you want to delete this record ?')) { call{$name}Ajax('/cody/form/delete','{$name}Target', {object : a{$name}object, record: a{recordid}record, db: '{$db}' }) }", "btn btn-danger", "", true);
                             break;
                         case "view":
                             $buttons .= (new Cody())->bootStrapButton("btnInsert", "View", "call{$name}Ajax('/cody/form/view','{$name}Target', {object : a{$name}object, record: a{recordid}record, db: '{$db}' })", "btn btn-warning", "", true);
                             break;
                     }
                 }
             }
             $buttons = div(["class" => "btn-group btn-group"], $buttons);
         }
         $hideColumns = $object[2];
         $customFields = "";
         if (!empty($object[4])) {
             $customFields = $object[4];
         }
         if (!empty($object[15])) {
             $mtooltip = $object[15];
         } else {
             $mtooltip = "";
         }
         $hideColumns = explode(",", strtoupper($hideColumns));
         $DEB->getRow("select first 1 * from ({$sql}) t");
         $fieldInfo = $DEB->fieldinfo;
         $filter = "";
         if (!empty($search)) {
             $filter = [];
             foreach ($fieldInfo as $cid => $field) {
                 if ($field["type"] === "DATE") {
                     $filter[] = "cast(\"{$field["alias"]}\" as varchar(20)) like '%" . strtoupper($search) . "%'";
                 } else {
                     if ($field["type"] === "VARCHAR") {
                         $filter[] = "upper({$field["alias"]}) like '" . strtoupper($search) . "%'";
                     } else {
                         if (is_numeric($search)) {
                             $filter[] = "\"{$field["alias"]}\" = '{$search}'";
                         }
                     }
                 }
             }
             $filter = join(" or ", $filter);
             $filter = "where ({$filter})";
         }
         $data = $DEB->getRow("select count(*) as COUNTRECORDS from ({$sql}) t {$filter}");
         $recordCount = $data->COUNTRECORDS;
         $sql = "select first {$limit} skip {$offSet} * from ({$sql}) t {$filter} {$orderBy}";
         $records = $DEB->getRows($sql, DEB_ASSOC, true);
         $rows = [];
         $value = "";
         $calcField = null;
         foreach ($records as $rid => $record) {
             $row = null;
             $rowButtons = $buttons . "";
             foreach ($fieldInfo as $fid => $field) {
                 if ($fid == 0) {
                     $field["align"] = "left";
                 }
                 if (!in_array(strtoupper($field["name"]), $hideColumns)) {
                     $fieldName = strtoupper($field["name"]);
                     $fid = strtoupper($field["name"]);
                     if (isset($customFields->{$fieldName})) {
                         $customField = $customFields->{$fieldName};
                         //Populate variables in URL path
                         if (!empty($customField->url)) {
                             $urlPath = $customField->url;
                             foreach ($fieldInfo as $fid2 => $field2) {
                                 $urlPath = str_ireplace("{" . $field2["name"] . "}", $record[$field2["name"]], $urlPath);
                             }
                         } else {
                             $urlPath = "";
                         }
                         //Populate variables in Onclick event
                         if (!empty($customField->onclick)) {
                             $onClickEvent = $customField->onclick;
                             foreach ($fieldInfo as $fid2 => $field2) {
                                 $onClickEvent = str_ireplace("{" . $field2["name"] . "}", $record[$field2["name"]], $onClickEvent);
                             }
                         } else {
                             $onClickEvent = "";
                         }
                         $uniq = "";
                         //Populate variables in Onclick event
                         if (!empty($customField->id)) {
                             $parsedId = $customField->id;
                             foreach ($fieldInfo as $fid2 => $field2) {
                                 $parsedId = str_ireplace("{" . $field2["name"] . "}", $record[$field2["name"]], $parsedId);
                             }
                             $uniq = $parsedId;
                         } else {
                             $parsedId = "";
                         }
                         //Populate variables in comment field
                         if (!empty($customField->comment)) {
                             $comment = $customField->comment;
                             foreach ($fieldInfo as $fid2 => $field2) {
                                 $comment = str_ireplace("{" . $field2["name"] . "}", $record[$field2["name"]], $comment);
                             }
                         } else {
                             $comment = "";
                         }
                         //Populate variables in Dropdown SQL
                         if (!empty($customField->lookup)) {
                             $dropDownSql = $customField->lookup;
                             foreach ($fieldInfo as $fid2 => $field2) {
                                 $dropDownSql = str_ireplace("{" . $field2["name"] . "}", $record[$field2["name"]], $dropDownSql);
                             }
                         } else {
                             $dropDownSql = "";
                         }
                         //check to see if we have a custom class and add it to the container
                         if (empty($customField->class) ? $extraclass = "" : ($extraclass = $customField->class)) {
                         }
                         if (empty($customField->disabled)) {
                             $customField->disabled = false;
                         }
                         if (!empty($customField->list)) {
                             if (is_object($customField->list)) {
                                 $customField->list = get_object_vars($customField->list);
                             }
                         }
                         if (empty($customField->type)) {
                             $customField->type = "text";
                         }
                         switch ($customField->type) {
                             case "hidden":
                                 $row[$field["name"]] = $record[$fid];
                                 break;
                             case "lookup":
                                 if (!empty($customField->list[$record[$fid]])) {
                                     $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"] . " " . $extraclass], "" . $customField->list[$record[$fid]] . "");
                                 } else {
                                     $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"] . " " . $extraclass], "Unknown Lookup");
                                 }
                                 break;
                             case "link":
                                 $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"] . " " . $extraclass], area(["id" => $parsedId, "href" => $urlPath, "onclick" => $onClickEvent], "" . $record[$fid] . ""));
                                 break;
                             case "checkbox":
                                 $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"] . " " . $extraclass], "" . $record[$fid] . "");
                                 break;
                             case "calculated":
                                 eval('$value = ' . $customField->formula . ';');
                                 $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"] . " " . $extraclass], "" . $value . "");
                                 break;
                             case "image":
                                 if (empty($customField->size)) {
                                     $customField->size = "100x100";
                                 }
                                 $size = explode("x", $customField->size);
                                 $styleWidth = $size[0];
                                 $styleHeight = $size[1];
                                 $row[$field["name"]] = "" . img(["class" => "thumbnail", "style" => "height: {$styleHeight}px; width: {$styleWidth}px", "src" => $DEB->encodeImage($record[$fid], "/imagestore", $customField->size), "alt" => ucwords(str_replace("_", " ", strtolower($field["alias"])))]);
                                 //we need to make the record happy as well;
                                 $record[$fid] = "[image]";
                                 break;
                             case "dropdown":
                                 if (empty($uniq)) {
                                     $uniq = $customField["id"] . uniqid();
                                 }
                                 $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"] . " " . $extraclass], div(["class" => "dropdown"], button(["class" => "btn btn-default dropdown-toggle", "style" => "width:100%", "type" => "button", "id" => $uniq, "data-toggle" => "dropdown", "aria-expanded" => "true"], "" . $record[$fid] . " " . span(["class" => "caret"])), ul(["class" => "dropdown-menu", "role" => "menu", "aria-labelledby" => "dropdownMenu1"], (new Cody($DEB))->populateDropDownItems($dropDownSql, $onClickEvent, $uniq))));
                                 break;
                             default:
                                 $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"] . " " . $extraclass], "" . $record[$fid] . "");
                                 break;
                         }
                     } else {
                         $row[$field["name"]] = "" . div(["class" => "text-" . $field["align"]], "" . $record[$fid] . "");
                     }
                 }
                 if ($rowButtons !== "") {
                     $rowButtons = str_ireplace("{" . $field["name"] . "}", $record[strtoupper($field["name"])], $rowButtons . "");
                 }
                 $rows[$rid] = $row;
             }
             if ($rowButtons !== "") {
                 $rowButtons = str_replace("{record}", rawurlencode(json_encode($record)), $rowButtons);
                 $rowButtons = str_replace("{recordid}", $rid, $rowButtons);
             }
             if ($rowButtons !== "") {
                 $rows[$rid]["BUTTONS"] = "" . div($rowButtons);
             }
         }
         $result = ["total" => $recordCount, "rows" => $rows, "sql" => $sql];
         echo json_encode($result);
     });
 }
Example #4
0
 function loadDefines()
 {
     if (!empty(Ruth::getOBJECT("DEB"))) {
         if (TINA4_HAS_CACHE) {
             $defines = unserialize(xcache_get(md5("defines")));
         }
         if (empty($defines)) {
             $defines = @Ruth::getOBJECT("DEB")->getRows("select global_name, global_value from global_setting");
             if (TINA4_HAS_CACHE) {
                 xcache_set(md5("defines"), serialize($defines));
             }
         }
         if (!empty($defines)) {
             foreach ($defines as $did => $define) {
                 define($define->GLOBAL_NAME, $define->GLOBAL_VALUE);
             }
         }
     }
 }