/app/models/project.php

https://github.com/germc/CrowdFund · PHP · 293 lines · 219 code · 53 blank · 21 comment · 23 complexity · db2b8fa041c55caa712446118d6cc4d9 MD5 · raw file

  1. <?php
  2. class Project extends AppModel {
  3. var $name = 'Project';
  4. var $belongsTo = array(
  5. 'User' => array(
  6. 'className' => 'User'
  7. ),
  8. 'ProjectCategory' => array(
  9. 'className' => 'ProjectCategory'
  10. )
  11. );
  12. //var $hasMany = 'LineItem';
  13. var $actsAs = array( 'Sluggable' => array( 'label' => 'title',
  14. 'slug' => 'slug' ) );
  15. var $hasMany = array(
  16. 'LineItem' => array(
  17. 'className' => 'LineItem'
  18. ),
  19. 'Collaborator' => array(
  20. 'className' => 'Collaborator'
  21. ),
  22. 'OutsideFundingSource' => array(
  23. 'className' => 'OutsideFundingSource'
  24. ),
  25. 'Donation' => array(
  26. 'className' => 'Donation'
  27. ),
  28. 'Image'=>array(
  29. 'className'=>'Image',
  30. 'foreignKey'=>'foreign_id',
  31. 'conditions'=>array(
  32. 'Image.type'=>'project')
  33. ),
  34. 'Link'=>array(
  35. 'className'=>'Link',
  36. 'foreignKey'=>'foreign_id',
  37. 'conditions'=>array(
  38. 'Link.type'=>'project')
  39. ),
  40. 'EmbeddedVideo'=>array(
  41. 'className'=>'EmbeddedVideo',
  42. 'foreignKey'=>'foreign_id',
  43. 'conditions'=>array(
  44. 'EmbeddedVideo.type'=>'project')
  45. ),
  46. 'Document'=>array(
  47. 'className'=>'Document',
  48. 'foreignKey'=>'foreign_id',
  49. 'conditions'=>array(
  50. 'Document.type'=>'project_doc')
  51. )
  52. );
  53. var $validate = array(
  54. 'title' => array(
  55. 'alphaNumeric' => array(
  56. 'rule' => array('custom', '/^[a-z0-9 ]*$/i'),
  57. 'required' => true,
  58. 'message' => EUREKA_ERROR_ALPHA_EN
  59. ),
  60. 'between' => array(
  61. 'rule' => array('between', 2, 50),
  62. 'message' => EUREKA_ERROR_PROJECT_TITLE_BETWEEN_EN
  63. )
  64. ),
  65. 'abstract' => array(
  66. 'notEmpty' => array(
  67. 'rule' => 'notEmpty',
  68. 'required' => true,
  69. 'message' => EUREKA_ERROR_EMPTY_EN
  70. )
  71. ),
  72. 'background' => array(
  73. 'notEmpty' => array(
  74. 'rule' => 'notEmpty',
  75. 'required' => true,
  76. 'message' => EUREKA_ERROR_EMPTY_EN
  77. )
  78. ),
  79. 'homepage' => array(
  80. 'URL' => array(
  81. 'rule' => 'URL',
  82. 'required' => false,
  83. 'allowEmpty' => true,
  84. 'message' => EUREKA_ERROR_URL_VALID_EN
  85. )
  86. )
  87. );
  88. /**
  89. * Overridden paginate method
  90. */
  91. function paginateAnswer($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
  92. $projects = array();
  93. $projectIds = array();
  94. $fields=null;
  95. //grab searchstr
  96. if (!empty($conditions['searchstr'])) {
  97. $searchstr = $conditions['searchstr'];
  98. //now rebuild conditions
  99. $approved = array('Project.status' => 'APPROVED');
  100. //FIND BY TITLE -- EXACT
  101. $conditions = array_merge($approved, array('Project.title LIKE' => $searchstr));
  102. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  103. foreach($found as $project) {
  104. if (!in_array($project['Project']['id'], $projectIds)) {
  105. $projectIds[] = $project['Project']['id'];
  106. $projects[] = $project;
  107. }
  108. }
  109. //FIND BY TITLE -- BEGINS WITH
  110. $conditions = array_merge($approved, array('Project.title LIKE' => $searchstr.'%'));
  111. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  112. foreach($found as $project) {
  113. if (!in_array($project['Project']['id'], $projectIds)) {
  114. $projectIds[] = $project['Project']['id'];
  115. $projects[] = $project;
  116. }
  117. }
  118. //FIND BY TITLE -- MATCH
  119. $conditions = array_merge($approved, array('Project.title LIKE' => '%'.$searchstr.'%'));
  120. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  121. foreach($found as $project) {
  122. if (!in_array($project['Project']['id'], $projectIds)) {
  123. $projectIds[] = $project['Project']['id'];
  124. $projects[] = $project;
  125. }
  126. }
  127. //FIND BY ABSTRACT
  128. $conditions = array_merge($approved, array('Project.abstract LIKE' => '%'.$searchstr.'%'));
  129. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  130. foreach($found as $project) {
  131. if (!in_array($project['Project']['id'], $projectIds)) {
  132. $projectIds[] = $project['Project']['id'];
  133. $projects[] = $project;
  134. }
  135. }
  136. //FIND BY USER FNAME
  137. $conditions = array_merge($approved, array('User.fname LIKE' => '%'.$searchstr.'%'));
  138. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  139. foreach($found as $project) {
  140. if (!in_array($project['Project']['id'], $projectIds)) {
  141. $projectIds[] = $project['Project']['id'];
  142. $projects[] = $project;
  143. }
  144. }
  145. //FIND BY USER LNAME
  146. $conditions = array_merge($approved, array('User.lname LIKE' => '%'.$searchstr.'%'));
  147. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  148. foreach($found as $project) {
  149. if (!in_array($project['Project']['id'], $projectIds)) {
  150. $projectIds[] = $project['Project']['id'];
  151. $projects[] = $project;
  152. }
  153. }
  154. //FIND BY PROJECT BACKGROUND
  155. $conditions = array_merge($approved, array('Project.background LIKE' => '%'.$searchstr.'%'));
  156. pr($conditions);
  157. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  158. foreach($found as $project) {
  159. if (!in_array($project['Project']['id'], $projectIds)) {
  160. $projectIds[] = $project['Project']['id'];
  161. $projects[] = $project;
  162. }
  163. }
  164. } else {
  165. //USE SUPPLIED CONDITIONS
  166. $found = $this->find('all', compact('conditions', 'fields', 'order'));
  167. $projects = $found;
  168. //pr($projects);
  169. }
  170. return $projects;
  171. }
  172. function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
  173. $result = $this->paginateAnswer($conditions, $fields, $order, $limit, $page, $recursive, $extra);
  174. $offset = ($page-1)*$limit;
  175. return array_slice($result, $offset, $limit);
  176. }
  177. /**
  178. * Overridden paginateCount method
  179. */
  180. function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
  181. return count($this->paginateAnswer($conditions, $recursive, null, null));
  182. }
  183. function afterFind( $results ) {
  184. if ( is_array( $results ) && ! isset( $results[ 'id' ] ) ) {
  185. // we found more than one project
  186. foreach ( $results as &$result ) {
  187. if ( isset( $result[ 'Image' ] ) ) { $this->_sortImages( $result['Image'] ); }
  188. if ( isset( $result[ 'LineItem' ] ) ) { $this->_groupAndTotalLineItemsAndDonations( $result ); }
  189. }
  190. } else {
  191. // we found just one project
  192. if ( isset( $results[ 'Image' ] ) ) { $this->_sortImages( $results['Image'] ); }
  193. if ( isset( $results[ 'LineItem' ] ) ) { $this->_groupAndTotalLineItemsAndDonations( $results ); }
  194. }
  195. return $results;
  196. }
  197. function _sortImages( &$image_array ) {
  198. usort($image_array, array( $this , '_orderCmp') );
  199. }
  200. function _groupAndTotalLineItemsAndDonations( &$project ) {
  201. $lineItemCategories = array();
  202. $goal = 0;
  203. foreach ($project['LineItem'] as $lineItem) {
  204. if ( isset( $lineItem['LineItemCategory'] ) ) {
  205. if (!in_array($lineItem['LineItemCategory']['title'], $lineItemCategories)) {
  206. $lineItemCategories[$lineItem['LineItemCategory']['title']] = $lineItem['amount'];
  207. } else {
  208. $lineItemCategories[$lineItem['LineItemCategory']['title']] += $lineItem['amount'];
  209. }
  210. }
  211. $goal += $lineItem['amount'];
  212. }
  213. $donations_total = 0;
  214. if ( isset( $project['Donation'] ) ) {
  215. foreach ( $project['Donation'] as $donation ) {
  216. $donations_total += $donation['project_donation_amt'];
  217. }
  218. }
  219. $project['goal']['by_line_item'] = $lineItemCategories;
  220. $project['goal']['total'] = $goal;
  221. $project['goal']['raised'] = $donations_total;
  222. $ratio = ( $goal != 0 ) ? min($donations_total/$goal, 1) : 0;
  223. $thermoFill = (1-$ratio)*136;
  224. $thermoMove = 56+136-$thermoFill;
  225. $project['goal']['ratio'] = $ratio;
  226. $project['goal']['thermoFill'] = $thermoFill;
  227. $project['goal']['thermoMove'] = $thermoMove;
  228. }
  229. /* used for comparing order of images */
  230. function _orderCmp($im1, $im2)
  231. {
  232. $a = $im1['order'];
  233. $b = $im2['order'];
  234. if ($a == $b) {
  235. return 0;
  236. }
  237. return ($a < $b) ? -1 : 1;
  238. }
  239. }
  240. ?>