1: <?php
2:
3: namespace Alpha\Controller;
4:
5: use Alpha\Util\Logging\Logger;
6: use Alpha\Util\Logging\LogProviderFile;
7: use Alpha\Util\Feed\RSS2;
8: use Alpha\Util\Feed\RSS;
9: use Alpha\Util\Feed\Atom;
10: use Alpha\Util\Http\Request;
11: use Alpha\Util\Http\Response;
12: use Alpha\Util\Config\ConfigProvider;
13: use Alpha\Exception\ResourceNotFoundException;
14: use Alpha\Exception\IllegalArguementException;
15: use Alpha\Model\Article;
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: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
59: class FeedController extends Controller implements ControllerInterface
60: {
61: 62: 63: 64: 65: 66: 67:
68: private $ActiveRecordType;
69:
70: 71: 72: 73: 74: 75: 76:
77: private $type;
78:
79: 80: 81: 82: 83: 84: 85:
86: protected $title;
87:
88: 89: 90: 91: 92: 93: 94:
95: protected $description;
96:
97: 98: 99: 100: 101: 102: 103:
104: protected $fieldMappings;
105:
106: 107: 108: 109: 110: 111: 112:
113: private $sortBy = 'OID';
114:
115: 116: 117: 118: 119: 120: 121:
122: private static $logger = null;
123:
124: 125: 126: 127: 128:
129: public function __construct()
130: {
131: self::$logger = new Logger('FeedController');
132: self::$logger->debug('>>__construct()');
133:
134: $config = ConfigProvider::getInstance();
135:
136:
137: parent::__construct('Public');
138:
139: self::$logger->debug('<<__construct');
140: }
141:
142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152:
153: public function doGET($request)
154: {
155: self::$logger->debug('>>doGET($request=['.var_export($request, true).'])');
156:
157: $config = ConfigProvider::getInstance();
158:
159: $params = $request->getParams();
160:
161: $response = new Response(200);
162:
163: try {
164: if (isset($params['ActiveRecordType'])) {
165: $ActiveRecordType = urldecode($params['ActiveRecordType']);
166: } else {
167: throw new IllegalArguementException('ActiveRecordType not specified to generate feed!');
168: }
169:
170: if (isset($params['type'])) {
171: $type = $params['type'];
172: } else {
173: throw new IllegalArguementException('No feed type specified to generate feed!');
174: }
175:
176: if (class_exists($ActiveRecordType)) {
177: $this->ActiveRecordType = $ActiveRecordType;
178: } else {
179: throw new IllegalArguementException('No ActiveRecord available to render!');
180: }
181: $this->type = $type;
182:
183: $this->setup();
184:
185: switch ($type) {
186: case 'RSS2':
187: $feed = new RSS2($this->ActiveRecordType, $this->title, str_replace('&', '&', $request->getURI()), $this->description);
188: $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3]);
189: $response->setHeader('Content-Type', 'application/rss+xml');
190: break;
191: case 'RSS':
192: $feed = new RSS($this->ActiveRecordType, $this->title, str_replace('&', '&', $request->getURI()), $this->description);
193: $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3]);
194: $response->setHeader('Content-Type', 'application/rss+xml');
195: break;
196: case 'Atom':
197: $feed = new Atom($this->ActiveRecordType, $this->title, str_replace('&', '&', $request->getURI()), $this->description);
198: $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3],
199: $this->fieldMappings[4]);
200: if ($config->get('feeds.atom.author') != '') {
201: $feed->addAuthor($config->get('feeds.atom.author'));
202: }
203: $response->setHeader('Content-Type', 'application/atom+xml');
204: break;
205: }
206:
207:
208: $feed->loadBOs(20, $this->sortBy);
209: $response->setBody($feed->render());
210:
211:
212: $feedLog = new LogProviderFile();
213: $feedLog->setPath($config->get('app.file.store.dir').'logs/feeds.log');
214: $feedLog->writeLine(array($this->ActiveRecordType, $this->type, date('Y-m-d H:i:s'), $request->getUserAgent(), $request->getIP()));
215: } catch (IllegalArguementException $e) {
216: self::$logger->error($e->getMessage());
217: throw new ResourceNotFoundException($e->getMessage());
218: }
219:
220: self::$logger->debug('<<doGet');
221:
222: return $response;
223: }
224:
225: 226: 227:
228: protected function setup()
229: {
230: self::$logger->debug('>>setup()');
231:
232: $config = ConfigProvider::getInstance();
233:
234: $bo = new $this->ActiveRecordType();
235:
236: if ($bo instanceof Article) {
237: $this->title = 'Latest articles from '.$config->get('app.title');
238: $this->description = 'News feed containing all of the details on the latest articles published on '.$config->get('app.title').'.';
239: $this->fieldMappings = array('title', 'URL', 'description', 'created_ts', 'OID');
240: $this->sortBy = 'created_ts';
241: }
242:
243: self::$logger->debug('<<setup');
244: }
245: }
246: