update( 'pp_articles_images', [ 'o' => $i++ ], [ 'AND' => [ 'article_id' => $article_id, 'id' => $image_id ] ] ); } return true; } public static function image_alt_change( $image_id, $image_alt ) { global $mdb; $result = $mdb -> update( 'pp_articles_images', [ 'alt' => $image_alt ], [ 'id' => $image_id ] ); \S::delete_cache(); return $result; } public static function article_url( $article_id ) { global $mdb; $results = $mdb -> query( "SELECT seo_link FROM pp_articles_langs AS pal, pp_langs AS pl WHERE lang_id = pl.id AND article_id = " . (int)$article_id . " AND seo_link != '' ORDER BY o ASC LIMIT 1" ) -> fetchAll(); if ( !$results[0]['seo_link'] ) { $title = self::article_title( $article_id ); return 'a-' . $article_id . '-' . \S::seo( $title ); } else return $results[0]['seo_link']; } public static function articles_by_date_add( $date_start, $date_end ) { global $mdb; $results = $mdb -> query( 'SELECT ' . 'id ' . 'FROM ' . 'pp_articles ' . 'WHERE ' . 'status = 1 ' . 'AND ' . 'date_add BETWEEN \'' . $date_start . '\' AND \'' . $date_end . '\' ' . 'ORDER BY ' . 'date_add DESC' ) -> fetchAll(); if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row ) $articles[] = \front\factory\Articles::article_details( $row['id'], 'pl' ); return $articles; } public static function article_pages( $article_id ) { global $mdb; $results = $mdb -> query( "SELECT page_id FROM pp_articles_pages WHERE article_id = " . (int)$article_id ) -> fetchAll(); if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row ) { if ( $out == '' ) $out .= ' - '; $out .= \admin\factory\Pages::page_title( $row['page_id'] ); if ( end( $results ) != $row ) $out .= ' / '; } return $out; } public static function article_title( $article_id ) { global $mdb; $results = $mdb -> query( "SELECT title FROM pp_articles_langs AS pal, pp_langs AS pl WHERE lang_id = pl.id AND article_id = " . (int)$article_id . " AND title != '' ORDER BY o ASC LIMIT 1" ) -> fetchAll(); return $results[0]['title']; } public static function articles_set_archive( $article_id ) { global $mdb; return $mdb -> update( 'pp_articles', [ 'status' => -1 ], [ 'id' => (int)$article_id ] ); } public static function file_name_change( $file_id, $file_name ) { global $mdb; $mdb -> update( 'pp_articles_files', [ 'name' => $file_name ], [ 'id' => (int)$file_id ] ); return true; } public static function delete_file( $file_id ) { global $mdb; $mdb -> update( 'pp_articles_files', [ 'to_delete' => 1 ], [ 'id' => (int)$file_id ] ); return true; } public static function delete_img( $image_id ) { global $mdb; $mdb -> update( 'pp_articles_images', [ 'to_delete' => 1 ], [ 'id' => (int)$image_id ] ); return true; } public static function article_details( $article_id ) { global $mdb; if ( $article = $mdb -> get( 'pp_articles', '*', [ 'id' => (int)$article_id ] ) ) { $results = $mdb -> select( 'pp_articles_langs', '*', [ 'article_id' => (int)$article_id ] ); if ( is_array( $results ) ) foreach ( $results as $row ) $article['languages'][ $row['lang_id'] ] = $row; $article['images'] = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => (int)$article_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'DESC' ] ] ); $article['files'] = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => (int)$article_id ] ); $article['pages'] = $mdb -> select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$article_id ] ); } return $article; } public static function max_order() { global $mdb; return $mdb -> max( 'pp_articles_pages', 'o' ); } public static function article_save( $article_id, $title, $main_image, $entry, $text, $table_of_contents, $status, $show_title, $show_table_of_contents, $show_date_add, $date_add, $show_date_modify, $date_modify, $seo_link, $meta_title, $meta_description, $meta_keywords, $layout_id, $pages, $noindex, $repeat_entry, $copy_from, $social_icons, $block_direct_access ) { global $mdb, $user; if ( !$article_id ) { $mdb -> insert( 'pp_articles', [ 'show_title' => $show_title == 'on' ? 1 : 0, 'show_date_add' => $show_date_add == 'on' ? 1 : 0, 'show_date_modify' => $show_date_modify == 'on' ? 1 : 0, 'date_add' => date( 'Y-m-d H:i:s' ), 'date_modify' => date( 'Y-m-d H:i:s' ), 'modify_by' => $user['id'], 'layout_id' => $layout_id ? (int)$layout_id : null, 'status' => $status == 'on' ? 1 : 0, 'repeat_entry' => $repeat_entry == 'on' ? 1 : 0, 'social_icons' => $social_icons == 'on' ? 1 : 0, 'show_table_of_contents' => $show_table_of_contents == 'on' ? 1 : 0, ] ); $id = $mdb -> id(); if ( $id ) { foreach ( $title as $key => $val ) { $mdb -> insert( 'pp_articles_langs', [ 'article_id' => (int)$id, 'lang_id' => $key, 'title' => $title[$key] != '' ? $title[$key] : null, 'main_image' => $main_image[$key] != '' ? $main_image[$key] : null, 'entry' => $entry[$key] != '' ? $entry[$key] : null, 'text' => $text[$key] != '' ? $text[$key] : null, 'table_of_contents' => $table_of_contents[$key] != '' ? $table_of_contents[$key] : null, 'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null, 'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null, 'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null, 'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null, 'noindex' => $noindex[$key] == 'on' ? 1 : 0, 'copy_from' => $copy_from[$key] != '' ? $copy_from[$key] : null, 'block_direct_access' => $block_direct_access[$key] == 'on' ? 1 : 0 ] ); } if ( is_array( $pages ) ) foreach ( $pages as $page ) { $order = self::max_order() + 1; $mdb -> insert( 'pp_articles_pages', [ 'article_id' => (int)$id, 'page_id' => (int)$page, 'o' => (int)$order ] ); } else if ( $pages ) { $order = self::max_order() + 1; $mdb -> insert( 'pp_articles_pages', [ 'article_id' => (int)$id, 'page_id' => (int)$pages, 'o' => (int)$order ] ); } $results = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { $dir = '/upload/article_files/article_' . $id; $new_file_name = str_replace( '/upload/article_files/tmp', $dir, $row['src'] ); if ( file_exists( '..' . $row['src'] ) ) { if ( !is_dir( '../' . $dir ) and $created !== true ) { if ( mkdir( '../' . $dir, 0755, true ) ) $created = true; } rename( '..' . $row['src'], '..' . $new_file_name ); } $mdb -> update( 'pp_articles_files', [ 'src' => $new_file_name, 'article_id' => $id ], [ 'id' => $row['id'] ] ); } $created = false; /* zdjęcia */ $results = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { $dir = '/upload/article_images/article_' . $id; $new_file_name = str_replace( '/upload/article_images/tmp', $dir, $row['src'] ); if ( file_exists( '../' . $new_file_name ) ) { $ext = strrpos( $new_file_name, '.' ); $fileName_a = substr( $new_file_name, 0, $ext ); $fileName_b = substr( $new_file_name, $ext ); $count = 1; while ( file_exists( '../' . $fileName_a . '_' . $count . $fileName_b ) ) $count++; $new_file_name = $fileName_a . '_' . $count . $fileName_b; } if ( file_exists( '..' . $row['src'] ) ) { if ( !is_dir( '../' . $dir ) and $created !== true ) { if ( mkdir( '../' . $dir, 0755, true ) ) $created = true; } rename( '..' . $row['src'], '..' . $new_file_name ); } $mdb -> update( 'pp_articles_images', [ 'src' => $new_file_name, 'article_id' => (int)$id ], [ 'id' => $row['id'] ] ); } \S::htacces(); \S::delete_dir( '../temp/' ); return $id; } } else { $mdb -> update( 'pp_articles', [ 'show_title' => $show_title == 'on' ? 1 : 0, 'show_date_add' => $show_date_add == 'on' ? 1 : 0, 'date_add' => $date_add, 'show_date_modify' => $show_date_modify == 'on' ? 1 : 0, 'date_modify' => $date_modify, 'modify_by' => $user['id'], 'layout_id' => $layout_id ? (int)$layout_id : null, 'status' => $status == 'on' ? 1 : 0, 'repeat_entry' => $repeat_entry == 'on' ? 1 : 0, 'social_icons' => $social_icons == 'on' ? 1 : 0, 'show_table_of_contents' => $show_table_of_contents == 'on' ? 1 : 0, ], [ 'id' => (int)$article_id ] ); foreach ( $title as $key => $val ) { if ( $translation_id = $mdb -> get( 'pp_articles_langs', 'id', [ 'AND' => [ 'article_id' => $article_id, 'lang_id' => $key ] ] ) ) $mdb -> update( 'pp_articles_langs', [ 'lang_id' => $key, 'title' => $title[$key] != '' ? $title[$key] : null, 'main_image' => $main_image[$key] != '' ? $main_image[$key] : null, 'entry' => $entry[$key] != '' ? $entry[$key] : null, 'text' => $text[$key] != '' ? $text[$key] : null, 'table_of_contents' => $table_of_contents[$key] != '' ? $table_of_contents[$key] : null, 'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null, 'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null, 'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null, 'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null, 'noindex' => $noindex[$key] == 'on' ? 1 : 0, 'copy_from' => $copy_from[$key] != '' ? $copy_from[$key] : null, 'block_direct_access' => $block_direct_access[$key] == 'on' ? 1 : 0 ], [ 'id' => $translation_id ] ); else $mdb -> insert( 'pp_articles_langs', [ 'article_id' => (int)$article_id, 'lang_id' => $key, 'title' => $title[$key] != '' ? $title[$key] : null, 'main_image' => $main_image[$key] != '' ? $main_image[$key] : null, 'entry' => $entry[$key] != '' ? $entry[$key] : null, 'text' => $text[$key] != '' ? $text[$key] : null, 'table_of_contents' => $table_of_contents[$key] != '' ? $table_of_contents[$key] : null, 'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null, 'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null, 'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null, 'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null, 'noindex' => $noindex[$key] == 'on' ? 1 : 0, 'copy_from' => $copy_from[$key] != '' ? $copy_from[$key] : null, 'block_direct_access' => $block_direct_access[$key] == 'on' ? 1 : 0 ] ); } $not_in = [ 0 ]; if ( is_array( $pages ) ) foreach ( $pages as $page ) $not_in[] = $page; else if ( $pages ) $not_in[] = $pages; $mdb -> delete( 'pp_articles_pages', [ 'AND' => [ 'article_id' => (int)$article_id, 'page_id[!]' => $not_in ] ] ); $pages_tmp = $mdb -> select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$article_id ] ); if ( !is_array( $pages ) ) $pages = [ $pages ]; $pages = array_diff( $pages, $pages_tmp ); if ( is_array( $pages ) ) foreach ( $pages as $page ) { $order = self::max_order() + 1; $mdb -> insert( 'pp_articles_pages', [ 'article_id' => (int)$article_id, 'page_id' => (int)$page, 'o' => (int)$order ] ); } $results = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { $dir = '/upload/article_files/article_' . $article_id; $new_file_name = str_replace( '/upload/article_files/tmp', $dir, $row['src'] ); if ( file_exists( '..' . $row['src'] ) ) { if ( !is_dir( '../' . $dir ) and $created !== true ) { if ( mkdir( '../' . $dir, 0755, true ) ) $created = true; } rename( '..' . $row['src'], '..' . $new_file_name ); } $mdb -> update( 'pp_articles_files', [ 'src' => $new_file_name, 'article_id' => (int)$article_id ], [ 'id' => $row['id'] ] ); } $created = false; /* zdjęcia */ $results = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { $dir = '/upload/article_images/article_' . $article_id; $new_file_name = str_replace( '/upload/article_images/tmp', $dir, $row['src'] ); if ( file_exists( '../' . $new_file_name ) ) { $ext = strrpos( $new_file_name, '.' ); $fileName_a = substr( $new_file_name, 0, $ext ); $fileName_b = substr( $new_file_name, $ext ); $count = 1; while ( file_exists( '../' . $fileName_a . '_' . $count . $fileName_b ) ) $count++; $new_file_name = $fileName_a . '_' . $count . $fileName_b; } if ( file_exists( '..' . $row['src'] ) ) { if ( !is_dir( '../' . $dir ) and $created !== true ) { if ( mkdir( '../' . $dir, 0755, true ) ) $created = true; } rename( '..' . $row['src'], '..' . $new_file_name ); } $mdb -> update( 'pp_articles_images', [ 'src' => $new_file_name, 'article_id' => (int)$article_id ], [ 'id' => $row['id'] ] ); } $results = $mdb -> select( 'pp_articles_images', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); } $mdb -> delete( 'pp_articles_images', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] ); $results = $mdb -> select( 'pp_articles_files', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); } $mdb -> delete( 'pp_articles_files', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] ); \S::htacces(); \S::delete_dir( '../temp/' ); return $article_id; } } public static function delete_nonassigned_files() { global $mdb; $results = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); } $mdb -> delete( 'pp_articles_files', [ 'article_id' => null ] ); } public static function delete_nonassigned_images() { global $mdb; $results = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); } $mdb -> delete( 'pp_articles_images', [ 'article_id' => null ] ); } } ?>