<commit_before><?php

abstract class Srdata_RestController extends Zend_Rest_Controller
{
		protected $tableName; 
		protected $db;
		protected $restTable;
		protected $idName;
		protected $theRequest;

    public function init()
    {
			date_default_timezone_set("America/New_York");
			$this->logger = new Zend_Log();
			$this->logger->addWriter(new Zend_Log_Writer_Stream("/tmp/sr_rest.log"));

//			$this->db = $this->getInvokeArg('bootstrap')->getResource('db');
//			$this->restTable = new Srdata_Model_DbTable_Users($this->db);

			$this->theRequest = $this->getRequest();
			$this->_helper->viewRenderer->setNoRender(true);
			$this->logger->log("REST Class: ".$this->tableName." Inited. ", Zend_Log::DEBUG);	

    }

		// GET (Index) Action === READ ALL fetchAll
    public function indexAction()
    {

			$countRows = "SELECT COUNT(*) AS count FROM ".$this->tableName;
			$tableSize = $this->db->fetchOne($countRows);
		
			$select = $this->restTable->select();

			// TO SUPPORT SORTING Look for sort param
			$tableSort = $this->_getAllParams();	
/*			$sortArr = explode(',',$tableSort);
			if( is_array( $sortArr ) ) {
				foreach($sortArr as $key=> $val) {
					if( preg_match( "/^\_(.*)/", $val, $valArr) ) {
						$sortArr[$key] = $valArr[1]." ASC";
					}elseif( preg_match( "/^-(.*)/", $val, $valArr) ) {
						$sortArr[$key] = $valArr[1]." DESC";
					}
				}
			}
*/
			$this->logger->log("TEST: ".print_r($tableSort,true), Zend_Log::DEBUG);	
			

			// TO SUPPORT PAGINATION WE NEED TO LOOK FOR RANGE = VAR and TRANSLATE IT TO
			// OFFSET AND LIMIT parameters in query.
			$range = $this->_request->getHeader('Range');	
			$rangeArr = array();
			if( preg_match( "/items=(\d+)-(\d+)/", $range,$rangeArr) ) {
				$itemsRange = $rangeArr[0];
				$offset = $rangeArr[1];
				$limit  = $rangeArr[2];
				$select->limit($limit,$offset);
				$this->_response->setHeader('Content-Range', $itemsRange.'/'.$tableSize);
			}
			$this->logger->log($this->tableName." Index (READ ALL)  Action Called", Zend_Log::DEBUG);	
			$rows = $this->restTable->fetchAll($select);
			print Zend_Json::encode($rows->toArray());

    }

		// GET Action === READ SPECIFIC ROW
		public function getAction() 
		{
			$this->logger->log($this->tableName." Get (READ) Action Called: ", Zend_Log::DEBUG);	
			$id = $this->_getParam('id');<commit_msg>Sorting in admin works now<commit_after><?php

abstract class Srdata_RestController extends Zend_Rest_Controller
{
		protected $tableName; 
		protected $db;
		protected $restTable;
		protected $idName;
		protected $theRequest;

    public function init()
    {
			date_default_timezone_set("America/New_York");
			$this->logger = new Zend_Log();
			$this->logger->addWriter(new Zend_Log_Writer_Stream("/tmp/sr_rest.log"));

//			$this->db = $this->getInvokeArg('bootstrap')->getResource('db');
//			$this->restTable = new Srdata_Model_DbTable_Users($this->db);

			$this->theRequest = $this->getRequest();
			$this->_helper->viewRenderer->setNoRender(true);
			$this->logger->log("REST Class: ".$this->tableName." Inited. ", Zend_Log::DEBUG);	

    }

		// GET (Index) Action === READ ALL fetchAll
    public function indexAction()
    {

			$countRows = "SELECT COUNT(*) AS count FROM ".$this->tableName;
			$tableSize = $this->db->fetchOne($countRows);
		
			$select = $this->restTable->select();

			// TO SUPPORT SORTING Look for sort param
			$tableSort = "";
			$paramArr = $this->_getAllParams();	
			foreach($paramArr as $key=> $val) {
				if( preg_match( "/^sort\((.*)\)/", $key, $keyArr) ) {
					$tableSort = $keyArr[1];
					continue;
				}
			}
			$sortArr = explode(',',$tableSort);
//			$this->logger->log("TEST2: $tableSort\n".print_r($sortArr,true), Zend_Log::DEBUG);	
			if( is_array( $sortArr ) ) {
				foreach($sortArr as $key=> $val) {
					if( preg_match( "/^\_(.*)/", $val, $valArr) ) {
						$sortArr[$key] = $valArr[1]." ASC";
					}elseif( preg_match( "/^-(.*)/", $val, $valArr) ) {
						$sortArr[$key] = $valArr[1]." DESC";
					}
				}
			}
			$select->order($sortArr);


//			$this->logger->log("TEST: ".print_r($sortArr,true), Zend_Log::DEBUG);	
			

			// TO SUPPORT PAGINATION WE NEED TO LOOK FOR RANGE = VAR and TRANSLATE IT TO
			// OFFSET AND LIMIT parameters in query.
			$range = $this->_request->getHeader('Range');	
			$rangeArr = array();
			if( preg_match( "/items=(\d+)-(\d+)/", $range,$rangeArr) ) {
				$itemsRange = $rangeArr[0];
				$offset = $rangeArr[1];
				$limit  = $rangeArr[2];
				$select->limit($limit,$offset);
				$this->_response->setHeader('Content-Range', $itemsRange.'/'.$tableSize);
			}
			$this->logger->log($this->tableName." Index (READ ALL)  Action Called", Zend_Log::DEBUG);	
			$rows = $this->restTable->fetchAll($select);
			print Zend_Json::encode($rows->toArray());

    }

		// GET Action === READ SPECIFIC ROW
		public function getAction() 
		{
			$this->logger->log($this->tableName." Get (READ) Action Called: ", Zend_Log::DEBUG);	
			$id = $this->_getParam('id');
{'fraction_non_alphanumeric': 0.19911682055399438, 'fraction_numerical': 0.0024086712163789645, 'mean_word_length': 3.6096207215541165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6867193', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1713, 'n_words': 334}