getAllValuesByItem($id); $oldValues = array_column($values, 'value', 'variable'); foreach ($extraFieldValuesToSave as $key => $value) { if (is_numeric(strpos($key, 'extra_'))) { $replace = str_replace('extra_', '', $key); if (isset($oldValues[$replace])) { if ($value != $oldValues[$replace]) { $changeList[] = $replace; } } else { // It means there's a new extra field that was not included before. $changeList[] = $replace; } } } } if ($last['content'] != $content || !empty($changeList)) { $version = self::getLastVersion($language); $version++; $params = [ 'language_id' => $language, 'content' => $content, 'changes' => $changes, 'type' => $type, 'version' => $version, 'date' => $time, ]; $id = Database::insert($legalTable, $params); self::updateExtraFields($id, $extraFieldValuesToSave); return $id; } elseif ($last['type'] != $type && $language == $last['language_id']) { // Update $id = $last['id']; $params = [ 'changes' => $changes, 'type' => $type, 'date' => $time, ]; Database::update($legalTable, $params, ['id = ?' => $id]); self::updateExtraFields($id, $extraFieldValuesToSave); return $id; } return 0; } /** * @param int $itemId * @param array $values * * @return bool */ public static function updateExtraFields($itemId, $values) { if (empty($itemId)) { return false; } $extraFieldValues = new ExtraFieldValue('terms_and_condition'); $values['item_id'] = $itemId; $extraFieldValues->saveFieldValues($values); return true; } /** * @param int $id */ public static function delete($id) { /* $legalTable = Database::get_main_table(TABLE_MAIN_LEGAL); $id = (int) $id; $sql = "DELETE FROM $legalTable WHERE id = '".$id."'"; */ } /** * Gets the last version of a Term and condition by language. * * @param int $language language id * * @return int */ public static function getLastVersion($language) { $table = Database::get_main_table(TABLE_MAIN_LEGAL); $language = (int) $language; $sql = "SELECT version FROM $table WHERE language_id = $language ORDER BY id DESC LIMIT 1 "; $result = Database::query($sql); $row = Database::fetch_array($result); if (Database::num_rows($result) > 0) { return (int) $row['version']; } return 0; } /** * Gets the data of a Term and condition by language. * * @param int $language language id * * @return array all the info of a Term and condition */ public static function get_last_condition($language) { $table = Database::get_main_table(TABLE_MAIN_LEGAL); $language = (int) $language; $sql = "SELECT * FROM $table WHERE language_id = $language ORDER BY version DESC LIMIT 1 "; $result = Database::query($sql); $result = Database::fetch_array($result, 'ASSOC'); if (isset($result['content'])) { $result['content'] = self::replaceTags($result['content']); } return $result; } /** * Check if an specific version of an agreement exists. * * @param int $language * @param int $version * * @return bool */ public static function hasVersion($language, $version) { $table = Database::get_main_table(TABLE_MAIN_LEGAL); $language = (int) $language; $version = (int) $version; if (empty($language)) { return false; } $sql = "SELECT version FROM $table WHERE language_id = $language AND version = $version LIMIT 1 "; $result = Database::query($sql); if (Database::num_rows($result) > 0) { return true; } return false; } /** * @param string $content * * @return string */ public static function replaceTags($content) { if (strpos($content, '{{sessions}}')) { $sessionListToString = ''; $sessionList = SessionManager::get_sessions_by_user(api_get_user_id()); if ($sessionList) { $sessionListToString = get_lang('SessionList').'