コード例 #1
0
ファイル: delivery.php プロジェクト: radjybaba/testserver
 /**
  * Creates a copy of all records related to the specified Delivery into the content database and disables all records in user database then releases the lock
  * @param  {int} $UID  UID of the Delivery that is being published
  * @param  {int} $user The user that is publishing the Delivery
  * @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, 'DELIVERY_BASE', $user) == false) {
         debugLog::log("<i>[delivery.php:publish_changes]</i> cannot publish Delivery (" . $UID . ") because the user is not locking it or is locked by someone else");
         return false;
     }
     // get selected Delivery
     $curr_Delivery = Delivery::get_edited_Delivery_by_UID($UID);
     if ($curr_Delivery == null) {
         debugLog::log("<i>[Delivery::publish_changes]:</i> Could not find Delivery of the base (" . $UID . ") in user");
         return false;
     }
     // disable all Delivery information
     Delivery::disable_all_Delivery_info($UID, 'content');
     // copy Delivery 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(), "DELIVERY_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() . ".DELIVERY_BASE (" . $columns_names . ") SELECT " . $columns_names . " FROM " . $dbObj->db_get_usersDB() . ".DELIVERY_BASE " . $where_sttmnt . "";
     $dbObj->run_query($dbObj->db_get_contentDB(), $query);
     // get front Delivery table name
     $front_table_name = Delivery::get_front_table_name($curr_Delivery["FRONT_TYPE"]);
     if ($front_table_name == null) {
         debugLog::log("<i>[Delivery::publish_changes]:</i> Could not find front Delivery 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 = Delivery::get_relations_tables_names();
     for ($i = 0; $i < count($links_tables_names); $i++) {
         // prepare where statement
         if ($links_tables_names[$i] == 'R_LD2D') {
             $where_sttmnt = ' (PARENT_ID = ' . $UID . ' OR CHILD_ID = ' . $UID . ') ';
         } else {
             $where_sttmnt = ' (Delivery_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
     Delivery::disable_all_Delivery_info($UID, 'user');
     // release lock off the Delivery
     if (Lock::release_lock($UID, 'DELIVERY_BASE', $user) == false) {
         debugLog::log("<i>[delivery.php:publish_changes]</i> Could not release lock off Delivery (" . $UID . ")");
         return false;
     }
     return true;
 }