1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2: class Model_article extends CI_Model{
3:
4: public function browse()
5: {
6: $cache_name = '_articles_term_' . $this->input->get('term') . '_leval_' . $this->input->get('leval') . '_alpha_' . $this->input->get('alpha') . '_sort_' . $this->input->get('sort') . '_order_' . $this->input->get('order') . '_page_' . $this->input->get('page');
7: if (!$data = $this->cache->file->get($cache_name)) {
8: $this->db->select('xenon_data.data_id, data_title, data_modified, data_editor, data_editing_status, empl_dname, data_status as link_status');
9:
10: $this->db->join('xenon_employee', 'xenon_employee.empl_id = xenon_data.data_editor');
11: $this->db->from('xenon_data');
12: if ($this->session->userdata('role_assigned') == 'y') {
13: $this->db->where('data_editor', $this->session->userdata('empl_id'));
14: }
15:
16: if ($this->input->get('order') != '') {
17: $this->db->order_by('data_' . $this->input->get('order') . ($this->input->get('leval') != '' ? ' ' . $this->input->get('leval') : ''));
18: }
19: $this->db->where('xenon_data.is_deleted', 'n');
20:
21: if ($this->input->get('term') != '') {
22: $this->db->like("data_title", $this->input->get('term'));
23: }
24:
25: if ($this->input->get('alpha') != '') {
26: $this->db->where("data_title like '" . $this->input->get('alpha') . "%'", '', FALSE);
27: }
28:
29: if ($this->input->get('sort') != '') {
30: $this->db->order_by($this->input->get('sort'));
31: } else {
32: $this->db->order_by('data_title');
33: }
34:
35: if($this->input->get('page')!=''){
36: $page = $this->input->get('page');
37: $start = ($page * 50)-50;
38: $end = ($page * 50);
39: $this->db->limit(50,$start);
40: }else{
41: $this->db->limit(50);
42: }
43: $data = $this->db->get()->result();
44: $this->cache->file->save($cache_name, $data, 2592000);
45: }
46:
47:
48:
49: return $data;
50: }
51:
52: public function render($article){
53: return '
54: <tr>
55: <td>'.$article->data_title.'</td>
56: <td>'.date('F d,Y',$article->data_modified).'</td>
57: <td align="center">'.$article->empl_dname.'</td>
58: <td align="center">'.data_status($article->data_editing_status).'</td>
59: <td align="center"><a href="'.site_url('article/edit/'.urlencode(base64_encode($article->data_id))).'">Edit</a> '.(check_permission('dc')?'| <a href="'.site_url('article/detail/'.urlencode(base64_encode($article->data_id))).'">Details</a> | <a href="?action=delete&id='.urlencode(base64_encode($article->data_id)).'">Delete</a> | <a href="'.site_url('gmreader/read/'.urlencode(base64_encode($article->data_id))).'" target="_blank">Reader Cache</a> | <a href="?action='.($article->link_status == 'e' ?'disable':'enable').'&id='.urlencode(base64_encode($article->data_id)).'">'.($article->link_status == 'e' ?'Disable Link':'Enable Link').'</a>':'').
60: '</td></tr>
61: ';
62: }
63:
64: public function render_pop($article){
65: $url = $article->data_id;
66: $url = urlencode(base64_encode($url));
67: return '
68: <tr>
69: <td>'.$article->data_title.'</td>
70: <td align="center"><a href="?selected='.$url.'">Select</a>
71: </tr>
72: ';
73: }
74:
75: public function render_emp_edit($article,$check = 'n'){
76: $url = '<a target="_blank" href="'.site_url('article/view/'.urlencode(base64_encode($article->data_id))).'">View</a>';
77:
78: return '
79: <tr>
80: <td align="center"><input type="checkbox" name="selected[]" '.($check =='y' || $article->empl_id > 0?'checked = "checked"':'').' value="'.urlencode(base64_encode($article->data_id)).'"></td>
81: <td>'.$article->data_title.'</td>
82: <td align="center">'.$url.'</td>
83: </tr>
84: ';
85: }
86:
87: public function render_emp_add($article){
88: $url = '<a target="_blank" href="'.site_url('article/view/'.urlencode(base64_encode($article->data_id))).'">View</a>';
89:
90: return '
91: <tr>
92: <td align="center"><input type="checkbox" name="selected[]" value="'.urlencode(base64_encode($article->data_id)).'::'.$article->data_title.'"></td>
93: <td>'.$article->data_title.'</td>
94: <td align="center">'.$url.'</td>
95: </tr>
96: ';
97: }
98:
99: public function error(){
100: return '
101: <tr>
102: <td colspan="5">No artciles found</td>
103: </tr>
104: ';
105: }
106:
107: public function pick($enc_id){
108: $article = $this->db->select('*,(select data_status from xenon_empolyee_data where xenon_empolyee_data.data_id = xenon_data.data_id limit 1) as data_status')->from('xenon_data')->where('data_id',base64_decode(urldecode($enc_id)))->get()->row();
109: return $article;
110: }
111:
112: public function pick_link($enc_id){
113: $data_id = $this->db->select('data_id')->from('xenon_link')->where('link_id',base64_decode(urldecode($enc_id)))->get()->row()->data_id;
114: return $this->pick(urlencode(base64_encode($data_id)));
115: }
116:
117: public function index($enc_id){
118: return $this->db->from('xenon_index')->where('index_data',base64_decode(urldecode($enc_id)))->get()->result();
119: }
120:
121: public function reference($enc_id){
122: return $this->db->from('xenon_reference_data')->join('xenon_reference','xenon_reference_data.refe_id = xenon_reference.refe_id')->where('data_id',base64_decode(urldecode($enc_id)))->get()->result();
123: }
124:
125: public function update($id){
126: if($this->input->post('action')=='update_article'){
127: $id = base64_decode(urldecode($id));
128: $data['data_title'] = $this->input->post('dtitle');
129: $data['data_body_temp'] = $this->input->post('content');
130: $ref_count = 0;
131: $refences = '';
132:
133: if($this->input->post('generate_cache')!='') {
134: $this->build_article_cache_gmreader($data['data_body_temp'], $id);
135: $this->build_article_cache($data['data_body_temp'],$id);
136: }
137:
138: $data['data_modified'] = time();
139: $data['data_modifiedby'] = $this->session->userdata('empl_id');
140: $data['data_editor'] = $this->input->post('employee');
141: $data['data_editing_status'] = $this->input->post('status');
142: $this->db->where('data_id',$id);
143: $this->db->update('xenon_data',$data);
144: return array('success'=>'Article has been updated Successfully');
145: }
146: }
147:
148: public function remove_cache($id){
149: $data['data_body_cache'] = '';
150: $this->db->where('data_id',base64_decode(urldecode($id)));
151: $this->db->update('xenon_data',$data);
152: }
153:
154: public function remove($enc_id){
155: $data['is_deleted'] = 'y';
156: $this->db->where('data_id',base64_decode(urldecode($enc_id)));
157: $this->db->update('xenon_data',$data);
158: }
159:
160: public function remove_index($enc_id){
161: $this->db->delete('xenon_index','index_id = '.base64_decode(urldecode($enc_id)));
162: }
163:
164: public function remove_article_full(){
165: if($this->input->get('action') == 'delete'){
166: $enc_id = $this->input->get('id');
167: if($enc_id != ''){
168: $this->db->delete('xenon_data',array('data_id'=>base64_decode(urldecode($enc_id))));
169: $this->db->delete('xenon_index',array('index_data'=>base64_decode(urldecode($enc_id))));
170: }
171: }
172: if($this->input->get('action') == 'disable'){
173: $data['link_status'] = 'n';
174: $this->db->where('data_id',base64_decode(urldecode($this->input->get('id'))));
175: $this->db->update('xenon_link',$data);
176:
177: $data['data_status'] = 'd';
178: $this->db->where('data_id',base64_decode(urldecode($this->input->get('id'))));
179: $this->db->update('xenon_data',$data);
180:
181:
182: $links = $this->db->select('link_id')->from('xenon_link')->where('data_id',base64_decode(urldecode($this->input->get('id'))))->get();
183: if($links->num_rows() > 0){
184: $links_list = array();
185: foreach($links->result() as $l){
186: $links_list[] = $l->link_id;
187: }
188: $datas = $this->db->select('data_id')->from('xenon_data_links')->where_in('link_id',$links_list)->get();
189: if($datas->num_rows() > 0){
190: $datas_list = array();
191: foreach($datas->result() as $l){
192: $datas_list[] = $l->data_id;
193: }
194: $dataz['data_body_cache'] = '';
195: $this->db->where_in('data_id',$datas_list);
196: $this->db->update('xenon_data',$dataz);
197: }
198: }
199: }
200:
201: if($this->input->get('action') == 'enable'){
202: $data['link_status'] = 'y';
203: $this->db->where('data_id',base64_decode(urldecode($this->input->get('id'))));
204: $this->db->update('xenon_link',$data);
205:
206: $data['data_status'] = 'e';
207: $this->db->where('data_id',base64_decode(urldecode($this->input->get('id'))));
208: $this->db->update('xenon_data',$data);
209:
210:
211: $links = $this->db->select('link_id')->from('xenon_link')->where('data_id',base64_decode(urldecode($this->input->get('id'))))->get();
212: if($links->num_rows() > 0){
213: $links_list = array();
214: foreach($links->result() as $l){
215: $links_list[] = $l->link_id;
216: }
217: $datas = $this->db->select('data_id')->from('xenon_data_links')->where_in('link_id',$links_list)->get();
218: if($datas->num_rows() > 0){
219: $datas_list = array();
220: foreach($datas->result() as $l){
221: $datas_list[] = $l->data_id;
222: }
223: $dataz['data_body_cache'] = '';
224: $this->db->where_in('data_id',$datas_list);
225: $this->db->update('xenon_data',$dataz);
226: }
227: }
228: }
229: }
230:
231: public function add_index($name = '', $parent = ''){
232: $data['index_title'] = $name;
233: $data['index_data'] = $parent;
234: $this->db->insert('xenon_index',$data);
235: return $this->db->insert_id();
236: }
237:
238: public function update_data_only(){
239: $id = base64_decode(urldecode($this->input->post('id')));
240: $data['data_body'] = $this->input->post('content');
241: $this->db->where('data_id',$id);
242: $this->db->update('xenon_data',$data);
243: }
244:
245: public function build_article_cache($body,$id){
246: $ref_count = 0;
247: $refences = '';
248: $dom = new DOMDocument();
249: $dom->loadHTML($body);
250: $a_tags = $dom->getElementsByTagName('a');
251: foreach ($a_tags as $a_tag) {
252: $link_status = $this->db->from('xenon_link')->where('link_id',base64_decode(urldecode($a_tag->getAttribute('href'))))->where('link_status','y')->get();
253: $link_data_status = $this->db->from('xenon_data_links')->where('link_id',base64_decode(urldecode($a_tag->getAttribute('href'))))->where('data_id',$id)->where('status','y')->get();
254: if($link_status->num_rows() > 0 && $link_data_status->num_rows() > 0){
255: $txt = str_replace('&','&',str_replace('’', "'", $a_tag->nodeValue));
256: $txt = htmlentities($txt);
257: $link_new = 'link/'.$a_tag->getAttribute('href');
258: $class = '';
259: if($txt == 'references'){
260: $link_text = $this->db->from('xenon_link')->join('xenon_reference','xenon_link.data_id = xenon_reference.refe_id')->where('link_id',base64_decode(urldecode($a_tag->getAttribute('href'))))->get();
261: if($link_text->num_rows() > 0){
262: $link_text = $link_text->row()->refe_title;
263: }else{
264: $link_text = "Not Found";
265: }
266:
267: $ref_count = $ref_count + 1;
268: $txt = '<sup>'.'['.$ref_count.']'.'</sup>';
269: $class = 'reference';
270: $refences .= '<div><a href="/reference/link/'.substr($link_new,5).'" class="reference-transfer" target="_blank" "="" id="'.$ref_count.'">'.$link_text.'</a></div>';
271: $link_new = '#'.$ref_count;
272: $link = $dom->createElement('a',$txt);
273: if($class == 'reference'){
274: $link->setAttribute('class','reference');
275: }
276: $link->setAttribute('href', $link_new);
277: $a_tag->parentNode->replaceChild($link, $a_tag);
278: }
279: }else{
280: $a_tag->parentNode->removeChild($a_tag);
281: }
282: }
283: $data['data_body_cache'] = substr($dom->saveHTML(),119,-15).($refences!=''?'<h2>References</h2>'.$refences:'');
284: $this->db->where('data_id',$id);
285: $this->db->update('xenon_data',$data);
286: }
287:
288: public function build_article_cache_gmreader($body,$id){
289: $ref_count = 0;
290: $refences = '';
291: preg_match_all("/<a.*?href=\"([^`]*?)\">([^`]*?)<\/a>/",$body,$ar);
292: $a_tag = $ar[0];
293: $a_href = $ar[1];
294: $a_title = $ar[2];
295: for($i = 0; $i < count($a_tag);$i++){
296: $link_status = $this->db->from('xenon_link_gmreader')->where('link_id',base64_decode(urldecode($a_href[$i])))->where('link_status','y')->get();
297: $link_data_status = $this->db->from('xenon_data_links_gmreader')->where('link_id',base64_decode(urldecode($a_href[$i])))->where('data_id',$id)->where('status','y')->get();
298: if($link_status->num_rows() > 0 && $link_data_status->num_rows() > 0){
299: $txt = str_replace('&','&',str_replace('’', "'", $a_title[$i]));
300: $txt = htmlentities($txt);
301: $link_new = $a_href[$i];
302: $class = '';
303: if($txt == 'references'){
304: $link_text = $this->db->from('xenon_link_gmreader')->join('xenon_reference','xenon_link_gmreader.data_id = xenon_reference.refe_id')->where('link_id',base64_decode(urldecode($a_href[$i])))->get();
305: if($link_text->num_rows() > 0){
306: $tmp_row = $link_text->row();
307: $link_text = $tmp_row->refe_title;
308: $link_new = $tmp_row->refe_id;
309: $ref_count = $ref_count + 1;
310: $txt = '['.$ref_count.']';
311: $class = 'reference';
312: $refences .= '<div><a href="'.$link_new.'" class="reference-transfer" id="'.$ref_count.'">'.$ref_count.'. '.$link_text.'</a></div>';
313: $link_new = '#'.$ref_count;
314: $body = str_replace($a_tag[$i], '<a href="'.$link_new.'" class="reference"><pre>'.$txt.'</pre></a>', $body);
315: }else{
316: $body = str_replace($a_tag[$i], '',$body);
317: }
318: }else{
319: $link_text = $this->db->from('xenon_link_gmreader')->join('xenon_data','xenon_link_gmreader.data_id = xenon_data.data_id')->where('link_id',base64_decode(urldecode($a_href[$i])))->get();
320: if($link_text->num_rows() > 0){
321: $tmp_row = $link_text->row();
322: $link_text = $tmp_row->data_title;
323: $link_new = $tmp_row->data_id;
324: $body = str_replace($a_tag[$i], '<a href="'.$link_new.'" class="data">'.$txt.'</a>', $body);
325: }else{
326: $body = str_replace($a_tag[$i], '',$body);
327: }
328: }
329: }else{
330: $body = str_replace($a_tag[$i], '',$body);
331: }
332: }
333: $dom = new DOMDocument();
334: $dom->loadHTML($body);
335: $data['data_body_cache_gmreader'] = substr($dom->saveHTML(),119,-15).($refences!=''?'<div class="ref-container"><h2 class="ref-head">References</h2>'.$refences.'</div>':'');
336: $this->db->where('data_id',$id);
337: $this->db->update('xenon_data',$data);
338: }
339:
340: public function build_article_temp($body,$id){
341: $ref_count = 0;
342: $refences = '';
343: $dom = new DOMDocument();
344: $dom->loadHTML(html_entity_decode(mb_convert_encoding($body ,"HTML-ENTITIES","UTF-8")));
345: $a_tags = $dom->getElementsByTagName('a');
346: foreach ($a_tags as $a_tag) {
347: $txt = str_replace('&','&',str_replace('’', "'", $a_tag->nodeValue));
348: $txt = htmlentities($txt);
349: $link_href = str_replace("JavaScript:XdkPopup('",'',$a_tag->getAttribute('href'));
350: $link_href = str_replace("')",'',$link_href);
351: $link_href = str_replace("%20",' ',$link_href);
352:
353: $link_text = $this->db->from('xenon_link')->where('link_href',$link_href)->where('link_text',$txt)->get();
354: if($link_text->num_rows() == 0){
355: echo $id."::SELECT * FROM (`xenon_link`) WHERE `link_href` = '".$a_tag->getAttribute('href')."' AND `link_text` = '".$txt."'<br />";
356: $link_new = 0;
357: }else{
358: $link_new = urlencode(base64_encode($link_text->row()->link_id));
359:
360: }
361:
362: if($link_text->num_rows() != 0){
363:
364: if($this->db->from('xenon_data_links')->where('link_id',$link_text->row()->link_id)->where('data_id',$id)->get()->num_rows() <= 0){
365: $data_link_add['link_id'] = $link_text->row()->link_id;
366: $data_link_add['data_id'] = $id;
367: $data_link_add['status'] = 'y';
368: $this->db->insert('xenon_data_links',$data_link_add);
369: }
370: 371: 372: 373: 374: 375: 376: 377: 378:
379: }
380: $link = $dom->createElement('a',$txt);
381: $link->setAttribute('href', $link_new);
382: $a_tag->parentNode->replaceChild($link, $a_tag);
383:
384: }
385:
386: $data['data_body_temp'] = substr($dom->saveHTML(),119,-15).($refences!=''?'<h2>References</h2>'.$refences:'');
387: $this->db->where('data_id',$id);
388: $this->db->update('xenon_data',$data);
389: }
390:
391: public function build_article_temp_gmreader($body,$id){
392: $ref_count = 0;
393: $refences = '';
394: $dom = new DOMDocument();
395: $dom->loadHTML(html_entity_decode(mb_convert_encoding($body ,"HTML-ENTITIES","UTF-8")));
396: $a_tags = $dom->getElementsByTagName('a');
397: foreach ($a_tags as $a_tag) {
398: $txt = str_replace('&','&',str_replace('’', "'", $a_tag->nodeValue));
399: $txt = htmlentities($txt);
400: $link_href = str_replace("JavaScript:XdkPopup('",'',$a_tag->getAttribute('href'));
401: $link_href = str_replace("')",'',$link_href);
402: $link_href = str_replace("%20",' ',$link_href);
403:
404: $link_text = $this->db->from('xenon_link_gmreader')->where('link_href',$link_href)->where('link_text',$txt)->get();
405: if($link_text->num_rows() == 0){
406: echo $id."::SELECT * FROM (`xenon_link_gmreader`) WHERE `link_href` = '".$a_tag->getAttribute('href')."' AND `link_text` = '".$txt."'<br />";
407: $link_new = 0;
408: }else{
409: $link_new = urlencode(base64_encode($link_text->row()->link_id));
410:
411: }
412:
413: $link = $dom->createElement('a',$txt);
414: $link->setAttribute('href', $link_new);
415: $a_tag->parentNode->replaceChild($link, $a_tag);
416:
417: }
418:
419: return substr($dom->saveHTML(),119,-15).($refences!=''?'<h2>References</h2>'.$refences:'');
420: }
421:
422: public function article_article_add($id,$rid){
423: $link = $this->db->from('xenon_link')->where('data_id',$rid)->where('link_type','a')->get();
424: $refe = $this->db->from('xenon_data')->where('data_id',$rid)->get();
425:
426: if($link->num_rows() > 0){
427: $refe_link = $this->db->from('xenon_data_links')->where('data_id',$id)->where('link_id',$link->row()->link_id)->get();
428: if($refe_link->num_rows() <= 0){
429: $data['data_id'] = $id;
430: $data['link_id'] = $link->row()->link_id;
431: $this->db->insert('xenon_data_links',$data);
432: }
433: return array('<li>'.$refe->row()->data_title.'<a href="?dellink='.urlencode(base64_encode($link->row()->link_id)).'"><div class="delref"></div></a></li>','<a href="'.urlencode(base64_encode($link->row()->link_id)).'" class="blueref">'.$refe->row()->data_title.'</a>');
434: }else{
435: $data['link_class'] = 'blueref';
436: $data['data_id'] = $rid;
437: $data['link_text'] = 'references';
438: $data['link_href'] = $refe->row()->data_link;
439: $data['link_type'] = 'r';
440: $this->db->insert('xenon_link',$data);
441: $iddd = $this->db->insert_id();
442: $data['data_id'] = $id;
443: $data['link_id'] = $iddd;
444: $this->db->insert('xenon_data_links',$data);
445: return array('<li>'.$refe->row()->data_title.'<a href="?dellink='.urlencode(base64_encode($iddd)).'"><div class="delref"></div></a></li>','<a href="'.urlencode(base64_encode($iddd)).'" class="blueref">'.$refe->row()->data_title.'</a>');
446: }
447: }
448: }
449: ?>