PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/application/models/Content.php

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