Welcome Guest, Not a member yet? Register   Sign In
PyroCMS v0.9.7.4 - an open-source modular general purpose CMS

[eluser]Turv[/eluser]
Okay, Well i hit a stump...

I've got a solution that will allow an unlimited sub-page structure so you can have pages such as

/parent/primary/secondary/sub-page/

It works by passing an array of URI Segments to the page function, This receives an array such as

Code:
Array
(
    [1] => parent
    [2] => primary
    [3] => secondary
    [4] => sub-page
)

With sub-page being the targetting page for content, I store this slug in a variable, remove it from the array. The last item in the array (Secondary) is then the parent of this page. So i query the database to get the ID (Parent ID) for the Secondary page.

If there are more than one result, I perform the final query where i find a row in the database where the slug is 'sub-page' and the parent id is whatever the id of secondary is.

This means you can have multiple 'sub-page' slugs.

However...The problem i have is that if i have pages where the last parts are identical (unlikley to happen?) it causes problems. Such as, If i have another page such as...

Code:
Array
(
    [1] => test-page
    [2] => other
    [3] => secondary
    [4] => sub-page
)

Then with the last two parts (secondary/sub-page) being the same it fails. What i need is a way to recursively call getIdBySlug passing a new parent slug until i get a reslt with only one row.

This is the code i have...

Pages Controller, _remap function
Change: If a sub page (of any kind) exists then pass on the uri segment array, otherwise just pass the first segment, call page function directly
Code:
function _remap()
    {
        // If Sub page exsits
        if($this->uri->segment(2)) {
            $slug = $this->uri->segment_array();
        } else {
            $slug = $this->uri->segment(1, 'home');
        }
        
        // This basically keeps links to /home always pointing to the actual homepage even when the default_controller is changed
        @include(APPPATH.'/config/routes.php'); // simple hack to get the default_controller, could find another way.
        
        // The default route is set to a different module than pages. Send them to there if they come looking for the homepage
        if(!empty($route) && $slug == 'home' && $route['default_controller'] != 'pages')
        {
            redirect('');
        }
        
        // Default route = pages
        else
        {
            // Show the requested page with all segments available
            //call_user_func_array(array($this, 'page'), $slug);
            $this->page($slug);
        }
    }

Page Controller, Pages function
Change: Restored back to the original, No changes needed in this function anymore

Pages Model, getBySlug function
Code:
public function getBySlug($slug = '', $lang = NULL)
    {
        /**
         * Slug is now an Array
         * Take the Last element as slug
         * Take the element before that for parent id
         * Perform database query
         */
         if(is_array($slug)) {
             // Get Last Element of Slug aray
             $Page = end($slug);
            
             // Remove last element from Slug array
             array_pop($slug);

             // The new last element will be used for the parent ID
            $ParentID = $this->getIdBySlug(end($slug), $Page);

             $this->db->where('slug', $Page);
             $this->db->where('parent', $ParentID);
         }
        
         // If $slug is not an array, must be single page, just use it directly
         else
         {
            $this->db->where('slug', $slug);
         }
        
        if($lang == 'all')
        {
            exit('where did this code go?! tell me if you see this message [email protected]!');
        }  
            
        elseif($lang != NULL)
        {
            $this->db->where('lang', $lang);
        }
        
        return $this->get($lang);
    }

Pages Model, getIdBySlug function
Code:
public function getIdBySlug($parent = null, $slug = null) {
        if($parent == null || $slug == null)
            return false;
        
        $this->db->where('slug', $parent);
        $Query = $this->db->get('pages');
        
        // Store Result - Continue
        $Result = $Query->result_array();

        // If there is more than one slug exists of the same name, perform additional checks
        if($Query->num_rows() > 1)
        {
            foreach ($Result as $Row) {
    
                $this->db->where('slug', $slug);
                $this->db->where('parent', $Row['id']);
                $Query = $this->db->get('pages');
                
                // If there any results then return the ID
                if($Query->num_rows() > 0)
                    return $Row['id'];
            }
        }    
        // Only one result, return Id
        else
        {
            return $Result[0]['id'];
        }
    }

So basically, what i did in my test installation was create a number of child pages so i had two urls as below

/primary/sub-page/sub-sub-page/
/test-page/sub-page/sub-sub-page/

When going to /test-page/sub-page/sub-sub-page/ i received the content for /primary/sub-page/sub-sub-page/ because in my getIdBySlug function, when doing the additional check i return the id as soon as i find a match. What i need to do instead is like a recursive function using the rest of the uri array to....I don't know, have a look see if you can figure anything out.

This at the moment...seems to work perfectly for any url as long as there are not two segments of the same name as per the above example


Messages In This Thread
PyroCMS v0.9.7.4 - an open-source modular general purpose CMS - by El Forum - 08-24-2009, 08:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB