Welcome Guest, Not a member yet? Register   Sign In
[all solved] Ready to chuck it all out...Trying to create a ree-usable, customizable News Feed section.
#1

[eluser]techgnome[/eluser]
I'm trying to build a re-usable component (for the lack of a better word since it's going to consist of models, views and controllers). Part of this functionality is that I want parts of it to be configurable. So I created a file and tossed it into the config folder. Now when I load my controller, I can also load my configuration settings and use them. That's all great and dandy, but now I find myself wanting to use these configuration settings outside the controller.

Example: One of the options is to use slugs or ids for accessing the postings. What I'm trying to develop is a news feed sort of use for my site, so I don't need/want a full blown CMS or blogging app. However, I don't want to build an If... Else... in my views each time I want to write out the anchor tag for it. What I'd ideally like is to have a function I could call, say nf_AnchorTitle($article) and have it return the a href tag with the slug/id as appropriate.

So I tossed it into a helper file... I quickly found out however, that I can't get to the config class from inside the helper, since it doesn't have the $this context. Nor am I willing to get a $CI reference each time I call the function, and including the controller as a parameter to the function seems like overkill. BLAGNOG!

I then thought about creating a library. But I've run into two problems with that.
1) I still can't get to the Library functions from inside my view, so it's useless.
2) Setting the config didn't work as described in the UG.
Quote:You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder. Note that if you dynamically pass parameters as described above, the config file option will not be available.
Whaaaa? The last sentence I get. The first intrigued me. The second one I did... OK.. then what? How do I access them?

In the end I've come to the conclusion that I'm either going about this all bass-ackwards... or it's going to require some trick that I don't really want to do (like getting the CI instance) ... at this point, CI2.0 isn't an option. I'm still running PHP4... and there isn't anything I can do about it (was supposed to be done 3Q2010... it now being 4Q2010... we'll see).

I thought about looping through the data and creating a "URL" or "Link" property to the data... but that seems wrong... and not very efficient.

All I want is a way to enter news articles, and have them display on a "News" section of my site. For lack of a better comparison, a read-only blog... no comments or anything like that, a few categories, some admin control and that's about it.

Am I on a fool's errand? Have I lost my sanity? Or is there a way to make this work the way I want?

Ideally, I want my view to be this simple:
Code:
<?php

if ($articleCount != 0) {
?>
<h2>&lt;?php echo $articleCount; ?&gt; News Items Found</h2>
<ol>
&lt;?php
foreach($articles as $articleRow) {
?&gt;
    <li>
        <h3>&lt;?php echo article_link($articleRow); ?&gt;</h3>
        <p>&lt;?php echo $articleRow->articleContent; ?&gt;</p>
        <p>Posted on: &lt;?php echo formatDate($articleRow->articlePublishDate); ?&gt; by &lt;?php echo $articleRow->articleAuthorID ?&gt;</p>
        <p>Category: &lt;?php //echo $articleRow->articleCategory; ?&gt;</p>
    </li>
&lt;?php }
?&gt;
</ol>
&lt;?php } else echo 'No news at this time';?&gt;
Where article_link and formatDate take care of determining if the link uses the article ID or slug based on what's in the config file, and format date takes care of formatting my dates, again, based on what's in the config file.

Right now, I feel like I'm going in circles and I'm beginning to wonder if it's even worth it anymore.

If I wasn't planning on posting the end result, I'd say frack it and just hard code everything the way I want it to be (and I still haven't ruled out this action) but I figured someone else might also find it useful, so I'm trying to keep re-usability in mind.

-tg
#2

[eluser]techgnome[/eluser]
Ignore this... unless you really want to read it... about 20 minutes after posting it, I came across something in the UG that put it all together, or more accurately, gave me a clue on how to accomplish what I want. I didn't realize that $this would be available to me in the view. That resolved my problem of helper vs Library (going the library route). I would still like a better way to load my config settings... currently, I'm loading them like this in my controller:
Code:
$this->config->load('newsfeed',TRUE);
I then extract the settings into their own array... then pass that array into my library constructor... where it stores it internally. Now I can get to the settings when I need to from my functions. Feels like a hack. For now, it works for me since I need to have access to some of the settings in the controller and in the library.

Wait... is there a way to load my config settings into their own array? Currently I'm adding to the $config[] as suggested in the UG... Hmmm... I'm off to read more of the UG.

-tg
#3

[eluser]techgnome[/eluser]
Turns out I just need to read some of the more esoteric parts more often. I've managed to find everything I needed. This means I can get back to building this component. Yay.

Sorry for the mess... sometimes I just need to work these things out.

-tg
#4

[eluser]Bainzy[/eluser]
im always doing stuff like this ... posting and then workign it out .... its good thought as it helps you clear your mind of questions and that oftern clears things up ... ill sit there pulling my hair out some nights .... then ill go for a smoke and a cuppa ... come back and i figure it out Smile glad you got it sorted ... sounds interesting what you are doing ... is it just a new feed module then ?

Chris
#5

[eluser]techgnome[/eluser]
Quote:is it just a new feed module then ?
Yeah pretty much... I found myself opening up the currently hard-coded news.php file to add updates and realized that it would be nice if there was something a little more versatile... I was about to build my own from scratch, when I came across some of the pre-built blog setups... after a few issues, I decided that those are more designed to be the whole site, which isn't what I need. Plus I don't want comments (eventually I'll add a forum, and a hook, so that when a news item is posted, a thread is auto created in the forum, keep the discussions there). But I do want multiple categories (Site News, Industry News, etc) and multiple authors (thinking long term). Plus it seems to be a good way to delve into CI and what it can and can't do.

Turns out the key I'd been looking for was tucked away in some sample code in the config class documentation with only a single line mention. When I first read through the config section, I kinda glossed over it. I have a tendency to do that after reading documentation for long periods. The other thing I had glossed over was the mention that $this is available in my Views - something which I didn't know, and as it turned out was ultimately the final piece of the puzzle I needed. Now that I have the public side of my module going, I can work on the admin side of things. I suspect that's going to be the greater of the challenges.

Ultimately it will be able to create users and mark some as "Needing Moderation" ... not sure if that will be a user setting or a "Group" setting. Categories. Be able to create articles and save them without posting them (they get saved as drafts). Use slugs or IDs to reference articles. And so on. Once I get closer to something that works reasonably (even if it's ugly), I'll be sure to post it.

-tg
#6

[eluser]Bainzy[/eluser]
yeah thats a really cool idea ... i like the hooks idea ... but i have no idea how to implement hooks ... as it is i will be creating a news module for Dove Forums and i really really like the hooks idea .... so if you manage to get the module ugly or not .. let me know and i can maybe introduce it into Dove Forums as a plug in ... obviously giving you credit Smile ... maybe host it on my site for ppl to download or something ... i just may need to tweak it to suit the forums structure but im sure we can work something out Smile ... you need a forums ... i need a news feed Wink. Are you making it RSS too ?
#7

[eluser]techgnome[/eluser]
Dove Forms is CI2.0 though, right? Currently I'm relegated to 1.7.2 since I'm still on PHP4 with my host (was supposed to be php5 by 3Q2010... but we'll see)... But, yeah, I'm sure we could work something out. Regarding RSS, it wasn't part of the original road map, since it wasn't something I needed, but support for it could certainly be added. Wouldn't take much, a controller and a couple of views to the proper format (RSS1, RSS2, ATOM, etc).

-tg

EDIT - I found out how to use php5 at my host (their suggestion was to make a mapping in the htaccess file). I've ported over the work I've done so far (which wasn't much at this point) to CI2. You're still planning on integrating Ion_Auth into Dove, right? I was going to hobble my own login together, but why re-invent the wheel? Looks like I know what I'll be tinkering with the next couple of days.
#8

[eluser]Bainzy[/eluser]
hi mate, glad to hear your host has caught up wth the times Smile lol ... as of Ion Auth ... YES Smile its all now setup and running ... having a few teathing issues with it and i dont like the way you have to call the user information :

Code:
$user = $this->ion_auth->user();
$this->user->email;

I dont know i did not like the idea of creating multiple arrays of user data ... i have tried to place it into MY_Controller using the following :

Code:
// If the user is logged in, then create a array of user data
if($this->ion_auth->logged_in())
{
    $this->user = $this->ion_auth->user();
}

But when ever i try to access $this->user ... i get a error but its a shame as that would have being site wide and i would have being able to pick it up from views and controller alike, I think its something to do with HMVC but i have tried it a number of different ways and made sure all my controllers extend MY_Controller but it still spits up a error Sad

For now i have changed what session data is created when the user logs into there account ... i add a few extra bits of information and then use $this->session->userdata('user_id'); for example and that seems to work.

But yeah man keep me posted on your progress and the RSS is maybe something i can tinker with if it was not on your road map Smile

Chris




Theme © iAndrew 2016 - Forum software by © MyBB