public static function import_createedit($tfolder, $module, $username, $lastsync, $fields) { $table_source = "fnbl_simple_" . $module . "_imp"; $table_dest = "simple_" . $module; $sys_date = date("Y-m-d H:i:s"); $db_date = sgsml_parser::sql_date(); if (abs(strtotime($sys_date) - strtotime($db_date)) > 60) { sys_warning("{t}Error{/t}: {t}current time{/t} {t}System{/t}: " . $sys_date . " {t}Database{/t}: " . $db_date); } if (DEBUG) { echo "Sync4j: " . $table_source . " lastmodified > " . $lastsync . " " . date("c", $lastsync); } $count_insert = 0; $count_update = 0; $rows = db_select($table_source, "*", array("userid=@username@", "lastmodified > @lastmodified@"), "", "", array("username" => $username, "lastmodified" => $lastsync - 600)); if (is_array($rows) and count($rows) > 0) { foreach ($rows as $row) { if ($row["status"] == "D") { // delete self::_import_delete($tfolder, $row["syncid"], $table_dest, $module); continue; } unset($row["userid"]); unset($row["status"]); if ($table_dest == "simple_contacts" and empty($row["contactid"])) { continue; } if ($table_dest == "simple_tasks") { if (empty($row["begin"]) and empty($row["ending"])) { continue; } if (empty($row["begin"])) { $row["begin"] = $row["ending"]; } } $exists = db_select_value($table_dest, "id", "syncid=@id@", array("id" => $row["syncid"])); if (!empty($exists)) { $id = $exists; } else { $id = 0; } if ($id != 0) { // update $row["history"] = sprintf("{t}Item edited (%s) by %s at %s{/t} (sync)\n", "@fields@", $_SESSION["username"], sys_date("{t}m/d/y g:i:s a{/t}")); $cdata = ""; $data = $row; $cfields = array(); $data_old = db_select_first($table_dest, "*", "id=@id@", "", array("id" => $id)); if (!empty($data_old["id"])) { if ($row["lastmodified"] == $data_old["lastmodified"]) { continue; } foreach ($data as $key => $val) { if (isset($data_old[$key]) and $key != "history") { if ($data_old[$key] != $val) { if (trim($val) != "") { $cdata .= $key . ": " . $val . "\n"; } $cfields[] = $key; } else { unset($data[$key]); } } } } if (count($data) < 3) { continue; } $data["history"] = str_replace("@fields@", implode(", ", $cfields), $data["history"]) . $cdata . "\n"; if (DEBUG) { print_r($data); } $error_sql = db_update($table_dest, $data, array("id=@id@"), array("id" => $id)); $count_update++; } else { // new $id = sql_genID($table_dest) * 100; $row["id"] = $id; $row["folder"] = $tfolder; $row["dsize"] = 0; $row["history"] = sprintf("{t}Item created by %s at %s{/t} (sync)\n", $_SESSION["username"], sys_date("{t}m/d/y g:i:s a{/t}")); if (DEBUG) { print_r($row); } $error_sql = db_insert($table_dest, $row); $count_insert++; } if ($error_sql == "") { if ($module == "calendar") { trigger::calcappointment($id, $row, false, "simple_calendar"); } if ($module == "tasks") { trigger::duration($id, $row, false, "simple_tasks"); } trigger::notify($id, $row, array(), "simple_" . $module); db_search_update($table_dest, $id, $fields); if ($count_insert > 0) { sys_log_stat("new_records", $count_insert); } if ($count_update > 0) { sys_log_stat("changed_records", $count_update); } } } } db_update_treesize($table_dest, $tfolder); return ""; }
static function database_triggers() { // 0.664 if (!file_exists(SIMPLE_STORE . "/setup_emails")) { setup::out(sprintf("{t}Processing %s ...{/t}", "emails message")); $rows = db_select("simple_emails", "*", array("message_html='' and message!=''"), "", ""); if (is_array($rows) and count($rows) > 0) { foreach ($rows as $row) { trigger::createemail($row["id"], $row); } } touch(SIMPLE_STORE . "/setup_emails"); } // 0.704 if (!file_exists(SIMPLE_STORE . "/setup_notify")) { $notifications = array("simple_tasks" => "closed='0'", "simple_contacts" => "birthday!=''", "simple_contactactivities" => "finished='0'", "simple_sys_users" => "activated='1'"); foreach ($notifications as $table => $where) { setup::out(sprintf("{t}Processing %s ...{/t}", $table)); $rows = db_select($table, "*", array($where, "notification!=''"), "", ""); if (!is_array($rows) or count($rows) == 0) { continue; } foreach ($rows as $row) { trigger::notify($row["id"], $row, array(), $table); } } touch(SIMPLE_STORE . "/setup_notify"); } if (!file_exists(SIMPLE_STORE . "/setup_duration")) { setup::out(sprintf("{t}Processing %s ...{/t}", "tasks duration")); $rows = db_select("simple_tasks", "*", array(), "", ""); if (is_array($rows) and count($rows) > 0) { foreach ($rows as $row) { trigger::duration($row["id"], $row, false, "simple_tasks"); } } setup::out(sprintf("{t}Processing %s ...{/t}", "projects duration")); $rows = db_select("simple_projects", "*", array(), "", ""); if (is_array($rows) and count($rows) > 0) { foreach ($rows as $row) { trigger::createeditproject($row["id"], $row); } } touch(SIMPLE_STORE . "/setup_duration"); } setup::out(sprintf("{t}Processing %s ...{/t}", "appointments")); $rows = db_select("simple_calendar", "*", array(), "", ""); if (is_array($rows) and count($rows) > 0) { foreach ($rows as $row) { trigger::calcappointment($row["id"], $row, null, "simple_calendar"); } } }