Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Include category folder' does not create folders #9

Open
Finetuned opened this issue Mar 24, 2015 · 4 comments
Open

'Include category folder' does not create folders #9

Finetuned opened this issue Mar 24, 2015 · 4 comments

Comments

@Finetuned
Copy link

After enabling staticsaver.include_category in the system settings, folders are not automatically created on disk. If I prefix the generated filename with "foldername/", the folder is created.

Replicated on v2.2.15 and 2.3.1

It seems that the following lines in staticsaver.js are the issue as if the condition is true only the chunk name is used:

if (staticFile.getValue() == '') {
            setValue(staticFile, nameInput.getAttribute('value'));
        } else {
            getRequest();
        }
@Finetuned
Copy link
Author

I suspect It's because there's no recursive lookup on the category; creating a path with two or more folders works to create the folder but reloading the chunk truncates the path to the immediate category name.

@playcock
Copy link

Replicated issue on v2.5.7

@playcock
Copy link

playcock commented Oct 13, 2017

It's not an elegant solution, but it is a solution that works for my site and allows me to have the nested directory paths preserved as I have them set for the respective category nesting in my Elements tab.

I updated the staticsaver.js file in order to help me find out if the category has any parent categories. If the element has a category, then I send the category to a processor file on my site to write the category_folder variable in the staticsaver.js with what is returned from the processor.

The processor file only goes a total of 4 levels deep, including the immediate category.

getRequest function StaticSaver-master/assets/components/staticsaver/js/staticsaver.js

var getRequest = function() {
	var catval = categoryInput.getValue();
	var category_path = false;
	Ext.Ajax.request({
		url:  MODx.config['assets_url'] + 'components/staticsaver/connector.php',
		success: function(e){
			var response = JSON.parse(e.responseText);
			var value = response.result;
			if (MODx.config['staticsaver.enable_rewrite'] || value == '1') {
				setValue(staticFile, nameInput.getAttribute('value'));
			}
			var category;
			var category_folder = "";
			if( catval != "" ) {
				category = response.category;
				category_folder = category.toLowerCase().replace(/ /g,"_")+"/";
			}			
			if (MODx.config['staticsaver.include_category'] == "1") {
				var lookup_category_tree = typeof category != 'undefined' ? true : false;
				// the elemnt has a category can be used to find out if the category has any parents
				if ( lookup_category_tree) {
					Ext.Ajax.request({
						url: 'your-processor-url.php/get-static-saver-category-tree.html?category=' + category,
						success: function(e){
							var result = JSON.parse(e.responseText);
							
							if ( result.category_path == '' ) { // the path never got set in the processor and returned false
								setValue(staticFile, category_folder+nameInput.getAttribute('value'));
								console.log('01 the directory has no parent');
							} else { // the path has been set in the processor and has been returned
								category_path = result.category_path;
								category_folder = category_path.replace(/ /g,"_");
								setValue(staticFile, category_folder+nameInput.getAttribute('value'));
								console.log('the directory has parent(s)');
							}
							
						}
					});
				} else { // the elemnt does not have a category, default to category_path setting to staticsaver's default setting
					setValue(staticFile, category_folder+nameInput.getAttribute('value'));
					console.log('02 the directory has no parent');
				}
			}
		},
		params: {
			action: 'check',
			type : config.type,
			id: MODx.request.id,
			source: sourceInput.getValue(),
			category: categoryInput.getValue(),
			static_file: staticFile.getValue()
		}
	});
}

processor PHP code

<?php
$immediate_category_get = isset( $_GET['category'] ) ? $_GET['category'] : false;
/*
 function returns the nested levels of a category's hierarchy
 ## Only goes 4 levels deep ##
*/
$category_hierarchy = array(
	'category_hierarchy' => false,
	'immediate_category' => false,
	'parent_category' => false,
	'grandparent_category' => false,
	'greatgrandparent_category' => false,
	'category_path' => false,
	
);
if ( $immediate_category_get ) {
	$category_hierarchy['category_hierarchy'] = true;
	$category_hierarchy['immediate_category'] = $immediate_category_get . '/';
	// queries immediate category
	$immediate_category_result = $modx->query("SELECT * FROM modx_categories WHERE category like '" . $immediate_category_get . "'");
	$immediate_category = $immediate_category_result->fetch(PDO::FETCH_ASSOC);
	// checks for parent category and queries that if parent is found
	$immediate_category_parent_id = ! empty( $immediate_category['parent'] ) && $immediate_category['parent'] > 0 ? $immediate_category['parent'] : false;
	if ( $immediate_category_parent_id ) {
		$immediate_category_parent_result = $modx->query("SELECT * FROM modx_categories WHERE id = " . $immediate_category_parent_id);
		$immediate_category_parent = $immediate_category_parent_result->fetch(PDO::FETCH_ASSOC);
		$category_hierarchy['parent_category'] = $immediate_category_parent['category'] .'/';
	}
	// checks for grandparent category and queries that if grandparent is found
	$immediate_category_grandparent_id = ! empty( $immediate_category_parent['parent'] ) && $immediate_category_parent['parent'] > 0 ? $immediate_category_parent['parent'] : false;
	if ( $immediate_category_grandparent_id ) {
		$immediate_category_grandparent_result = $modx->query("SELECT * FROM modx_categories WHERE id = " . $immediate_category_grandparent_id);
		$immediate_category_grandparent = $immediate_category_grandparent_result->fetch(PDO::FETCH_ASSOC);
		$category_hierarchy['grandparent_category'] = $immediate_category_grandparent['category'] .'/';
	}
	// checks for greatgrandparent category and queries that if grandparent is found
	$immediate_category_greatgrandparent_id = ! empty( $immediate_category_grandparent['parent'] ) && $immediate_category_grandparent['parent'] > 0 ? $immediate_category_grandparent['parent'] : false;
	if ( $immediate_category_greatgrandparent_id ) {
		$immediate_category_greatgrandparent_result = $modx->query("SELECT * FROM modx_categories WHERE id = " . $immediate_category_greatgrandparent_id);
		$immediate_category_greatgrandparent = $immediate_category_greatgrandparent_result->fetch(PDO::FETCH_ASSOC);
		$category_hierarchy['greatgrandparent_category'] = $immediate_category_greatgrandparent['category'] .'/';
	}
	
	$category_hierarchy['category_path'] = $category_hierarchy['greatgrandparent_category'] . $category_hierarchy['grandparent_category'] . $category_hierarchy['parent_category'] . $category_hierarchy['immediate_category'];
}
echo json_encode( $category_hierarchy );
?>

@playcock
Copy link

I am finding that I have to save the element twice for the above code to work as expected because the initial save store the file in the root of the chunks directory. Subsequent saves to the element does create the nested directory structure desired and save a new file there, but you still have the original file save in the root of the chunk directory. So, you have to delete the file created from the initial save to not have duplicates on your server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants