db = $db; } public function filesOrderSave( $articleId, $order ): void { $i = 0; $order = explode( ';', $order ); if ( is_array( $order ) && !empty( $order ) ) foreach ( $order as $fileId ) $this->db->update( 'pp_articles_files', [ 'o' => (int)$i++ ], [ 'AND' => [ 'article_id' => $articleId, 'id' => $fileId ] ] ); } public function galleryOrderSave( $articleId, $order ): void { $i = 0; $order = explode( ';', $order ); if ( is_array( $order ) && !empty( $order ) ) foreach ( $order as $imageId ) $this->db->update( 'pp_articles_images', [ 'o' => $i++ ], [ 'AND' => [ 'article_id' => $articleId, 'id' => $imageId ] ] ); } public function additionalParams( $language = 0 ) { return $this->db->select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => $language ] ] ); } public function imageAltChange( $imageId, $imageAlt ) { $result = $this->db->update( 'pp_articles_images', [ 'alt' => $imageAlt ], [ 'id' => $imageId ] ); \S::delete_cache(); return $result; } public function articleUrl( $articleId ) { $results = $this->db->query( "SELECT seo_link FROM pp_articles_langs AS pal, pp_langs AS pl WHERE lang_id = pl.id AND article_id = " . (int)$articleId . " AND seo_link != '' ORDER BY o ASC LIMIT 1" )->fetchAll(); if ( !$results[0]['seo_link'] ) { $title = $this->articleTitle( $articleId ); return 'a-' . $articleId . '-' . \S::seo( $title ); } return $results[0]['seo_link']; } public function articlePages( $articleId ) { $pagesRepo = new \Domain\Pages\PagesRepository( $this->db ); $results = $this->db->query( "SELECT page_id FROM pp_articles_pages WHERE article_id = " . (int)$articleId )->fetchAll(); if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row ) { if ( $out == '' ) $out .= ' - '; $out .= $pagesRepo->pageTitle( $row['page_id'] ); if ( end( $results ) != $row ) $out .= ' / '; } return $out; } public function articleTitle( $articleId ) { $results = $this->db->query( "SELECT title FROM pp_articles_langs AS pal, pp_langs AS pl WHERE lang_id = pl.id AND article_id = " . (int)$articleId . " AND title != '' ORDER BY o ASC LIMIT 1" )->fetchAll(); return $results[0]['title']; } public function deleteFile( $fileId ): bool { $this->db->update( 'pp_articles_files', [ 'to_delete' => 1 ], [ 'id' => (int)$fileId ] ); return true; } public function deleteImg( $imageId ): bool { $this->db->update( 'pp_articles_images', [ 'to_delete' => 1 ], [ 'id' => (int)$imageId ] ); return true; } public function articleDetails( $articleId ) { if ( $article = $this->db->get( 'pp_articles', '*', [ 'id' => (int)$articleId ] ) ) { $results = $this->db->select( 'pp_articles_langs', '*', [ 'article_id' => (int)$articleId ] ); if ( is_array( $results ) ) foreach ( $results as $row ) $article['languages'][ $row['lang_id'] ] = $row; $article['images'] = $this->db->select( 'pp_articles_images', '*', [ 'article_id' => (int)$articleId, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] ); $article['files'] = $this->db->select( 'pp_articles_files', '*', [ 'article_id' => (int)$articleId, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] ); $article['pages'] = $this->db->select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$articleId ] ); $article['tags'] = $this->db->select( 'pp_tags', [ '[><]pp_articles_tags' => [ 'id' => 'tag_id' ] ], 'name', [ 'article_id' => (int)$articleId ] ); $article['params'] = $this->db->select( 'pp_articles_additional_values', [ 'param_id', 'value', 'language_id' ], [ 'article_id' => (int)$articleId ] ); } return $article; } public function insertMissingHash(): bool { if ( $this->db->count( 'pp_articles', [ 'hash' => null ] ) ) { $rows = $this->db->select( 'pp_articles', [ 'id', 'date_add' ], [ 'hash' => null ] ); if ( is_array( $rows ) ) foreach ( $rows as $row ) $this->db->update( 'pp_articles', [ 'hash' => md5( $row['id'] . $row['date_add'] ) ], [ 'id' => $row['id'] ] ); } return true; } public function articlesByDateAdd( $dateStart, $dateEnd ) { $results = $this->db->query( 'SELECT id FROM pp_articles WHERE status = 1 AND date_add BETWEEN \'' . $dateStart . '\' AND \'' . $dateEnd . '\' ORDER BY date_add DESC' )->fetchAll(); if ( is_array( $results ) && !empty( $results ) ) foreach ( $results as $row ) $articles[] = \front\factory\Articles::article_details( $row['id'], 'pl' ); return isset( $articles ) ? $articles : null; } public function articlesSetArchive( $articleId ) { $result = $this->db->update( 'pp_articles', [ 'status' => -1 ], [ 'id' => (int)$articleId ] ); \S::htacces(); \S::delete_cache(); return $result; } public function fileNameChange( $fileId, $fileName ): bool { $this->db->update( 'pp_articles_files', [ 'name' => $fileName ], [ 'id' => (int)$fileId ] ); return true; } public function deleteNonassignedFiles(): void { $results = $this->db->select( 'pp_articles_files', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); $this->db->delete( 'pp_articles_files', [ 'article_id' => null ] ); } public function deleteNonassignedImages(): void { $results = $this->db->select( 'pp_articles_images', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); $this->db->delete( 'pp_articles_images', [ 'article_id' => null ] ); } public function duplicateArticle( $articleId, $userId ): bool { $article = $this->articleDetails( $articleId ); if ( !$article ) return false; $this->db->insert( 'pp_articles', [ 'show_title' => $article['show_title'], 'show_date_add' => $article['show_date_add'], 'show_date_modify' => $article['show_date_modify'], 'date_add' => date( 'Y-m-d H:i:s' ), 'date_modify' => date( 'Y-m-d H:i:s' ), 'modify_by' => $userId, 'layout_id' => $article['layout_id'], 'status' => $article['status'], 'repeat_entry' => $article['repeat_entry'], 'social_icons' => $article['social_icons'], 'date_start' => $article['date_start'], 'date_end' => $article['event_date'], 'priority' => $article['priority'], 'password' => $article['password'], 'pixieset' => $article['pixieset'] ] ); $articleTmpId = $this->db->id(); if ( !$articleTmpId ) return false; foreach ( $article['languages'] as $key => $val ) $this->db->insert( 'pp_articles_langs', [ 'article_id' => $articleTmpId, 'lang_id' => $key, 'title' => 'Kopia: ' . $val['title'], 'entry' => $val['entry'], 'text' => $val['text'], 'meta_title' => null, 'meta_description' => null, 'meta_keywords' => null, 'seo_link' => null, 'copy_from' => $val['copy_from'], 'block_direct_access' => $val['block_direct_access'] ] ); foreach ( $article['params'] as $param ) $this->db->insert( 'pp_articles_additional_values', [ 'param_id' => $param['param_id'], 'value' => $param['value'], 'article_id' => $articleTmpId, 'language_id' => $param['language_id'] ] ); foreach ( $article['pages'] as $page ) { $order = $this->maxOrder() + 1; $this->db->insert( 'pp_articles_pages', [ 'article_id' => $articleTmpId, 'page_id' => $page, 'o' => (int)$order ] ); } return true; } public function articleSave( $articleId, $title, $mainImage, $entry, $text, $tableOfContents, $status, $showTitle, $showTableOfContents, $showDateAdd, $dateAdd, $showDateModify, $dateModify, $seoLink, $metaTitle, $metaDescription, $metaKeywords, $layoutId, $pages, $noindex, $repeatEntry, $copyFrom, $socialIcons, $eventDate, $tags, $blockDirectAccess, $priority, $password, $pixieset, $idAuthor, $params, $userId ) { $eventDate = explode( ' - ', $eventDate ); if ( !$articleId ) { $this->db->insert( 'pp_articles', [ 'show_title' => $showTitle == 'on' ? 1 : 0, 'show_table_of_contents' => $showTableOfContents == 'on' ? 1 : 0, 'show_date_add' => $showDateAdd == 'on' ? 1 : 0, 'show_date_modify' => $showDateModify == 'on' ? 1 : 0, 'date_add' => date( 'Y-m-d H:i:s' ), 'date_modify' => date( 'Y-m-d H:i:s' ), 'modify_by' => $userId, 'layout_id' => $layoutId ? (int)$layoutId : null, 'status' => $status == 'on' ? 1 : 0, 'repeat_entry' => $repeatEntry == 'on' ? 1 : 0, 'social_icons' => $socialIcons == 'on' ? 1 : 0, 'date_start' => $eventDate[0] ? $eventDate[0] : null, 'date_end' => $eventDate[1] ? $eventDate[1] : null, 'priority' => $priority == 'on' ? 1 : 0, 'password' => $password ? $password : null, 'pixieset' => $pixieset, 'id_author' => $idAuthor ? $idAuthor : null ] ); $id = $this->db->id(); if ( !$id ) return false; $i = 0; $results = $this->db->select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] ); if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row ) { $this->db->insert( 'pp_articles_langs', [ 'article_id' => (int)$id, 'lang_id' => $row['id'], 'title' => $title[ $i ] != '' ? $title[ $i ] : null, 'main_image' => $mainImage[$i] != '' ? $mainImage[$i] : null, 'entry' => $entry[ $i ] != '' ? $entry[ $i ] : null, 'text' => $text[ $i ] != '' ? $text[ $i ] : null, 'table_of_contents' => $tableOfContents[$i] != '' ? $tableOfContents[$i] : null, 'meta_title' => $metaTitle[ $i ] != '' ? $metaTitle[ $i ] : null, 'meta_description' => $metaDescription[ $i ] != '' ? $metaDescription[ $i ] : null, 'meta_keywords' => $metaKeywords[ $i ] != '' ? $metaKeywords[ $i ] : null, 'seo_link' => \S::seo( $seoLink[ $i ] ) != '' ? \S::seo( $seoLink[ $i ] ) : null, 'noindex' => $noindex[ $i ], 'copy_from' => $copyFrom[ $i ] != '' ? $copyFrom[ $i ] : null, 'block_direct_access' => $blockDirectAccess[ $i ] ] ); $i++; } else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row ) { $this->db->insert( 'pp_articles_langs', [ 'article_id' => (int)$id, 'lang_id' => $row['id'], 'title' => $title != '' ? $title : null, 'main_image' => $mainImage != '' ? $mainImage : null, 'entry' => $entry != '' ? $entry : null, 'text' => $text != '' ? $text : null, 'table_of_contents' => $tableOfContents != '' ? $tableOfContents : null, 'meta_title' => $metaTitle != '' ? $metaTitle : null, 'meta_description' => $metaDescription != '' ? $metaDescription : null, 'meta_keywords' => $metaKeywords != '' ? $metaKeywords : null, 'seo_link' => \S::seo( $seoLink ) != '' ? \S::seo( $seoLink ) : null, 'noindex' => $noindex, 'copy_from' => $copyFrom != '' ? $copyFrom : null, 'block_direct_access' => $blockDirectAccess ] ); } $results = $this->db->select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => 0 ] ] ); if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row ) { $this->db->insert( 'pp_articles_additional_values', [ 'param_id' => $row['id'], 'value' => $params[ 'ap_' . $row['name'] ], 'article_id' => (int)$id, 'language_id' => null ] ); } if ( is_array( $pages ) ) foreach ( $pages as $page ) { $order = $this->maxOrder() + 1; $this->db->insert( 'pp_articles_pages', [ 'article_id' => (int)$id, 'page_id' => (int)$page, 'o' => (int)$order ] ); } else if ( $pages ) { $order = $this->maxOrder() + 1; $this->db->insert( 'pp_articles_pages', [ 'article_id' => (int)$id, 'page_id' => (int)$pages, 'o' => (int)$order ] ); } $results = $this->db->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 ); } $this->db->update( 'pp_articles_files', [ 'src' => $new_file_name, 'article_id' => $id ], [ 'id' => $row['id'] ] ); } $created = false; $results = $this->db->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 ); } $this->db->update( 'pp_articles_images', [ 'src' => $new_file_name, 'article_id' => (int)$id ], [ 'id' => $row['id'] ] ); } $tags = explode( ',', $tags ); if ( is_array( $tags ) ) foreach ( $tags as $tag ) { if ( trim( $tag ) != '' ) { $tag_id = $this->db->get( 'pp_tags', 'id', [ 'name' => $tag ] ); if ( !$tag_id ) { $this->db->insert( 'pp_tags', [ 'name' => $tag ] ); $tag_id = $this->db->id(); } $this->db->insert( 'pp_articles_tags', [ 'article_id' => (int)$id, 'tag_id' => (int)$tag_id ] ); } } \S::htacces(); \S::delete_cache(); return $id; } else { $this->db->update( 'pp_articles', [ 'show_title' => $showTitle == 'on' ? 1 : 0, 'show_table_of_contents' => $showTableOfContents == 'on' ? 1 : 0, 'show_date_add' => $showDateAdd == 'on' ? 1 : 0, 'date_add' => $dateAdd, 'show_date_modify' => $showDateModify == 'on' ? 1 : 0, 'date_modify' => $dateModify ? $dateModify : date( 'Y-m-d H:i:s' ), 'modify_by' => $userId, 'layout_id' => $layoutId ? (int)$layoutId : null, 'status' => $status == 'on' ? 1 : 0, 'repeat_entry' => $repeatEntry == 'on' ? 1 : 0, 'social_icons' => $socialIcons == 'on' ? 1 : 0, 'date_start' => $eventDate[0] ? $eventDate[0] : null, 'date_end' => $eventDate[1] ? $eventDate[1] : null, 'priority' => $priority == 'on' ? 1 : 0, 'password' => $password ? $password : null, 'pixieset' => $pixieset, 'id_author' => $idAuthor ? $idAuthor : null ], [ 'id' => (int)$articleId ] ); if ( $dateAdd ) $this->db->update( 'pp_articles', [ 'date_add' => $dateAdd ], [ 'id' => (int)$articleId ] ); $i = 0; $this->db->delete( 'pp_articles_langs', [ 'article_id' => (int)$articleId ] ); $results = $this->db->select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] ); if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row ) { $this->db->insert( 'pp_articles_langs', [ 'article_id' => (int)$articleId, 'lang_id' => $row['id'], 'title' => $title[ $i ] != '' ? $title[ $i ] : null, 'main_image' => $mainImage[$i] != '' ? $mainImage[$i] : null, 'entry' => $entry[ $i ] != '' ? $entry[ $i ] : null, 'text' => $text[ $i ] != '' ? $text[ $i ] : null, 'table_of_contents' => $tableOfContents[$i] != '' ? $tableOfContents[$i] : null, 'meta_title' => $metaTitle[ $i ] != '' ? $metaTitle[ $i ] : null, 'meta_description' => $metaDescription[ $i ] != '' ? $metaDescription[ $i ] : null, 'meta_keywords' => $metaKeywords[ $i ] != '' ? $metaKeywords[ $i ] : null, 'seo_link' => \S::seo( $seoLink[ $i ] ) != '' ? \S::seo( $seoLink[ $i ] ) : null, 'noindex' => $noindex[ $i ], 'copy_from' => $copyFrom[ $i ] != '' ? $copyFrom[ $i ] : null, 'block_direct_access' => $blockDirectAccess[ $i ] ] ); $i++; } else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row ) { $this->db->insert( 'pp_articles_langs', [ 'article_id' => (int)$articleId, 'lang_id' => $row['id'], 'title' => $title != '' ? $title : null, 'main_image' => $mainImage != '' ? $mainImage : null, 'entry' => $entry != '' ? $entry : null, 'text' => $text != '' ? $text : null, 'table_of_contents' => $tableOfContents != '' ? $tableOfContents : null, 'meta_title' => $metaTitle != '' ? $metaTitle : null, 'meta_description' => $metaDescription != '' ? $metaDescription : null, 'meta_keywords' => $metaKeywords != '' ? $metaKeywords : null, 'seo_link' => \S::seo( $seoLink ) != '' ? \S::seo( $seoLink ) : null, 'noindex' => $noindex, 'copy_from' => $copyFrom != '' ? $copyFrom : null, 'block_direct_access' => $blockDirectAccess ] ); } $this->db->delete( 'pp_articles_additional_values', [ 'article_id' => (int)$articleId ] ); $results = $this->db->select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => 0 ] ] ); if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row ) { $this->db->insert( 'pp_articles_additional_values', [ 'param_id' => $row['id'], 'value' => $params[ 'ap_' . $row['name'] ], 'article_id' => (int)$articleId, 'language_id' => null ] ); } $results = $this->db->select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => 1 ] ] ); if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row ) { $results2 = $this->db->select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] ); if ( is_array( $results2 ) ) foreach ( $results2 as $row2 ) { $this->db->insert( 'pp_articles_additional_values', [ 'param_id' => $row['id'], 'value' => $params[ 'ap_' . $row['name'] . '_' . $row2['id'] ], 'article_id' => (int)$articleId, 'language_id' => $row2['id'] ] ); } } $not_in = [ 0 ]; if ( is_array( $pages ) ) foreach ( $pages as $page ) $not_in[] = $page; else if ( $pages ) $not_in[] = $pages; $this->db->delete( 'pp_articles_pages', [ 'AND' => [ 'article_id' => (int)$articleId, 'page_id[!]' => $not_in ] ] ); $pages_tmp = $this->db->select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$articleId ] ); if ( !is_array( $pages ) ) $pages = [ $pages ]; $pages = array_diff( $pages, $pages_tmp ); if ( is_array( $pages ) ) foreach ( $pages as $page ) { $order = $this->maxOrder() + 1; $this->db->insert( 'pp_articles_pages', [ 'article_id' => (int)$articleId, 'page_id' => (int)$page, 'o' => (int)$order ] ); } $results = $this->db->select( 'pp_articles_files', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { $dir = '/upload/article_files/article_' . $articleId; $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 ); } $this->db->update( 'pp_articles_files', [ 'src' => $new_file_name, 'article_id' => (int)$articleId ], [ 'id' => $row['id'] ] ); } $created = false; $results = $this->db->select( 'pp_articles_images', '*', [ 'article_id' => null ] ); if ( is_array( $results ) ) foreach ( $results as $row ) { $dir = '/upload/article_images/article_' . $articleId; $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 ); } $this->db->update( 'pp_articles_images', [ 'src' => $new_file_name, 'article_id' => (int)$articleId ], [ 'id' => $row['id'] ] ); } $results = $this->db->select( 'pp_articles_images', '*', [ 'AND' => [ 'article_id' => (int)$articleId, 'to_delete' => 1 ] ] ); if ( is_array( $results ) ) foreach ( $results as $row ) if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); $this->db->delete( 'pp_articles_images', [ 'AND' => [ 'article_id' => (int)$articleId, 'to_delete' => 1 ] ] ); $results = $this->db->select( 'pp_articles_files', '*', [ 'AND' => [ 'article_id' => (int)$articleId, 'to_delete' => 1 ] ] ); if ( is_array( $results ) ) foreach ( $results as $row ) if ( file_exists( '../' . $row['src'] ) ) unlink( '../' . $row['src'] ); $this->db->delete( 'pp_articles_files', [ 'AND' => [ 'article_id' => (int)$articleId, 'to_delete' => 1 ] ] ); $this->db->delete( 'pp_articles_tags', [ 'article_id' => (int)$articleId ] ); $tags = explode( ',', $tags ); if ( is_array( $tags ) ) foreach ( $tags as $tag ) { if ( trim( $tag ) != '' ) { $tag_id = $this->db->get( 'pp_tags', 'id', [ 'name' => $tag ] ); if ( !$tag_id ) { $this->db->insert( 'pp_tags', [ 'name' => $tag ] ); $tag_id = $this->db->id(); } $this->db->insert( 'pp_articles_tags', [ 'article_id' => (int)$articleId, 'tag_id' => (int)$tag_id ] ); } } \S::htacces(); \S::delete_cache(); return $articleId; } } public function maxOrder() { return $this->db->max( 'pp_articles_pages', 'o' ); } } ?>