~fabiocbalbuquerque/sahana-agasti/web-services

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

/**
 * extends sfActions for common functionality
 *
 * PHP Version 5.3
 *
 * LICENSE: This source file is subject to LGPLv2.1 license
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * @author Charles Wisniewski, CUNY SPS
 *
 * Copyright of the Sahana Software Foundation, sahanafoundation.org
 */
class agActions extends sfActions
{

  protected $_searchedModels;

  public function __construct($context, $moduleName, $actionName)
  {
    parent::__construct($context, $moduleName, $actionName);
    if (empty($this->_searchedModels)) {
      $this->_searchedModels = array('agStaff', 'agFacility', 'agOrganization');
    }
  }

  public function executeSearch(sfWebRequest $request)
  {
    $this->targetAction = 'search';
    $string = $request->getParameter('query');
    $pattern = "/\W/";
    $replace = " ";
    $this->params = '?query=' . urlencode(trim(preg_replace($pattern, $replace, $string), '+'));
//    $this->params = '?query=' . $request->getParameter('query');
    $currentPage = ($request->hasParameter('page')) ? $request->getParameter('page') : 1;
    self::doSearch($request->getParameter('query'), $currentPage);
    $this->setTemplate(sfConfig::get('sf_app_dir') . DIRECTORY_SEPARATOR . 'modules/search/templates/search');
    //$this->setTemplate('global/search');
  }

  public function executeStatus(sfWebRequest $request)
  {
    ////TODO: module_ACTION_status instead? -UA
    //$statusId = implode('_', array($this->moduleName, 'status'));
    //$context = $this->getContext();
    ////$context = sfContext::getInstance();
    //$status = $context->has($statusId) ? $this->getContext()->get($statusId) : $statusId/*array(0, 0, 0)*/;

    //TODO: get import data directory root info from global param
    $importDataRoot = sfConfig::get('sf_upload_dir');
    $importDir = $importDataRoot . DIRECTORY_SEPARATOR . $this->moduleName;
    $statusFile = $importDir . DIRECTORY_SEPARATOR . 'status.yml';
    $status = is_readable($statusFile) ? sfYaml::load($statusFile) : array()/* array(0, 0, 0) */;
    $format = $request->getRequestFormat();
    if ('json' == $format) {
      $this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
      $status = json_encode($status);
    }
    //TODO: else, use partial instead of returning?
    //TODO: the else block below is for testing -- remove when finished
    else {
      $this->getResponse()->setHttpHeader('Content-Type', 'text/plain; charset=utf-8');
      $status = json_encode($status);
    }

    return $this->renderText($status);
  }

  public function doSearch($searchquery, $currentPage, $isFuzzy = TRUE, $widget = NULL)
  {
    $models = $this->getSearchedModels();
    $this->targetModule = 'staff';
    $this->searchquery = $searchquery;
    $this->getResponse()->setTitle('Search results for: ' . $this->searchquery);

    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());
    $query = LuceneSearch::find($this->searchquery);
    if ($isFuzzy == TRUE) {
      $query->fuzzy();
    }
    $query->in($models);
    $this->results = $query->getRecords();
    $this->hits = $query->getHits();
    $this->widget = $widget;

    $searchResult = $query->getRecords(); //agStaff should be $models
    // TODO
    //a) we can return the results hydrated as scalar
    // (only get the PK's[person,entity,staff,facility,etc])

    $resultsPerPage = agGlobal::getParam('search_list_results_per_page');

    if ($models[0] == 'agStaff')
    {
      $staff_ids = array();
      $this->targetModule = 'staff';
      if ( (isset($searchResult['agStaff']) || ($models[0] == 'agStaff')) && (count($searchResult) != 0) )
      {
        $staffCollection = $searchResult['agStaff'];
        $staff_ids = $staffCollection->getKeys();
      }
      list($this->displayColumns, $pagerQuery) = agListHelper::getStaffList($staff_ids);
    } elseif ($models[0] == 'agFacility') {
      $facility_ids = array();
      $this->targetModule = 'facility';
      if ( (isset($searchResult['agFacility']) || ($models[0] == 'agFacility')) && (count($searchResult) != 0) )
      {
        $facilityCollection = $searchResult['agFacility'];
        $facility_ids = $facilityCollection->getKeys();
      }
      list($this->displayColumns, $pagerQuery) = agListHelper::getFacilityList($facility_ids);
    } elseif($models[0] == 'agOrganization') {
      $organization_ids = array();
      $this->targetModule = 'organization';
      if ( (isset($searchResult['agOrganization']) || ($models[0] == 'agOrganization')) && (count($searchResult) != 0) )
      {
        $organizationCollection = $searchResult['agOrganization'];
        $organization_ids = $organizationCollection->getKeys();
      }
      list($this->displayColumns, $pagerQuery) = agListHelper::getOrganizationList($organization_ids);
    } else {
      $this->forward404("Search Module Undefined.");
    }

    $this->pager = new Doctrine_Pager($pagerQuery, $currentPage, $resultsPerPage);
    $this->data = $this->pager->execute(array(), Doctrine_Core::HYDRATE_SCALAR);
  }

  public function getSearchedModels()
  {
    return $this->_searchedModels;
  }

}