Ejemplo n.º 1
0
 /**
  * Creates a copy of all records related to the specified Kbit into the content database and disables all records in user database then releases the lock
  * @param  {int} $UID  UID of the kbit that is being published
  * @param  {int} $user The user that is publishing the kbit
  * @return {bool}       true on success false otherwise
  */
 public static function publish_changes($UID, $user)
 {
     // add new record in content database for each data record and disable all data records in user database
     if (Lock::is_locked_by_user($UID, 'KBIT_BASE', $user) == false) {
         debugLog::log("<i>[Kbits.php:publish_changes]</i> cannot publish Kbit (" . $UID . ") because the user is not locking it or is locked by someone else");
         return false;
     }
     // get selected Kbit
     $curr_kbit = Kbit::get_edited_kbit_by_UID($UID);
     if ($curr_kbit == null) {
         debugLog::log("<i>[Kbit::publish_changes]:</i> Could not find Kbit of the base (" . $UID . ") in user");
         return false;
     }
     // disable all kbit information
     Kbit::disable_all_kbit_info($UID, 'content');
     // copy kbit from user database into content database
     $dbObj = new dbAPI();
     $where_sttmnt = ' WHERE UID = ' . $UID . ' AND ENABLED = 1 ';
     // get columns names
     $columns_names = $dbObj->db_get_columns_names($dbObj->db_get_contentDB(), "KBIT_BASE", true);
     // remove primary key (auto-increment) column (id)
     $columns_names = str_replace("id,", "", $columns_names);
     // copy record from user to content
     $query = "INSERT INTO " . $dbObj->db_get_contentDB() . ".KBIT_BASE (" . $columns_names . ") SELECT " . $columns_names . " FROM " . $dbObj->db_get_usersDB() . ".KBIT_BASE " . $where_sttmnt . "";
     $dbObj->run_query($dbObj->db_get_contentDB(), $query);
     // get front kbit table name
     $front_table_name = Kbit::get_front_table_name($curr_kbit["FRONT_TYPE"]);
     if ($front_table_name == null) {
         debugLog::log("<i>[Kbit::publish_changes]:</i> Could not find front kbit table name (" . $UID . ")");
         // roll back option here
         return false;
     }
     // get columns names
     $columns_names = $dbObj->db_get_columns_names($dbObj->db_get_contentDB(), $front_table_name, true);
     // remove primary key (auto-increment) column (id)
     $columns_names = str_replace("id,", "", $columns_names);
     // copy front record from user to content
     $query = "INSERT INTO " . $dbObj->db_get_contentDB() . "." . $front_table_name . " (" . $columns_names . ") SELECT " . $columns_names . " FROM " . $dbObj->db_get_usersDB() . "." . $front_table_name . " " . $where_sttmnt . "";
     $dbObj->run_query($dbObj->db_get_contentDB(), $query);
     // loop over links and copy records from user to content
     $links_tables_names = Kbit::get_relations_tables_names();
     for ($i = 0; $i < count($links_tables_names); $i++) {
         // prepare where statement
         if ($links_tables_names[$i] == 'R_LK2K') {
             $where_sttmnt = ' (PARENT_ID = ' . $UID . ' OR CHILD_ID = ' . $UID . ') ';
         } else {
             $where_sttmnt = ' (KBIT_BASE_ID = ' . $UID . ') ';
         }
         // get columns names
         $columns_names = $dbObj->db_get_columns_names($dbObj->db_get_contentDB(), $links_tables_names[$i], true);
         // remove primary key (auto-increment) column (id)
         $columns_names = str_replace("id,", "", $columns_names);
         // get all relevant relations
         $query = "SELECT id FROM " . $links_tables_names[$i] . " where " . $where_sttmnt . " AND ENABLED = '1'";
         $relations = $dbObj->db_select_query($dbObj->db_get_usersDB(), $query);
         // loop over all records and insert them
         for ($j = 0; $j < count($relations); $j++) {
             $where_sttmnt = ' id =' . $relations[$j]["id"] . ' ';
             $query = "INSERT INTO " . $dbObj->db_get_contentDB() . "." . $links_tables_names[$i] . " (" . $columns_names . ") SELECT " . $columns_names . " FROM " . $dbObj->db_get_usersDB() . "." . $links_tables_names[$i] . " where " . $where_sttmnt . "";
             $dbObj->run_query($dbObj->db_get_contentDB(), $query);
         }
     }
     // remove copies from user database
     Kbit::disable_all_kbit_info($UID, 'user');
     // release lock off the Kbit
     if (Lock::release_lock($UID, 'KBIT_BASE', $user) == false) {
         debugLog::log("<i>[Kbits.php:publish_changes]</i> Could not release lock off kbit (" . $UID . ")");
         return false;
     }
     return true;
 }