<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Edikon Forums - csv import function doesn't create category_url correctly]]></title>
		<link>http://www.edikon.com/forums/topic/6691/</link>
		<description><![CDATA[The most recent posts in csv import function doesn't create category_url correctly.]]></description>
		<lastBuildDate>Fri, 05 Feb 2010 14:18:56 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: csv import function doesn't create category_url correctly]]></title>
			<link>http://www.edikon.com/forums/post/25172/#p25172</link>
			<description><![CDATA[<p>i don&#039;t think i posted this anywhere else, but after updating the csv_import category_url issue, i also created a function that updates the category_url when a category is updated.&nbsp; it updates the url for the edited category and all subcategories as well.</p><div class="codebox"><pre><code>   /**************************************************************************
  ** name: update_subcat_urls()
  ** created by:  jnewman67
  ** description:  updates the category_urls after a category is updated/relocated
  ** parameters:  parent category_id and parent category_url
  ** returns:
  ***************************************************************************/    
  function update_subcat_urls($cat_id, $cat_url) {

        $db = new ps_DB;
        $dbc = new ps_DB;
        $q  = &quot;SELECT * FROM category, category_xref &quot;;
        $q .= &quot;WHERE category_xref.category_parent_id=&#039;$cat_id&#039; AND &quot;;
        $q .= &quot;category_xref.category_child_id=category.category_id &quot;;
        $db-&gt;query($q);
        while ($db-&gt;next_record()) {
            $new_url = $cat_url . &quot;_&quot; . str_replace(&quot; &quot;, &quot;&quot;, str_replace(&quot;_&quot;, &quot;&quot;, strtolower($db-&gt;f(&quot;category_name&quot;))));
            $q  = &quot;UPDATE category &quot;;
            $q .= &quot;SET category_url = &#039;&quot; . $new_url . &quot;&#039; &quot;;
            $q .= &quot;WHERE category_id = &#039;&quot; . $db-&gt;f(&quot;category_id&quot;) . &quot;&#039; &quot;;
            $dbc-&gt;query($q);
            $this-&gt;update_subcat_urls($db-&gt;f(&quot;category_id&quot;), $new_url);
            }
        return True;
        }</code></pre></div><p>this function should be added to ps_product_category.inc.</p><p>it is called from the &quot;update&quot; function in ps_product_category.&nbsp; the mods for that function call are here:</p><p>add this to the top of the function, just inside the $this-&gt;validate_update($d) check:<br /></p><div class="codebox"><pre><code>             //  check to see if updating the subcategories is necessary
            $update_subs = False;
            $q  = &quot;SELECT * FROM category &quot;;
            $q .= &quot;WHERE category.category_id=&#039;&quot; . $d[&quot;category_id&quot;] . &quot;&#039; &quot;;
            $db-&gt;query($q);
            if ($db-&gt;next_record()) {
                if ($db-&gt;f(&quot;cateogry_url&quot;) &lt;&gt; $d[&quot;category_url&quot;]) $update_subs = True;
                }</code></pre></div><p>add this to the end of the same $this-&gt;validate_update($d) check, right before the &quot;return True&quot;:<br /></p><div class="codebox"><pre><code>      // Update subcategories if necessary
            if ($update_subs) $this-&gt;update_subcat_urls($d[&quot;category_id&quot;], $d[&quot;category_url&quot;]);</code></pre></div><p>as a note, if you open/save all the root categories, you&#039;ll effectively update all the category_urls for every product in the site.&nbsp; and it does it quickly (900 in one shot wasn&#039;t even noticable on my server/site/equipment).</p>]]></description>
			<author><![CDATA[dummy@example.com (jnewman67)]]></author>
			<pubDate>Fri, 05 Feb 2010 14:18:56 +0000</pubDate>
			<guid>http://www.edikon.com/forums/post/25172/#p25172</guid>
		</item>
		<item>
			<title><![CDATA[csv import function doesn't create category_url correctly]]></title>
			<link>http://www.edikon.com/forums/post/25099/#p25099</link>
			<description><![CDATA[<p>there is actually a bug in the csv_import class - the function csv_category doesn&#039;t create the full category path when populating the category_url field.&nbsp; it only inserts the final category name as the category_url, not the full path of the category tree.</p><p>example:&nbsp; a new category path of &quot;store/widgets/red/small&quot; would result in all the categories being created and nested correctly, but the category_url would be &quot;store&quot;, &quot;widgets&quot;, &quot;red&quot; and &quot;small&quot; for each category creeated, respectively.&nbsp; they should be &quot;store&quot;, &quot;store_widgets&quot;, &quot;store_widgets_red&quot; and &quot;store_widgets_red_small&quot; respectively.</p><p>in fairness, any changes made to the category after it&#039;s been imported would correctly reset the value in the DB.&nbsp; categories unmodified would retain their incorrect url.</p><p>reason this is important:&nbsp; if you want to find all products that are in a category and all subcategories in one search, you only have to search for products who&#039;s categories have a &quot; category_url LIKE &#039;$category_url%&#039; &quot;.&nbsp; much simplier than recursively traversing the category tree from the starting point to the bottom to find all the possible category_ids that are invovled.</p><p>this updated function fixes that problem.</p><p>this is an updated csv_category in ps_csv.inc<br /></p><div class="codebox"><pre><code>  /**************************************************************************
  ** name: csv_category()
  ** created by: John Syben
  ** updated by: jnewman67
  ** Creates categories from slash delimited line
  ***************************************************************************/
  function csv_category($line) {
    // Explode slash delimited category tree into array
    $category_list = explode(&quot;/&quot;, $line);
    $category_count = count($category_list);

    $db = new ps_DB;
    $category_parent_id = &#039;0&#039;;
        $full_url = &quot;&quot;;

      // For each category in array
      for($i = 0; $i &lt; $category_count; $i++) {
        // See if this category exists with it&#039;s parent in xref
        $q = &quot;SELECT category.category_id FROM category,category_xref &quot;;
        $q .= &quot;WHERE category.category_name=&#039;&quot; . $category_list[$i] . &quot;&#039; &quot;;
        $q .= &quot;AND category_xref.category_child_id=category.category_id &quot;;
        $q .= &quot;AND category_xref.category_parent_id=&#039;$category_parent_id&#039;&quot;;
        $db-&gt;query($q);
          // If it does not exist, create it
          if ($db-&gt;next_record()) { // Category exists
            $category_id = $db-&gt;f(&quot;category_id&quot;);
          }
          else { // Category does not exist - create it
            $hash_secret=&quot;PHPShopIsCool&quot;;
            $category_id = md5(uniqid($hash_secret));
            $timestamp = time();

                        $cat_url = strtolower($category_list[$i]);
                        $cat_url = str_replace(&quot; &quot;, &quot;&quot;,$cat_url);
                        $cat_url = str_replace(&quot;_&quot;, &quot;&quot;,$cat_url);
                        if ($full_url == &quot;&quot;) {
                            $full_url = $cat_url;
                        } else {
                            $full_url .= &quot;_&quot; . $cat_url;
                        }

            // Add category
            $q = &quot;INSERT INTO category &quot;;
            $q .= &quot;(category_id,vendor_id,category_name,category_url,category_publish,cdate,mdate) &quot;;
            $q .= &quot;VALUES (&#039;&quot;;
            $q .= $category_id . &quot;&#039;,&#039;&quot;;
            $q .= &quot;1&#039;, &#039;&quot;;
            $q .= $category_list[$i] . &quot;&#039;, &#039;&quot;;
            $q .= &quot;$full_url&#039;, &#039;&quot;;
            $q .= &quot;Y&#039;, &#039;&quot;;
            $q .= $timestamp . &quot;&#039;, &#039;&quot;;
            $q .= $timestamp . &quot;&#039;)&quot;;
            $db-&gt;query($q);

            // Create xref with parent
            $q = &quot;INSERT INTO category_xref &quot;;
            $q .= &quot;(category_parent_id, category_child_id) &quot;;
            $q .= &quot;VALUES (&#039;&quot;;
            $q .= $category_parent_id . &quot;&#039;, &#039;&quot;;
            $q .= $category_id . &quot;&#039;)&quot;;
            $db-&gt;query($q);
          }
        // Set this category as parent of next in line
        $category_parent_id = $category_id;
      } // end for

    // Return the last category_id which is where the product goes
    return $category_id;

  } // End function csv_category</code></pre></div><p>the full_url is used to track the full tree category path.&nbsp; any lines with that variable on it are the ones that were actually changed.</p>]]></description>
			<author><![CDATA[dummy@example.com (jnewman67)]]></author>
			<pubDate>Tue, 10 Jun 2008 12:49:38 +0000</pubDate>
			<guid>http://www.edikon.com/forums/post/25099/#p25099</guid>
		</item>
	</channel>
</rss>
