Bogiecom.com
  • About
  • Magento
  • Sitecore
  • WordPress

Author Archives: bc-admin

Tree Menu Traversal in C# Queryable

Posted on January 19, 2017 by bc-admin

Using C# to query entire tree system of pages looking for a particular attribute.  Normally I would use a recursive technique, but then found this to be easier and more readable.  Not sure how it is on performance, I think it’s supposed to be better. Found it here http://stackoverflow.com/questions/7062882/searching-a-tree-using-linq#answer-7063002

Here’s how I implmented

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    public class MenuItem
    {
        public virtual string NavigationTitle { get; set; }
        public virtual string Url { get; set; }
        public virtual bool ShowInMainNavigation { get; set; }
        public virtual bool ShowInFooterNavigation { get; set; }
        public virtual IEnumerable Children { get; set; }
        public virtual string TwitterHandle { get; set; }
        public virtual string FacebookHandle { get; set; }
        public virtual string InstagramHandle { get; set; }
 
        public IEnumerable GetDescendants(MenuItem root)
        {
 
            var nodes = new Stack(new[] { root });
            while (nodes.Any())
            {
                MenuItem node = nodes.Pop();
                yield return node;
                foreach (var n in node.Children) nodes.Push(n);
            }
 
        }
    }

Here’s how I used in Razor:

1
2
3
4
5
6
<ul>
@foreach (YourSite.Models.Common.MenuItem navItem in Model.GetDescendants(Model).Where(x => x.ShowInFooterNavigation == true))
{
@Html.Partial("~/Views/Shared/NavLink.cshtml", navItem)
}
</ul>

Posted in .NET, C#, Sitecore |

Quick Way to Archive a WordPress Site

Posted on December 16, 2013 by bc-admin

So if you need a quick way to archive your WordPress install and you have shell access through SSH, this script could help you do that quickly.  You will need to make sure that ‘zip’ is installed (of course you could also modify the script below to use something like gzip or bz2 or whatever).

So the steps to getting this to work are:

  1. Upload the script below to the server as “wordpress-arch.sh”
  2. Make it executable by running “chmod +x wordpress-arch.sh”
  3. Execute the script by running “./wordpress-arch.sh” or “/bin/sh /path/to/script/wordpress-arch.sh”
  4. Specify a file name without the zip extension
  5. Then provide the full path to the WordPress root folder

The script will then copy all of the existing files to a directory with the same name as the file name you specified.  Next it will find the wp-config.php and find the database connection info.  Then runs mysqldump and saves a .sql to the root of the copied files. Zips up that directory to a zip archive in the same directory the script was executed from, and deletes the copy of the wordpress installation.

Some assumptions it makes is that you want to put the database dump in the root of the archive, and that you want to clean out your database username and password before passing it to someone else for install.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# !bin/bash
# wordpress-arch.sh
# run 'chmod +x wordpress-arch.sh' to allow for execution
# execute by running './wordpress-arch.sh' or '/bin/sh wordpress-arch.sh'
 
# get unknown info
read -p "Enter final archive file name without the extension (.zip) " archPath
read -e -p "Enter the full path to the root of the wordpress installation " indir
 
# find database information
WPDBNAME=`cat $indir/wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat $indir/wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat $indir/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
 
# echo the Wordpress config info
echo "Found this database information"
echo "DBNAME $WPDBNAME"
echo "DBUSER $WPDBUSER"
echo "DBPASS $WPDBPASS"
 
# make a new directory for the migration
mkdir $archPath
 
# copy all of the files to a new directory
cp -rfv $indir/* $archPath
 
# dump database using the found wordpress database settings
mysqldump -u$WPDBUSER -p$WPDBPASS $WPDBNAME > "$archPath/$WPDBNAME.sql"
 
# get rid of Wordpress Database User & Password before zipping to archive
sed -ie "s/$WPDBUSER//gI" $archPath/wp-config.php
sed -ie "s/$WPDBPASS//gI" $archPath/wp-config.php
 
# zip up all of the files
zip -r "$archPath.zip" $archPath
 
# remove the copied files
rm -rf $archPath

I know, I know… pretty basic. However, it’s something I needed, and maybe just maybe someone else does as well. I might change this up to make it an automated migration script that could deploy to a remote server, install all of the files, import the database, and setup an Apache host. But for now, this is all I need. 🙂

Posted in Bash, Linux, Shell Scripting, Wordpress |

SED find and replace string, then revert backup.

Posted on November 14, 2013 by bc-admin

So testing a large change of adding a virtual include to all of the files in a website. Mainly for adding analytics tracking beneath the body tag for Google Tag Manager/Universal Analytics.

Making the change in all files with the .php extension

1
find ./ -type f -name "*.php" -exec sed -i.bak 's//\n<!-- #include virtual="includes/analytics-universal.php" -->/g' {} \;

I’m not happy with what came about, so now I want to revert the change

1
find ./ -name "*.bak" -exec sh -c 'mv -f $0 ${0%.bak}' {} \;

If you want to keep your changes, run this to get rid of the .bak files

1
find ./ -name "*.bak" -exec sh -c 'rm -f $0' {} \;

Posted in Linux, Sed, Shell Scripting | Leave a comment |

Magento Top Navigation Sort Alphabetically

Posted on February 9, 2013 by bc-admin

If you are having a little trouble getting the top navigation to ignore the sort order and use alphabetical sort, here is one option you can use…

Consider the following code for a navigation menu…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
   <ul id="custom-menu">
    <?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories(true, true, false) ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul style="width:940px;">
        <?php foreach($_categories as $_category): ?>
            <li>
                <b><a class="drop" href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a></b>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
 
                <?php
       $collection = $_category->getCollection();
                    $_subcategories = $_category->getChildrenCategories() ?>
                
                <?php if (count($_subcategories) > 0): ?>
                    <div class="dropdown_1column" style="width:235px;">
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <div class="col_1">
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </div>
                        <?php endforeach; ?>
                        <div class="clr" clear="all"></div>
                    </div>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>        
        </ul>                          
              
<?php endif; ?>

The problem is when you get down to the secondary level where it you use the getChildrenCategories(). This function only seems to respect sort order and doesn’t seem to have an option to sort by name. So if you look at the first call for the top level categories $_helper->getStoreCategories(true, true, false). The first parameter tells that section to sort by name.

Seeing as this only traverses the top level, the remaining subcategories are left unchanged. After searching all of the forums for a way around it, some people suggested changing the core file /Mage/Catalog/Model/Resource/Category.php and directly editing the getChildrenCategories() function. While this works… the best option would be to copy the contents of that function directly into the template, to keep it upgrade safe.

So this what that would like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    <ul id="custom-menu">
    <?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories(true, true, false) ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul style="width:940px;">
        <?php foreach($_categories as $_category): ?>
            <li>
                <b><a class="drop" href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a></b>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
 
                <?php
$collection = $_category->getCollection();
 
$_subcategories = $collection
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('name','ASC')
->addIdFilter($_category->getChildren())
->setOrder('name', Varien_Db_Select::SQL_ASC)
->joinUrlRewrite()
->load();
//$_subcategories = $_category->getChildrenCategories() ?>
                
                <?php if (count($_subcategories) > 0): ?>
                    <div class="dropdown_1column" style="width:235px;">
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <div class="col_1">
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </div>
                        <?php endforeach; ?>
                        <div class="clr" clear="all"></div>
                    </div>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>        
        </ul>                          
              
<?php endif; ?>

So instead of calling $_subcategories = $_category->getChildrenCategories()… you just set the $_subcategories variable to the following:

1
2
3
4
5
6
7
8
9
10
$_subcategories = $collection
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('name','ASC')
->addIdFilter($_category->getChildren())
->setOrder('name', Varien_Db_Select::SQL_ASC)
->joinUrlRewrite()
->load();

Posted in Magento, PHP | Tags: Alphabetical, getChildrenCategories, Magento, Sorting, Top Navigation |

MySQL Regex Column Name Selection

Posted on January 23, 2013 by bc-admin

If you’re like me, or if you’re not you might be at some point in your life… You may have a reason to lazily select all columns where the column name matches a pattern of sorts.

In my particular case, the data was organized in a fashion where several columns had the same name, but were also numbered. Since there were about 50 columns with the same pattern, I didn’t feel like writing out each column name.

1
2
3
4
5
6
7
SELECT CONCAT('SELECT ',
    GROUP_CONCAT(COLUMN_NAME SEPARATOR ', '),
    ' FROM ', 'test_db.test_table')
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'test_db'
    AND TABLE_NAME = 'test_table'
    AND COLUMN_NAME RLIKE '^FieldDesc';

And this outputs the select statement for you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT DISTINCT FieldDesc6, FieldDesc7, FieldDesc8,
                FieldDesc9, FieldDesc10, FieldDesc11,
                FieldDesc12, FieldDesc13, FieldDesc14,
                FieldDesc15, FieldDesc16, FieldDesc17,
                FieldDesc18, FieldDesc19, FieldDesc20,
                FieldDesc21, FieldDesc22, FieldDesc23,
                FieldDesc24, FieldDesc25, FieldDesc26,
                FieldDesc27, FieldDesc28, FieldDesc29,
                FieldDesc30, FieldDesc31, FieldDesc32,
                FieldDesc33, FieldDesc34, FieldDesc35,
                FieldDesc36, FieldDesc37, FieldDesc38,
                FieldDesc39, FieldDesc40, FieldDesc41,
                FieldDesc42, FieldDesc43, FieldDesc44,
                FieldDesc45, FieldDesc46, FieldDesc47,
                FieldDesc48, FieldDesc49, FieldDesc50,
                FieldDesc51, FieldDesc52, FieldDesc53,
                FieldDesc54, FieldDesc55, FieldDesc56
                  FROM test_db.test_table;

Posted in MySQL, Uncategorized | Leave a comment |

Recursive File Count with Powershell and Linux Shell

Posted on January 5, 2013 by bc-admin

Today, I needed a quick count of all of the files in Magento in order to set the “apc.num_files hint=xxx” in APC. Since all of the source files were on my Windows machine already, I started with Powershell.

Powershell (Windows)

1
(gci .\magento -recurse | Where-Object {-not ($_.PSsContainer)}).Count

After that, for accuracy sake, I checked on the server as well.

Linux Shell (Ran from inside the directory)

1
find . -type f | wc -l

Posted in Linux, Powershell, Shell Scripting | Leave a comment |

Powershell String Formatting to Html

Posted on August 22, 2012 by bc-admin

Ever had a bunch of rows in a text file that you would like to format as an html table, but don’t want to take as much time formatting all of the tr and td rows.  You can do something like this:

Say you have a comma-delimited text file with some rows like:

1
foo1,foo2,foo3

And you want to output them as html or something, you can do something like this:

1
2
Get-Content .\thetext.txt | %{ $data = $_.split(","); Write-Output "
$($data[0]) $($data[1]) $($data[2])" } > text-output.html

You should get something like this:

1
foo1 foo2 foo3

It certainly speeds up some tedious formatting of some static or maybe dynamic data.

Posted in Comma-Delimited, Files, HTML, Powershell | Leave a comment |

Quick Recursive Array Iteration with PHP’s RecursiveArrayIterator

Posted on July 27, 2012 by bc-admin

Today I was trying to find one small variable in a massive PHP array.  However, var_dump and print_r weren’t outputting the contents of the array in a human readable way. So thanks to the RecursiveArrayIterator, I was able to spit out the variables in more legible manner.
Here’s how it went:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function recurse_array($arr){
$iter = new RecursiveArrayIterator($arr);
echo " ";
 
while($iter->valid()){
  if($iter->hasChildren()){
    echo "<ul><li>".$iter->key()." => ";
    recurse_array($iter->getChildren());
    echo "</li></ul>";
 
  }else{
 
    echo "<ul>
<li>".$iter->key()." => ".$iter->current()."</li>
    </ul>"; }
 
    $iter->next();
  }
echo "";
} recurse_array($thearray);

Posted in Uncategorized | Leave a comment |

jQuery Regex Selector/Filter

Posted on July 18, 2012 by bc-admin

This came in handy today.

http://james.padolsey.com/javascript/regex-selector-for-jquery/

Posted in jQuery, Regular Expressions | Leave a comment |

Changing SQL Schema in Mysql Dump Files using Powershell

Posted on June 14, 2012 by bc-admin

Quick way to change the schema name in all mysql dump files in a directory using Powershell. Or you could just use it to change a string in files of particular type in a directory

1
2
C:\Users\SloshDev> foreach($f in gci("*.sql")){
              (Get-Content $f.fullname) | %{ $_ -replace "magento_1_9","magento_1_12" } | Set-Content $f.fullname }

Here’s how I changed the default character set from latin1 to utf8. May not want to use if different columns use different character sets.

1
2
3
C:\Users\SloshDev> foreach($f in gci("*.sql")){
      (Get-Content $f.fullname) | %{ $_ -replace "latin1","utf8" } | Set-Content $f.fullname
     }

Posted in Dump Files, MySQL, Powershell | Leave a comment |
Next Page »

Pages

  • About
  • Magento
  • Sitecore
  • WordPress

Archives

  • January 2017
  • December 2013
  • November 2013
  • February 2013
  • January 2013
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • November 2011
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • September 2009

Categories

  • .NET
  • Air Force
  • Apache Configuration
  • Bash
  • C#
  • Comma-Delimited
  • Custom Field Validator
  • Date Format
  • Dump Files
  • Files
  • Grep
  • Grooveshark
  • HTML
  • Image Resizing
  • Imagick
  • Integration
  • jQuery
  • Konami Code
  • Linux
  • Magento
  • Magento Third Party CMS Integration
  • Mini Shuttle
  • Miva Merchant 5.5
  • MySQL
  • PHP
  • Powershell
  • Regular Expressions
  • Rename-Item
  • Sed
  • Shell Scripting
  • Sitecore
  • Strings
  • Uncategorized
  • Updating Product Attributes
  • Video Games
  • Wordpress
  • X-37
  • XML
  • YUI Konami Event

CyberChimps WordPress Themes

© 2008 - 2016 Bogiecom.com