PageRenderTime 70ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/application/models/Content.php

https://github.com/jiiarra/site
PHP | 1505 lines | 689 code | 177 blank | 639 comment | 112 complexity | c966b33875321bc1fd51891d9687717b MD5 | raw file
  1. <?php
  2. /**
  3. * Content -> Content database model for content table.
  4. *
  5. * Copyright (c) <2009>, Markus Riihel�
  6. * Copyright (c) <2009>, Mikko Sallinen
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
  16. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * License text found in /license/
  19. */
  20. /**
  21. * Content - class
  22. *
  23. * @package models
  24. * @author Markus Riihel� & Mikko Sallinen
  25. * @copyright 2009 Markus Riihel� & Mikko Sallinen
  26. * @license GPL v2
  27. * @version 1.0
  28. */
  29. class Default_Model_Content extends Zend_Db_Table_Abstract
  30. {
  31. // Table name
  32. protected $_name = 'contents_cnt';
  33. // Table primary key
  34. protected $_primary = 'id_cnt';
  35. // Dependet tables
  36. protected $_dependentTables = array('Default_Model_ContentRatings', 'Default_Model_Comments',
  37. 'Default_Model_Links', // 'Default_Model_ContentTypes',
  38. 'Default_Model_ContentHasTag', 'Default_Model_ContentHasIndustries',
  39. 'Default_Model_ContentHasInnovationTypes', 'Default_Model_ContentHasContent',
  40. 'Default_Model_ContentHasUser', 'Default_Model_ContentHasGroup',
  41. 'Default_Model_Files', 'Default_Model_ContentPublishTimes',
  42. 'Default_Model_UserHasFavourites');
  43. // Table reference map
  44. protected $_referenceMap = array(
  45. 'ContentType' => array(
  46. 'columns' => array('id_cty_cnt'),
  47. 'refTableClass' => 'Default_Model_ContentTypes',
  48. 'refColumns' => array('id_cty')
  49. ),
  50. /*
  51. 'ContentPublishTimes' => array(
  52. 'columns' => array('id_cnt'),
  53. 'refTableClass' => 'Default_Model_PublishTimes',
  54. 'refColumns' => array('id_cnt_pbt')
  55. ),
  56. 'ContentRatings' => array(
  57. 'columns' => array('id_cnt'),
  58. 'refTableClass' => 'Default_Model_PublishTimes',
  59. 'refColumns' => array('id_cnt_crt')
  60. ),
  61. */
  62. );
  63. protected $_id = 0;
  64. protected $_data = null;
  65. /**
  66. * __construct
  67. *
  68. * Content constructor.
  69. *
  70. * @param integer $id Content id value.
  71. */
  72. public function __construct($id = -1)
  73. {
  74. parent::__construct();
  75. $this->_id = $id;
  76. if ($id != -1){
  77. $this->_data = $this->find((int)$id)->current();
  78. } // end if
  79. }
  80. /**
  81. * listRecent
  82. *
  83. * List recent content by content type.
  84. *
  85. * @param string $cty
  86. * @return array
  87. */
  88. public function listRecent($cty = 'all', $page = 1, $count = -1, $order = 'created', $lang = 'en', $ind = 0)
  89. {
  90. switch ($order) {
  91. case 'author':
  92. $order = 'login_name_usr';
  93. break;
  94. case 'header':
  95. $order = 'title_cnt';
  96. break;
  97. case 'views':
  98. $order = 'viewCount ASC';
  99. break;
  100. default:
  101. $order = 'created_cnt DESC';
  102. }
  103. $select = $this->select()->from($this, "id_cnt")
  104. ->where('published_cnt = 1')
  105. ->order($order);
  106. if ($cty != "all") {
  107. $select->join('content_types_cty', 'content_types_cty.id_cty = contents_cnt.id_cty_cnt',array())
  108. ->where('content_types_cty.key_cty = ?', $cty);
  109. }
  110. if ($count > 0){
  111. $select->limitPage($page, $count);
  112. } else {
  113. $select->limit($page);
  114. }
  115. // Content data
  116. //$data = $this->_db->fetchAll($select);
  117. $ids = $this->fetchAll($select);
  118. $data = $this->getContentRows($ids->toArray(), 'id_cnt', true);
  119. return $data;
  120. }
  121. /* getcontentRows
  122. *
  123. * Function to get data for content_row partial from given id parameters
  124. *
  125. * @param ids array array of arrays with content_id in it
  126. * @param id_cnt string what is the key of id_cnt in the ids array
  127. * @param sort bool sorts the data according to given id array or not
  128. * @return array array of all data needed for content_row partial
  129. */
  130. public function getContentRows($ids, $id_cnt = 'id_cnt', $sort = false) {
  131. if (empty($ids)) {
  132. return array();
  133. }
  134. $select = $this->_db->select()->from("contents_cnt", array( "id_cnt",
  135. "title_cnt",
  136. "lead_cnt",
  137. "language_cnt",
  138. "created_cnt"))
  139. ->joinLeft( "cnt_has_usr",
  140. "contents_cnt.id_cnt = cnt_has_usr.id_cnt",
  141. array())
  142. ->join( "users_usr",
  143. "users_usr.id_usr = cnt_has_usr.id_usr",
  144. array("login_name_usr", "id_usr"))
  145. ->joinLeft( "content_types_cty",
  146. "content_types_cty.id_cty = contents_cnt.id_cty_cnt",
  147. array("id_cty", "key_cty"))
  148. ->joinLeft( array("chu" => "cnt_has_usr"),
  149. "cnt_has_usr.id_usr = chu.id_usr",
  150. array("count" => "count(*)"))
  151. ->group('contents_cnt.id_cnt')
  152. ->where('contents_cnt.id_cnt IN (?)', $ids)
  153. ;
  154. $data = $this->_db->fetchAll($select);
  155. if ($sort) {
  156. $idList = array();
  157. foreach ($ids as $id) {
  158. $idList[] = $id[$id_cnt];
  159. }
  160. $idList = array_flip($idList);
  161. $sortedData = array();
  162. foreach ($data as $row) {
  163. if(isset($idList[$row['id_cnt']])) $sortedData[$idList[$row['id_cnt']]] = $row;
  164. }
  165. ksort($sortedData);
  166. return $sortedData;
  167. }
  168. return $data;
  169. }
  170. /**
  171. * getContentCountByContentType
  172. *
  173. * Get total content count by content type.
  174. *
  175. * @param string $cty Content type
  176. * @return array
  177. */
  178. public function getContentCountByContentType($cty, $lng)
  179. {
  180. $select = $this->_db->select()->from(array('cty' => 'content_types_cty'),
  181. array())
  182. ->join(array('cnt' => 'contents_cnt'),
  183. 'cnt.id_cty_cnt = cty.id_cty',
  184. array('contentCount' => 'COUNT(cnt.id_cnt)'))
  185. ->where('cty.key_cty = ?', $cty)
  186. ->where('cnt.published_cnt = 1');
  187. //->where('cnt.language_cnt = ?', $lng);
  188. $data = $this->_db->fetchAll($select);
  189. return $data[0]['contentCount'];
  190. }
  191. /**
  192. * getById
  193. *
  194. * Get content by id.
  195. * Is this function used anywhere?
  196. * If not, this function should probably be removed.
  197. *
  198. * @param ineteger $id
  199. * @return array
  200. */
  201. public function getById($id = 0)
  202. {
  203. // Array for content data
  204. $data = array();
  205. // Find content row by id
  206. $rowset = $this->find((int)$id)->current();
  207. // If content was found
  208. if(count($rowset) == 1) {
  209. // Content data
  210. $content_data = $rowset->toArray();
  211. // Select content ratings
  212. $select_ratings = $this->select()->from('content_ratings_crt', array('SUM(rating_crt) AS rate_crt'));
  213. $ratings = $rowset->findDependentRowset('Default_Model_ContentRatings', 'RatingsContent', $select_ratings)->toArray();
  214. // Find content owner
  215. $content_owner = $rowset->findManyToManyRowset('Default_Model_User', 'Default_Model_ContentHasUser');
  216. // Find content comments
  217. $select_comment = $this->select()->order('created_cmt ASC');
  218. $cmtModel = new Default_Model_Comments();
  219. $comments = $cmtModel->getCommentsByContent($id); //$rowset->findDependentRowset('Default_Model_Comments', 'CommentContent', $select_comment);
  220. // Find content keywords
  221. $tags = $rowset->findManyToManyRowset('Default_Model_Tags', 'Default_Model_ContentHasTag')->toArray();
  222. // Find content links
  223. $links = $rowset->findDependentRowset('Default_Model_Links')->toArray();
  224. // Find related content
  225. $related_content = $rowset->findManyToManyRowset('Default_Model_Content', 'Default_Model_ContentHasContent', 'ParentContent', 'ChildContent')->toArray();
  226. // Array for comment owners
  227. $comment_owners = array();
  228. // Go through all comments
  229. foreach($comments as $cmt) {
  230. // Find comment owner
  231. $usr = $cmt->findDependentRowset('Default_Model_User', 'CommentUser')->toArray();
  232. // If owner found
  233. if(!empty($usr)) {
  234. // Specify comment owner
  235. $comment_owners[$usr[0]['id_usr']] = $usr[0];
  236. } // end if
  237. } // end foreach
  238. // Gather content data
  239. $data['Content']['Data'] = $content_data;
  240. $data['Content']['Poster'] = $content_owner;
  241. $data['Content']['Tags'] = $tags;
  242. $data['Content']['Links'] = $links;
  243. $data['Content']['Related'] = $related_content;
  244. $data['Ratings'] = $ratings;
  245. $data['Comments']['Data'] = $comments;
  246. $data['Comments']['Posters'] = $comment_owners;
  247. }
  248. return $data;
  249. } // end of getById
  250. /**
  251. * getContentRow
  252. *
  253. * Get content data by id
  254. *
  255. * @param integer $id content id
  256. * @return array
  257. */
  258. public function getContentRow($id = -1)
  259. {
  260. if($id == -1) {
  261. $id = $this->_id;
  262. } // end if
  263. return $this->find((int)$id)->current()->toArray();
  264. } // end of getContentRow
  265. /**
  266. *
  267. *
  268. */
  269. /*
  270. public function getByAuthor($author_id = 0)
  271. {
  272. //$contentUser = new Default_Model_ContentHasUser();
  273. //$select = $this->select()->where('')
  274. //$this->findDependentRowset('Default_Model_ContentHasUser', );
  275. //$select = $this->_db->select()
  276. // ->from('contents_cnt', array('*'))
  277. // ->where('id_usr_cnt = ?', $author_id);
  278. //$stmt = $this->_db->query($select);
  279. //$result = $stmt->fetchAll();
  280. //return $result;
  281. }
  282. */
  283. /* getRelatedContents
  284. *
  285. * gets all contents that share tags with specified content
  286. *
  287. * @param int id content id
  288. * @param int limit limit to N contents
  289. * @return array array of title_cnt, id_cnt, viewCount, contentType
  290. */
  291. public function getRelatedContents($id, $limit = -1) {
  292. $tags = $this->getTagIdsByContentId($id);
  293. $linkedContents = array();
  294. $contents = array();
  295. if (!empty($tags)) {
  296. $cntHasTagModel = new Default_Model_ContentHasTag();
  297. $select = $cntHasTagModel->select()
  298. ->from('cnt_has_tag', array('id_cnt'))
  299. ->where('id_tag IN (?)', $tags)
  300. ->where('id_cnt != ?', $id);
  301. if($limit != -1) $select->limit($limit);
  302. $contents = $cntHasTagModel->fetchAll($select)->toArray();
  303. }
  304. $cnthascntModel = new Default_Model_ContentHasContent();
  305. $contents = array_merge($contents, $cnthascntModel->getContentLinkIds($id));
  306. $linkedContents = $this->find($contents);
  307. $viewsModel = new Default_Model_ContentViews();
  308. $rows = array();
  309. foreach ($linkedContents as $row) {
  310. $tempRow = array();
  311. $tempRow['title_cnt'] = $row->title_cnt;
  312. $tempRow['id_cnt'] = $row->id_cnt;
  313. $tempRow['language_cnt']= $row->language_cnt;
  314. $tempRow['viewCount'] = $viewsModel->getViewsByContentId($row->id_cnt);
  315. $tempRow['contentType'] = $row->findDependentRowset('Default_Model_ContentTypes', 'ContentType')->current()->key_cty;
  316. array_push($rows, $tempRow);
  317. if ($limit != -1 && count($rows) >= $limit) break;
  318. }
  319. return $rows;
  320. }
  321. /* getTagNamesByContentId
  322. * gets tag names by content id
  323. *
  324. * @param id_cnt content id
  325. * @return array (name_tag, name_tag, ...)
  326. */
  327. public function getTagNamesByContentId($id_cnt) {
  328. $content = $this->find($id_cnt)->current();
  329. $contentTagIds = $content->findDependentRowset('Default_Model_ContentHasTag', 'TagContent');
  330. $tagsArray = array();
  331. foreach ($contentTagIds as $tagId) {
  332. $tag = $tagId->findDependentRowset('Default_Model_Tags', 'TagTag')->current();
  333. array_push($tagsArray, $tag->name_tag);
  334. }
  335. return $tagsArray;
  336. }
  337. /*getTagIdsByContentId
  338. *
  339. * Gets all tag ids linked to content
  340. *
  341. * @param id_cnt content id
  342. * @return array (id_tag, id_tag, ...) tag ids
  343. */
  344. public function getTagIdsByContentId($id_cnt) {
  345. $content = $this->find($id_cnt)->current();
  346. $contentTags = $content->findDependentRowset('Default_Model_ContentHasTag', 'TagContent');
  347. $ids = array();
  348. foreach ($contentTags as $tag) {
  349. array_push($ids, $tag->id_tag);
  350. }
  351. return $ids;
  352. }
  353. /**
  354. * getByName
  355. *
  356. * Gets content by name from database. This is used in search function.
  357. *
  358. * This function rapes your database, when there's lots of content.
  359. * To fix this the <LIKE %searchTerm%> in query
  360. * should be replaced with something more efficient.
  361. * Also should find a better way to get total count of results.
  362. *
  363. * @param string $searchWord
  364. */
  365. public function getSearchResult($searchword = null, $page = 1, $count = 10, $order = 'created')
  366. {
  367. switch ($order) {
  368. case 'author':
  369. $order = 'usr.login_name_usr';
  370. break;
  371. case 'header':
  372. $order = 'cnt.title_cnt';
  373. break;
  374. case 'views':
  375. $order = 'viewCount DESC';
  376. break;
  377. default:
  378. $order = 'cnt.created_cnt DESC';
  379. }
  380. $contentEntries = array();
  381. // enable empty searches as content listing
  382. if ($searchword == NULL || $searchword == "") {
  383. $searchword = "%";
  384. } else {
  385. $searchword = '%'.$searchword.'%';
  386. }
  387. $select = $this->_db->select()->from(array('cty' => 'content_types_cty'),
  388. array('cty.key_cty'))
  389. ->joinLeft(array('cnt' => 'contents_cnt'),
  390. 'cnt.id_cty_cnt = cty.id_cty',
  391. array('cnt.id_cnt',
  392. 'cnt.title_cnt',
  393. 'cnt.lead_cnt',
  394. 'cnt.created_cnt'))
  395. ->joinLeft(array('cht' => 'cnt_has_tag'),
  396. 'cht.id_cnt = cnt.id_cnt',
  397. array())
  398. ->joinLeft(array('tag' => 'tags_tag'),
  399. 'cht.id_tag = tag.id_tag',
  400. array())
  401. ->joinLeft(array('chu' => 'cnt_has_usr'),
  402. 'chu.id_cnt = cnt.id_cnt',
  403. array())
  404. ->joinLeft(array('vws' => 'cnt_views_vws'),
  405. 'cnt.id_cnt = vws.id_cnt_vws',
  406. array('viewCount' => 'SUM(DISTINCT vws.views_vws)'))
  407. ->joinLeft(array('usr' => 'users_usr'),
  408. 'chu.id_usr = usr.id_usr',
  409. array('usr.id_usr', 'usr.login_name_usr'))
  410. ->where('cnt.published_cnt = 1')
  411. // extra "(" hacks AND statements together
  412. ->where('(cnt.title_cnt LIKE ?', $searchword)
  413. ->orWhere('cnt.lead_cnt LIKE ?', $searchword)
  414. ->orWhere('cnt.body_cnt LIKE ?', $searchword)
  415. ->orWhere('tag.name_tag LIKE ?)',$searchword)
  416. ->group('cnt.id_cnt')
  417. ->order($order)
  418. ->limitPage($page, $count);
  419. $contentEntries = $this->_db->fetchAll($select);
  420. return $contentEntries;
  421. } // end of getByName
  422. /**
  423. * getContentCountBySearch
  424. *
  425. * Get total content count by search.
  426. *
  427. * This also helps to rape your database.
  428. * Should find a better way to retrieve the total
  429. * count of content, that search finds.
  430. *
  431. * @param string $searchword
  432. * @return int amount
  433. */
  434. public function getContentCountBySearch($searchword = null)
  435. {
  436. if ($searchword == null) {
  437. $searchword = "%";
  438. } else {
  439. $searchword = '%'.$searchword.'%';
  440. }
  441. $select = $this->_db->select()->from(array('cnt' => 'contents_cnt'),
  442. array('contentCount' => 'COUNT(DISTINCT cnt.id_cnt)'))
  443. ->joinLeft(array('cht' => 'cnt_has_tag'),
  444. 'cht.id_cnt = cnt.id_cnt',
  445. array())
  446. ->joinLeft(array('tag' => 'tags_tag'),
  447. 'cht.id_tag = tag.id_tag',
  448. array())
  449. ->where('cnt.published_cnt = 1')
  450. ->where('cnt.title_cnt LIKE ?', $searchword)
  451. ->orWhere('cnt.lead_cnt LIKE ?', $searchword)
  452. ->orWhere('cnt.body_cnt LIKE ?', $searchword)
  453. ->orWhere('tag.name_tag LIKE ?',$searchword);
  454. $data = $this->_db->fetchAll($select);
  455. if (isset($data[0]['contentCount'])) {
  456. return $data[0]['contentCount'];
  457. }
  458. return 0;
  459. }
  460. /**
  461. * addContent
  462. *
  463. * Add content.
  464. *
  465. * @param array $data
  466. */
  467. public function addContent($data)
  468. {
  469. $auth = Zend_Auth::getInstance();
  470. // Create a new row
  471. $content = $this->createRow();
  472. //Zend_Debug::dump($content, $label=null, $echo=true);
  473. // Set data to row
  474. $content->id_cty_cnt = $data['content_type'];
  475. $content->title_cnt = htmlspecialchars($data['content_header']);
  476. $content->lead_cnt = htmlspecialchars($data['content_textlead']);
  477. $content->body_cnt = htmlspecialchars($data['content_text']);
  478. if(isset($data['content_research'])) {
  479. $content->research_question_cnt = htmlspecialchars($data['content_research']);
  480. }
  481. if(isset($data['content_opportunity'])) {
  482. $content->opportunity_cnt = htmlspecialchars($data['content_opportunity']);
  483. }
  484. if(isset($data['content_threat'])) {
  485. $content->threat_cnt = htmlspecialchars($data['content_threat']);
  486. }
  487. if(isset($data['content_solution'])) {
  488. $content->solution_cnt = htmlspecialchars($data['content_solution']);
  489. }
  490. $content->references_cnt = $data['content_references'];
  491. $content->published_cnt = $data['publish'];
  492. $content->created_cnt = new Zend_Db_Expr('NOW()');
  493. $content->modified_cnt = new Zend_Db_Expr('NOW()');
  494. $content->language_cnt = $data['content_language'];
  495. //Zend_Debug::dump($content, $label=null, $echo=true); die();
  496. /*$query = "INSERT INTO contents_cnt (id_cty_cnt, id_ind_cnt, title_cnt, lead_cnt, body_cnt, views_cnt, published_cnt, created_cnt, modified_cnt) ";
  497. $query .= "VALUES (".$data['content_type'].", ".$data['content_industry_id'].", '".strip_tags($data['content_header'])."', '".strip_tags($data['content_textlead'])."', '";
  498. $query .= strip_tags($data['content_text'])."', 0, 0, ".new Zend_Db_Expr('NOW()').", ".new Zend_Db_Expr('NOW()').")";*/
  499. //Zend_Debug::dump($query, $label=null, $echo=true); die();
  500. /*mysql_connect("localhost", "root", "lollero");
  501. mysql_query($query);
  502. echo mysql_error();
  503. mysql_close();
  504. die();*/
  505. if(!$content->save()) {
  506. $return = false;
  507. } else {
  508. // If save was successful, content id is returned, because it is needed
  509. // when redirecting
  510. $return = $content->id_cnt;
  511. /*if($_FILES['content_file_upload']['size'] != 0) {
  512. $files = new Default_Model_Files();
  513. $files->newFile($content->id_cnt, $auth->getIdentity()->user_id);
  514. }*/
  515. $filesModel = new Default_Model_Files();
  516. for ($i=1;$i < count($data['files']['name']);$i++)
  517. {
  518. $files = $data['files'];
  519. $file['name'] = $files['name'][$i];
  520. $file['type'] = $files['type'][$i];
  521. $file['tmp_name'] = $files['tmp_name'][$i];
  522. $file['error'] = $files['error'][$i];
  523. $file['size'] = $files['size'][$i];
  524. $filesModel->newFile($content->id_cnt, $data['User']['id_usr'], $file);
  525. }
  526. }
  527. // What is this used for
  528. //$contentTypes = new Default_Model_ContentTypes();
  529. //$content_type = $contentTypes->getTypeById($data['content_type']);
  530. if($data['content_relatesto_id'] != 0) {
  531. $contentHasContent = new Default_Model_ContentHasContent();
  532. $contentHasContent->addContentToContent(
  533. $data['content_relatesto_id'],
  534. $content->id_cnt
  535. );
  536. }
  537. // Add user to content
  538. $contentHasUser = new Default_Model_ContentHasUser();
  539. $contentHasUser->addUserToContent($content->id_cnt, $data['User']['id_usr'], 1);
  540. // Check if user has given keywords
  541. if(!empty($data['content_keywords'])) {
  542. $tagModel = new Default_Model_Tags();
  543. $tagModel->addTagsToContent(
  544. $content->id_cnt, $data['content_keywords']
  545. );
  546. // Go through all given keywords
  547. // This should be in Tags model
  548. /*
  549. foreach($data['content_keywords'] as $tag) {
  550. $tagRow = new Default_Model_Tags();
  551. $tag = strip_tags($tag);
  552. // Check if given keyword does not exists in database
  553. if($tagRow->tagExists($tag)) {
  554. // Create new keyword
  555. $tag = $tagRow->createTag($tag);
  556. } else {
  557. // Get keyword
  558. $tag = $tagRow->getTag($tag);
  559. } // end else
  560. // echo '<pre>';echo $tag->id_tag.' '.$content->id_cnt;echo '</pre>';
  561. //die();
  562. // Add keywords to content
  563. $contentHasTag = new Default_Model_ContentHasTag();
  564. $contentHasTag->addTagToContent($tag->id_tag, $content->id_cnt);
  565. } // end foreach
  566. */
  567. } // end if
  568. // Check if user has given related companies
  569. if(!empty($data['content_related_companies'])) {
  570. $recModel = new Default_Model_RelatedCompanies();
  571. $recModel->addRelatedCompaniesToContent(
  572. $content->id_cnt, $data['content_related_companies']
  573. );
  574. // Go through all given related companies
  575. // FIX: This should be in RelatedCompanies model
  576. /*foreach($data['content_related_companies'] as $relComp) {
  577. $relCompRow = new Default_Model_RelatedCompanies();
  578. $relComp = strip_tags($relComp);
  579. // Check if given related company does not exists in database
  580. if($relCompRow->relCompExists($relComp)) {
  581. // Create new related company
  582. $relComp = $relCompRow->createRelComp($relComp);
  583. } else {
  584. // Get related company
  585. $relComp = $relCompRow->getRelComp($relComp);
  586. } // end else
  587. // echo '<pre>';echo $tag->id_tag.' '.$content->id_cnt;echo '</pre>';
  588. //die();
  589. // Add related companies to content
  590. $contentHasRelatedCompany = new Default_Model_ContentHasRelatedCompany();
  591. $contentHasRelatedCompany->addRelCompToContent($relComp->id_rec, $content->id_cnt);
  592. } // end foreach */
  593. } // end if
  594. // Check if user has given campaigns
  595. if(!empty($data['content_campaigns'])) {
  596. $cmpModel = new Default_Model_Campaigns();
  597. $cmpModel->addCampaignsToContent(
  598. $content->id_cnt, $data['content_campaigns']
  599. );
  600. // Go through all given campaigns
  601. // FIX: This should be in Campaigns model
  602. /*foreach($data['content_campaigns'] as $campaign) {
  603. $campaignRow = new Default_Model_Campaigns();
  604. $campaign = strip_tags($campaign);
  605. // Check if given campaign does not exists in database
  606. if($campaignRow->campaignExists($campaign)) {
  607. // Create new campaign
  608. $campaign = $campaignRow->createCampaign($campaign);
  609. } else {
  610. // Get campaign
  611. $campaign = $campaignRow->getCampaign($campaign);
  612. } // end else
  613. // echo '<pre>';echo $tag->id_tag.' '.$content->id_cnt;echo '</pre>';
  614. //die();
  615. // Add related companies to content
  616. $contentHasCampaign = new Default_Model_ContentHasCampaign();
  617. $contentHasCampaign->addCampaignToContent($campaign->id_cmp, $content->id_cnt);
  618. } // end foreach */
  619. } // end if
  620. // Add industry to content
  621. $contentHasIndustry = new Default_Model_ContentHasIndustries();
  622. if(isset($data['content_industry'])) {
  623. $id_ind = 0;
  624. if($data['content_class'] != 0) {
  625. $id_ind = $data['content_class'];
  626. } elseif($data['content_group'] != 0) {
  627. $id_ind = $data['content_group'];
  628. } elseif($data['content_division'] != 0) {
  629. $id_ind = $data['content_division'];
  630. } elseif($data['content_industry'] != 0) {
  631. $id_ind = $data['content_industry'];
  632. }
  633. }
  634. if($id_ind != 0) {
  635. $contentHasIndustry->addIndustryToContent($content->id_cnt, $id_ind);
  636. }
  637. // Add future info classification to content
  638. if(isset($data['content_finfo_class'])) {
  639. if($data['content_finfo_class'] != 0) {
  640. $contentHasFutureinfoClass = new Default_Model_ContentHasFutureinfoClasses();
  641. $contentHasFutureinfoClass->addFutureinfoClassToContent($content->id_cnt, $data['content_finfo_class']);
  642. }
  643. }
  644. // Add innovation type to content
  645. if(isset($data['innovation_type'])) {
  646. if($data['innovation_type'] != 0) {
  647. $contentHasInnovationType = new Default_Model_ContentHasInnovationTypes();
  648. $contentHasInnovationType->addInnovationTypeToContent($content->id_cnt, $data['innovation_type']);
  649. }
  650. }
  651. return $return;
  652. } // end of addContent
  653. /**
  654. * editContent
  655. *
  656. * Edit content.
  657. *
  658. * @param array $data
  659. */
  660. public function editContent($data)
  661. {
  662. // Get the original content
  663. $content = $this->getContentRow($data['content_id']);
  664. // Get content type
  665. $contentTypes = new Default_Model_ContentTypes();
  666. $contentType = $contentTypes->getTypeById($content['id_cty_cnt']);
  667. // Unset fields that are not going to be updated
  668. unset($content['id_cnt']);
  669. unset($content['id_cty_cnt']);
  670. unset($content['views_cnt']);
  671. unset($content['created_cnt']);
  672. $content['title_cnt'] = htmlspecialchars($data['content_header']);
  673. $content['lead_cnt'] = htmlspecialchars($data['content_textlead']);
  674. $content['language_cnt'] = $data['content_language'];
  675. $content['body_cnt'] = htmlspecialchars($data['content_text']);
  676. if(!isset($data['content_research'])) {
  677. $data['content_research'] = "";
  678. }
  679. if(!isset($data['content_opportunity'])) {
  680. $data['content_opportunity'] = "";
  681. }
  682. if(!isset($data['content_threat'])) {
  683. $data['content_threat'] = "";
  684. }
  685. if(!isset($data['content_solution'])) {
  686. $data['content_solution'] = "";
  687. }
  688. $content['research_question_cnt'] = htmlspecialchars($data['content_research']);
  689. $content['opportunity_cnt'] = htmlspecialchars($data['content_opportunity']);
  690. $content['threat_cnt'] = htmlspecialchars($data['content_threat']);
  691. $content['solution_cnt'] = htmlspecialchars($data['content_solution']);
  692. $content['references_cnt'] = htmlspecialchars($data['content_references']);
  693. $content['modified_cnt'] = new Zend_Db_Expr('NOW()');
  694. if (isset($data['publish']) && $data['publish'] == 1)
  695. $content['published_cnt'] = 1;
  696. $where = $this->getAdapter()->quoteInto('`id_cnt` = ?', $data['content_id']);
  697. // MIK� VITTU T�SS� KUSEE?
  698. if(!$this->update($content, $where)) {
  699. $return = false;
  700. } else {
  701. $return = $data['content_id'];
  702. }
  703. // Check if user has given keywords
  704. if(!empty($data['content_keywords'])) {
  705. // Get existing keywords of the content
  706. $cntHasTag = new Default_Model_ContentHasTag();
  707. $existingTags = $cntHasTag->checkExistingTags(
  708. $data['content_id'], $data['content_keywords']
  709. );
  710. //$existingTags = $cntHasTag->getContentTags($data['content_id']);
  711. $modelTags = new Default_Model_Tags();
  712. $modelTags->addTagsToContent(
  713. $data['content_id'], $data['content_keywords'], $existingTags
  714. );
  715. /*
  716. $i = 0;
  717. // Go through all existing keywords
  718. // This belongs to contentHasTags model
  719. foreach($existingTags as $existingTag) {
  720. // If some of the existing keywords aren't found in sent keywords,
  721. // that keyword is deleted the from content and maybe even from the
  722. // database
  723. if(!in_array($existingTag['name_tag'], $data['content_keywords'])) {
  724. // Removing tag from content
  725. $cntHasTag->deleteTagFromContent($existingTag['id_tag'], $data['content_id']);
  726. // If other content(s) doesn't have this tag, the whole
  727. // tag is going to be removed from the database
  728. if(!$cntHasTag->checkIfOtherContentHasTag($existingTag['id_tag'], $data['content_id'])) {
  729. $modelTags->removeTag($existingTag['id_tag']);
  730. }
  731. // Remove tag from existingTags array
  732. unset($existingTags[$i]);
  733. }
  734. $i++;
  735. }
  736. */
  737. /*
  738. // Go through all sent keywords
  739. // This belongs to Tags model
  740. foreach($data['content_keywords'] as $tag) {
  741. $tag = strip_tags($tag);
  742. $foundTag = false;
  743. foreach($existingTags as $existingTag) {
  744. if($tag == $existingTag['name_tag']) {
  745. $foundTag = true;
  746. }
  747. }
  748. // If tag is not found in existing tags
  749. if(!$foundTag) {
  750. // Check if given keyword does not exists in database
  751. if($modelTags->tagExists($tag)) {
  752. // Create new keyword
  753. $tag = $modelTags->createTag($tag);
  754. } else {
  755. // Get keyword
  756. $tag = $modelTags->getTag($tag);
  757. } // end else
  758. // Add keywords to content
  759. $cntHasTag->addTagToContent($tag->id_tag, $data['content_id']);
  760. }
  761. } // end foreach
  762. */
  763. } // end if
  764. // Check if user has related companies
  765. if(!empty($data['content_related_companies'])) {
  766. // Get existing related companies of the content
  767. $cntHasRec = new Default_Model_ContentHasRelatedCompany();
  768. $existingCompanies = $cntHasRec->checkExistingCompanies(
  769. $data['content_id'], $data['content_related_companies']
  770. );
  771. //$existingRecs = $cntHasRec->getContentRelComps($data['content_id']);
  772. $modelRecs = new Default_Model_RelatedCompanies();
  773. $modelRecs->addRelatedCompaniesToContent(
  774. $data['content_id'], $data['content_related_companies'], $existingCompanies
  775. );
  776. /*
  777. $i = 0;
  778. // Go through all existing related companies
  779. // FIX: This belongs to ContentHasRelatedCompany model
  780. foreach($existingRecs as $existingRec) {
  781. // If some of the existing related companies aren't found in sent
  782. // related companies, that related company is deleted from the
  783. // content and maybe even from thedatabase
  784. // FIXED: Could have caused mismatches when adding,
  785. // since when related company is added strip_tags is used
  786. // but not when doing comparisons here.
  787. // This goes for campaigns as well.
  788. if(!in_array($existingRec['name_rec'], $data['content_related_companies'])) {
  789. // Removing rec from content
  790. $cntHasRec->deleteRelCompFromContent($existingRec['id_rec'], $data['content_id']);
  791. // If other content(s) doesn't have this related company, the whole
  792. // related company is going to be removed from the database
  793. if(!$cntHasRec->checkIfOtherContentHasRelComp($existingRec['id_rec'], $data['content_id'])) {
  794. $modelRecs->removeRelComp($existingRec['id_rec']);
  795. }
  796. // Remove related company from existingRecs array
  797. unset($existingRecs[$i]);
  798. }
  799. $i++;
  800. }
  801. */
  802. /*
  803. // Go through all sent related companies
  804. // FIX: This belongs to RelatedCompany model
  805. foreach($data['content_related_companies'] as $rec) {
  806. //$rec = strip_tags($rec);
  807. $foundRec = false;
  808. foreach($existingRecs as $existingRec) {
  809. if($rec == $existingRec['name_rec']) {
  810. $foundRec = true;
  811. }
  812. }
  813. // If related company is not found in existing related companies
  814. if(!$foundRec) {
  815. // Check if given related company does not exists in database
  816. if($modelRecs ->relCompExists($rec)) {
  817. // Create new related company
  818. $rec = $modelRecs ->createRelComp($rec);
  819. } else {
  820. // Get related company
  821. $rec = $modelRecs ->getRelComp($rec);
  822. } // end else
  823. // Add related company to content
  824. $cntHasRec->addRelCompToContent($rec->id_rec, $data['content_id']);
  825. }
  826. } // end foreach
  827. */
  828. } // end if
  829. /*
  830. if($_FILES['content_file_upload']['size'] != 0) {
  831. $files = new Default_Model_Files();
  832. $files->newFile($content->id_cnt, $auth->getIdentity()->user_id);
  833. }
  834. */
  835. // Check if user has given campaigns
  836. if(!empty($data['content_campaigns'])) {
  837. // Get existing campaigns of the content
  838. $cntHasCmp = new Default_Model_ContentHasCampaign();
  839. $existingCampaigns = $cntHasCmp->checkExistingCampaigns(
  840. $data['content_id'], $data['content_campaigns']
  841. );
  842. //$existingCmps = $cntHasCmp->getContentCampaigns($data['content_id']);
  843. $modelCmps = new Default_Model_Campaigns();
  844. $modelCmps->addCampaignsToContent(
  845. $data['content_id'], $data['content_campaigns'], $existingCampaigns
  846. );
  847. /*
  848. $i = 0;
  849. // Go through all existing campaigns
  850. // FIX: This belongs to ContentHasCampaign moodel
  851. foreach($existingCmps as $existingCmp) {
  852. // If some of the existing campaigns aren't found in sent campaigns,
  853. // that campaign is deleted the from content and maybe even from the
  854. // database
  855. if(!in_array($existingCmp['name_cmp'], $data['content_campaigns'])) {
  856. // Removing campaign from content
  857. $cntHasCmp->deleteCampaignFromContent($existingCmp['id_cmp'], $data['content_id']);
  858. // If other content(s) doesn't have this campaign, the whole
  859. // campaign is going to be removed from the database
  860. if(!$cntHasCmp->checkIfOtherContentHasCampaign($existingCmp['id_cmp'], $data['content_id'])) {
  861. $modelCmps->removeCampaign($existingCmp['id_cmp']);
  862. }
  863. // Remove campaign from existingCmps array
  864. unset($existingCmps[$i]);
  865. }
  866. $i++;
  867. }
  868. */
  869. /*
  870. // Go through all sent campaigns
  871. // FIX: This belongs to Campaigns model
  872. foreach($data['content_campaigns'] as $cmp) {
  873. //$cmp = strip_tags($cmp);
  874. $foundCmp = false;
  875. foreach($existingCmps as $existingCmp) {
  876. if($cmp == $existingCmp['name_cmp']) {
  877. $foundCmp = true;
  878. }
  879. }
  880. // If campaign is not found in existing tags
  881. if(!$foundCmp) {
  882. // Check if given campaign does not exists in database
  883. if($modelCmps->campaignExists($cmp)) {
  884. // Create new campaign
  885. $cmp = $modelCmps->createCampaign($cmp);
  886. } else {
  887. // Get campaign
  888. $cmp = $modelCmps->getCampaign($cmp);
  889. } // end else
  890. // Add campaigns to content
  891. $cntHasCmp->addCampaignToContent($cmp->id_cmp, $data['content_id']);
  892. }
  893. } // end foreach
  894. */
  895. } // end if
  896. /*// Update industry to content
  897. $contentHasIndustry = new Default_Model_ContentHasIndustries();
  898. $current_industry = $contentHasIndustry->getIndustryIdOfContent($data['content_id']);
  899. if($current_industry != $data['content_industry_id']) {
  900. if(!$contentHasIndustry->updateIndustryToContent($data['content_industry_id'], $data['content_id'])) {
  901. $return = false;
  902. }
  903. }*/
  904. if ($return) {
  905. $filesModel = new Default_Model_Files();
  906. for ($i=1;$i < count($data['files']['name']);$i++)
  907. {
  908. $files = $data['files'];
  909. $file['name'] = $files['name'][$i];
  910. $file['type'] = $files['type'][$i];
  911. $file['tmp_name'] = $files['tmp_name'][$i];
  912. $file['error'] = $files['error'][$i];
  913. $file['size'] = $files['size'][$i];
  914. $filesModel->newFile($data['content_id'], $data['User']['id_usr'], $file);
  915. }
  916. if (isset($data['uploadedFiles'])) $filesModel->deleteFiles($data['uploadedFiles']);
  917. //die;
  918. }
  919. if($contentType == "idea") {
  920. // Update innovation type to content
  921. $contentHasInnovationType = new Default_Model_ContentHasInnovationTypes();
  922. $current_innovation_type = $contentHasInnovationType->getInnovationTypeIdOfContent($data['content_id']);
  923. if($current_innovation_type != $data['innovation_type']) {
  924. if(!$contentHasInnovationType->updateInnovationTypeToContent($data['innovation_type'], $data['content_id'])) {
  925. $return = false;
  926. }
  927. }
  928. }
  929. return $return;
  930. }
  931. /**
  932. * publishContent
  933. * Publishes specified content
  934. *
  935. * @param int id_cnt The id of content to be published
  936. * @param(optional) int pubFlag Value for "published"-flag, set to true (1) by default.
  937. * @return bool $return
  938. * @author Pekka Piispanen
  939. */
  940. public function publishContent($id_cnt = 0, $pubFlag = 1)
  941. {
  942. $return = false;
  943. $data = array('published_cnt' => $pubFlag);
  944. $where = $this->getAdapter()->quoteInto('id_cnt = ?', (int)$id_cnt);
  945. if($this->update($data, $where)) {
  946. $return = true;
  947. }
  948. return $return;
  949. }
  950. /**
  951. * removeContent
  952. * Removes specified content from the database
  953. *
  954. * @param int id_cnt The id of content to be removed
  955. * @return bool $return
  956. * @author Pekka Piispanen
  957. */
  958. public function removeContent($id_cnt = 0)
  959. {
  960. $return = false;
  961. $content = new Default_Model_Content();
  962. if($this->delete("id_cnt = ".$id_cnt)) {
  963. $return = true;
  964. }
  965. return $return;
  966. } // end of removeContent
  967. /**
  968. * removeContent
  969. * Removes specified content from the database and all related stuff
  970. *
  971. * @param int id_cnt The id of content to be removed
  972. * @return boolean array $contentRemoveChecker
  973. * @author Mikko Korpinen
  974. */
  975. public function removeContentAndDepending($id_cnt = 0)
  976. {
  977. $contentRemoveChecker = array(
  978. 'removeContentFromCampaign' => true,
  979. 'removeContentFromContent' => true,
  980. 'removeContentFromFutureinfoClasses' => true,
  981. 'removeContentFromIndustries' => true,
  982. 'removeContentFromInnovationTypes' => true,
  983. 'removeContentFromRelatedCompanies' => true,
  984. 'removeContentRelatedCompanies' => true,
  985. 'removeContentFromTags' => true,
  986. 'removeContentTags' => true,
  987. 'removeContentFromUser' => true,
  988. 'removeContentViews' => true,
  989. 'removeContentFlags' => true,
  990. 'removeContentCommentFlags' => true,
  991. 'removeContentRatings' => true,
  992. 'removeContentFiles' => true,
  993. 'removeUserFromFavorites' => true,
  994. 'removeContent' => true,
  995. 'removeContentComments' => true
  996. );
  997. // cnt_has_cmp
  998. $cmpHasCnt = new Default_Model_CampaignHasContent();
  999. if (!$cmpHasCnt->removeContentCampaignLinks($id_cnt))
  1000. $contentRemoveChecker['removeContentFromCampaign'] = false;
  1001. // cnt_has_cnt
  1002. $cntHasCnt = new Default_Model_ContentHasContent();
  1003. if (!$cntHasCnt->removeContentFromContents($id_cnt))
  1004. $contentRemoveChecker['removeContentFromContent'] = false;
  1005. // cnt_has_fic
  1006. /* not used
  1007. $cntHasFic = new Default_Model_ContentHasFutureinfoClasses();
  1008. if (!$cntHasFic->removeFutureinfoClassesFromContent($id_cnt))
  1009. $contentRemoveChecker['removeContentFromFutureinfoClasses'] = false;
  1010. */
  1011. // cnt_has_grp
  1012. // Not used?
  1013. // cnt_has_ind
  1014. /* not used
  1015. $cntHasInd = new Default_Model_ContentHasIndustries();
  1016. if (!$cntHasInd->removeIndustriesFromContent($id_cnt))
  1017. $contentRemoveChecker['removeContentFromIndustries'] = false;
  1018. */
  1019. // cnt_has_ivt
  1020. /* not used
  1021. $cntHasIvt = new Default_Model_ContentHasInnovationTypes();
  1022. if (!$cntHasIvt->removeInnovationTypesFromContent($id_cnt))
  1023. $contentRemoveChecker['removeContentFromInnovationTypes'] = false;
  1024. */
  1025. // related_companies_rec and cnt_has_rec
  1026. $cntHasRec = new Default_Model_ContentHasRelatedCompany();
  1027. $recs = $cntHasRec->getContentRelComps($id_cnt);
  1028. $rec = new Default_Model_RelatedCompanies();
  1029. foreach($recs as $id_rec) {
  1030. if (!$cntHasRec->checkIfOtherContentHasRelComp($id_rec['id_rec'], $id_cnt)) {
  1031. if (!$rec->removeRelComp($id_rec['id_rec']))
  1032. $contentRemoveChecker['removeRelatedCompanies'] = false;
  1033. }
  1034. }
  1035. if (!$cntHasRec->removeContentRelComps($id_cnt))
  1036. $contentRemoveChecker['removeContentFromRelatedCompanies'] = false;
  1037. // tags_tag and cnt_has_tag
  1038. $cntHasTag = new Default_Model_ContentHasTag();
  1039. $tags = $cntHasTag->getContentTags($id_cnt);
  1040. $tag = new Default_Model_Tags();
  1041. foreach($tags as $id_tag) {
  1042. if(!$cntHasTag->checkIfOtherContentHasTag($id_tag['id_tag'], $id_cnt)) {
  1043. if (!$tag->removeTag($id_tag['id_tag']))
  1044. $contentRemoveChecker['removeTags'] = false;
  1045. }
  1046. }
  1047. if (!$cntHasTag->removeContentTags($id_cnt))
  1048. $contentRemoveChecker['removeContentFromTags'] = false;
  1049. // cnt_has_usr
  1050. $cntHasUsr = new Default_Model_ContentHasUser();
  1051. if (!$cntHasUsr->removeUserFromContent($id_cnt))
  1052. $contentRemoveChecker['removeContentFromUser'] = false;
  1053. // cnt_publish_times_pbt
  1054. // Not used?
  1055. // cnt_views_vws
  1056. $cntWiewsVws = new Default_Model_ContentViews();
  1057. if (!$cntWiewsVws->removeContentViews($id_cnt))
  1058. $contentRemoveChecker['removeContentViews'] = false;
  1059. // Flags from content_flags_cfl
  1060. $contentflagmodel = new Default_Model_ContentFlags();
  1061. $cnfl_ids = $contentflagmodel->getFlagsByContentId($id_cnt);
  1062. if (is_array($cnfl_ids)) {
  1063. foreach($cnfl_ids as $cfl_id) {
  1064. if (!$contentflagmodel->removeFlag($cfl_id))
  1065. $contentRemoveChecker['removeContentFlags'] = false;
  1066. }
  1067. }
  1068. // Flags from comment_flags_cfl
  1069. $commentflagmodel = new Default_Model_CommentFlags();
  1070. $cmfl_ids = $commentflagmodel->getFlagsByContentId($id_cnt);
  1071. if (is_array($cmfl_ids)) {
  1072. foreach($cmfl_ids as $cfl_id) {
  1073. if (!$commentflagmodel->removeFlag($cfl_id))
  1074. $contentRemoveChecker['removeContentCommentFlags'] = false;
  1075. }
  1076. }
  1077. // content_ratings_crt
  1078. $contentRatingRct = new Default_Model_ContentRatings();
  1079. if (!$contentRatingRct->removeContentRatings($id_cnt))
  1080. $contentRemoveChecker['removeContentRatings'] = false;
  1081. // files_fil
  1082. $files = new Default_Model_Files();
  1083. if (!$files->removeContentFiles($id_cnt))
  1084. $contentRemoveChecker['removeContentFiles'] = false;
  1085. // links_lnk
  1086. // Not used?
  1087. // usr_has_fvr
  1088. $usrHasFvr = new Default_Model_UserHasFavourites();
  1089. if (!$usrHasFvr->removeAllContentFromFavouritesByContentId($id_cnt))
  1090. $contentRemoveChecker['removeUserFromFavorites'] = false;
  1091. // contents_cnt
  1092. $contentmodel = new Default_Model_Content();
  1093. if (!$contentmodel->removeContent($id_cnt))
  1094. $contentRemoveChecker['removeContent'] = false;
  1095. // coments_cmt
  1096. $commentmodel = new Default_Model_Comments();
  1097. if (!$commentmodel->removeAllContentComments($id_cnt))
  1098. $contentRemoveChecker['removeContentComments'] = false;
  1099. return $contentRemoveChecker;
  1100. }
  1101. /**
  1102. * checkIfContentExists
  1103. *
  1104. */
  1105. public function checkIfContentExists($id_cnt = 0)
  1106. {
  1107. $return = false;
  1108. if((int)$id_cnt != 0) {
  1109. $select = $this->select()
  1110. ->from($this, array('*'))
  1111. ->where('id_cnt = ?', (int)$id_cnt);
  1112. $result = $this->fetchAll($select)->toArray();
  1113. if(count($result) != 0) {
  1114. $return = true;
  1115. }
  1116. }
  1117. return $return;
  1118. }
  1119. /**
  1120. * getContentTypeIdByContentId
  1121. *
  1122. *
  1123. */
  1124. public function getContentTypeIdByContentId($id_cnt = 0) {
  1125. $select = $this->select()
  1126. ->from($this, array('id_cty_cnt'))
  1127. ->where('id_cnt = ?', (int)$id_cnt);
  1128. $result = $this->fetchAll($select)->toArray();
  1129. return $result[0]['id_cty_cnt'];
  1130. }
  1131. /**
  1132. * getContentHeaderByContentId
  1133. *
  1134. *
  1135. */
  1136. public function getContentHeaderByContentId($id_cnt = 0) {
  1137. if((int)$id_cnt != 0) {
  1138. $select = $this->select()
  1139. ->from('contents_cnt', array('title_cnt'))
  1140. ->where('id_cnt = ?', (int)$id_cnt);
  1141. $result = $this->fetchAll($select)->toArray();
  1142. return $result[0]['title_cnt'];
  1143. } else {
  1144. return NULL;
  1145. }
  1146. }
  1147. /**
  1148. * getDataForView
  1149. *
  1150. * Get content by id.
  1151. * Is this function used anywhere?
  1152. * If not, this function should probably be removed.
  1153. *
  1154. * @param ineteger $id
  1155. * @return array
  1156. */
  1157. public function getDataForView($id = 0)
  1158. {
  1159. // Array for content data
  1160. $data = array();
  1161. // Find content row by id
  1162. //$rowset = $this->find((int)$id)->current();
  1163. $select = $this->_db->select()
  1164. ->from(array('contents_cnt' => 'contents_cnt'), array('*'))
  1165. ->where('id_cnt = ?', $id)
  1166. ;
  1167. $result = $this->_db->fetchAll($select);
  1168. // If content was found
  1169. if(count($result) == 1) {
  1170. $data['Content']['Data'] = $result[0];
  1171. // Find Ratings
  1172. //$select_ratings = $this->select()->from('content_ratings_crt', array('SUM(rating_crt) AS rate_crt'));
  1173. //$ratings = $rowset->findDependentRowset('Default_Model_ContentRatings', 'RatingsContent', $select_ratings)->toArray();
  1174. $ratings = new Default_Model_ContentRatings();
  1175. $rating = $ratings->getById($id);
  1176. // Find content owners
  1177. //$content_owner = $rowset->findManyToManyRowset('Default_Model_User', 'Default_Model_ContentHasUser');
  1178. $cntHasUser = new Default_Model_ContentHasUser();
  1179. $owners = $cntHasUser->getContentOwners($id);
  1180. // Find owners
  1181. $userModel = new Default_Model_User();
  1182. $i = 0;
  1183. foreach ($owners as $owner) {
  1184. $data['Content']['Data']['Owners'][$i] = $userModel->getSimpleUserDataById($owner);
  1185. $i++;
  1186. }
  1187. // Find content comments
  1188. //$select_comment = $this->select()->order('created_cmt ASC');
  1189. //$comments = $rowset->findDependentRowset('Default_Model_Comments', 'CommentContent', $select_comment);
  1190. $commentModel = new Default_Model_Comments();
  1191. $comments = $commentModel->getAllByContentId($id);
  1192. /* comment owner username is fetched in the previous query, no need for this anymore
  1193. // Array for comment owners
  1194. $comment_owners = array();
  1195. // Go through all comments
  1196. foreach($comments as $cmt)
  1197. {
  1198. // Find comment owner
  1199. $usr = $cmt->findDependentRowset('Default_Model_User', 'CommentUser')->toArray();
  1200. // If owner found
  1201. if(!empty($usr))
  1202. {
  1203. // Specify comment owner
  1204. $comment_owners[$usr[0]['id_usr']] = $usr[0];
  1205. } // end if
  1206. } // end foreach
  1207. */
  1208. // Find content keywords
  1209. //$tags = $rowset->findManyToManyRowset('Default_Model_Tags', 'Default_Model_ContentHasTag')->toArray();
  1210. $cntHasTag = new Default_Model_ContentHasTag();
  1211. $tags = $cntHasTag->getContentTags($id);
  1212. // Find content links - needs updating to this version
  1213. $links = array(); //$rowset->findDependentRowset('Default_Model_Links')->toArray();
  1214. // Find related content
  1215. //$$related_content = $rowset->findManyToManyRowset('Default_Model_Content', 'Default_Model_ContentHasContent', 'ParentContent', 'ChildContent')->toArray();
  1216. $contentHasContent = new Default_Model_ContentHasContent();
  1217. $familyTree = $contentHasContent->getContentFamilyTree($id);
  1218. // echo"<pre>"; print_r($tagArray); echo"</pre>"; die;
  1219. // Gather and format content data a bit
  1220. $data['Content']['Data']['rating'] = $rating;
  1221. //$data['Content']['Data']['owner'] = $owner;
  1222. $data['Content']['Tags'] = $tags;
  1223. $data['Content']['Links'] = $links;
  1224. $data['Content']['FamilyTree'] = $familyTree;
  1225. $data['Comments']['Data'] = $comments;
  1226. //echo"<pre>"; print_r($comments); echo"</pre>"; die;
  1227. //$data['Comments']['Posters'] = $comment_owners;
  1228. }
  1229. return $data;
  1230. } // end of getById
  1231. /**
  1232. * getDataAsSimpleArray
  1233. *
  1234. *
  1235. */
  1236. public function getDataAsSimpleArray($id = -1)
  1237. {
  1238. if((int)$id != -1) {
  1239. $select = $this->_db->select()
  1240. ->from(array('cnt' => 'contents_cnt'),
  1241. array('*'))
  1242. ->joinLeft(array('chu' => 'cnt_has_usr'),
  1243. 'chu.id_cnt = cnt.id_cnt',
  1244. array())
  1245. ->joinLeft(array('usr' => 'users_usr'),
  1246. 'usr.id_usr = chu.id_usr',
  1247. array('usr.id_usr', 'usr.login_name_usr'))
  1248. ->joinLeft(array('cty' => 'content_types_cty'),
  1249. 'cnt.id_cty_cnt = cty.id_cty',
  1250. array('cty.key_cty', 'cty.name_cty'))
  1251. ->where('cnt.id_cnt = ?', (int)$id)
  1252. // ->where('published_cnt = 1')
  1253. ;
  1254. $result = $this->_db->fetchAll($select);
  1255. if (isset($result[0]) && !empty($result[0])) {
  1256. return $result[0];
  1257. }
  1258. }
  1259. return false;
  1260. }
  1261. /*
  1262. public function getContentByIndustry($ind_id)
  1263. {
  1264. $select = $this->_db->select()->from(array('cnt' => $this), array('*'))
  1265. ->join(array('chi' => 'cnt_has_ind'),
  1266. 'cnt.id_cnt = chi.id_cnt',
  1267. array('*'))
  1268. ->join(array('ind' => 'industries_ind'),
  1269. 'chi.id_ind = ind.id_ind',
  1270. array('*'));
  1271. $result = $this->_db->fetchAll($select);
  1272. return $result;
  1273. }
  1274. */
  1275. /**
  1276. * getMostViewedType
  1277. *
  1278. *
  1279. */
  1280. public function getMostViewedType ($cty = 'all', $page = 1, $count = -1, $order = 'views', $lang = 'en', $ind = 0)
  1281. {
  1282. switch ($order) {
  1283. case 'author':
  1284. $order = 'usr.login_name_usr';
  1285. break;
  1286. case 'header':
  1287. $order = 'cnt.title_cnt';
  1288. break;
  1289. case 'views':
  1290. $order = 'viewCount DESC';
  1291. break;
  1292. default:
  1293. $order = 'cnt.created_cnt DESC';
  1294. }
  1295. // Needs more optimization
  1296. $sel