| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class tablePost{ |
|---|
| 4 | |
|---|
| 5 | var $db; |
|---|
| 6 | var $whereclause = ""; |
|---|
| 7 | var $modifiedby = NULL; |
|---|
| 8 | |
|---|
| 9 | var $maintable = ""; |
|---|
| 10 | var $datefieldname = ""; |
|---|
| 11 | var $notpostedCriteria = ""; |
|---|
| 12 | |
|---|
| 13 | function tablePost($db, $modifiedby = NULL){ |
|---|
| 14 | |
|---|
| 15 | $this->db = $db; |
|---|
| 16 | |
|---|
| 17 | if($modifiedby) |
|---|
| 18 | $this->modifiedby = $modifiedby; |
|---|
| 19 | else |
|---|
| 20 | $this->modifiedby = $_SESSION["userinfo"]["id"]; |
|---|
| 21 | |
|---|
| 22 | }//end method init |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | function getRecordsToPost($startdate = null, $enddate = null){ |
|---|
| 26 | // This function creates an array of |
|---|
| 27 | // record ids within the date range given |
|---|
| 28 | |
|---|
| 29 | $idarray = array(); |
|---|
| 30 | |
|---|
| 31 | $querystatement = " |
|---|
| 32 | SELECT |
|---|
| 33 | `".$this->maintable."`.id |
|---|
| 34 | FROM |
|---|
| 35 | `".$this->maintable."` |
|---|
| 36 | WHERE |
|---|
| 37 | `".$this->maintable."`.readytopost = 1"; |
|---|
| 38 | |
|---|
| 39 | if($startdate){ |
|---|
| 40 | |
|---|
| 41 | $querystatement .= " |
|---|
| 42 | AND `".$this->maintable."`.`".$this->datefieldname."` >= '".dateToString($startdate, "SQL")."' |
|---|
| 43 | AND `".$this->maintable."`.`".$this->datefieldname."` <= '".dateToString($enddate, "SQL")."'"; |
|---|
| 44 | |
|---|
| 45 | }//endif |
|---|
| 46 | |
|---|
| 47 | if($this->notpostedCriteria){ |
|---|
| 48 | |
|---|
| 49 | $querystatement .= " |
|---|
| 50 | AND ".$this->notpostedCriteria; |
|---|
| 51 | |
|---|
| 52 | }//endif |
|---|
| 53 | |
|---|
| 54 | $queryresult = $this->db->query($querystatement); |
|---|
| 55 | |
|---|
| 56 | while($therecord = $this->db->fetchArray($queryresult)) |
|---|
| 57 | $idarray[] = $therecord["id"]; |
|---|
| 58 | |
|---|
| 59 | //return array of ids within that date range |
|---|
| 60 | return $idarray; |
|---|
| 61 | |
|---|
| 62 | }//end method getRecordsToPost |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | function whereFromIds($idarray){ |
|---|
| 66 | // Takes an array of ids ad creates a SQL where clause |
|---|
| 67 | // segment using 'IN' |
|---|
| 68 | |
|---|
| 69 | $where = "`".$this->maintable."`.id "; |
|---|
| 70 | if(count($idarray)) |
|---|
| 71 | $where .= " IN(".implode(",", $idarray).")"; |
|---|
| 72 | else |
|---|
| 73 | $where .= " = -1000"; |
|---|
| 74 | |
|---|
| 75 | return $where; |
|---|
| 76 | |
|---|
| 77 | }//end method whereFromIds |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | function post($whereclause, $postsessionid = NULL){ |
|---|
| 81 | // Posts the records. In almost all cases, |
|---|
| 82 | // this function will be overriden |
|---|
| 83 | |
|---|
| 84 | if(!$postsessionid) |
|---|
| 85 | $postsessionid = $this->generatePostingSession("search"); |
|---|
| 86 | |
|---|
| 87 | $updatestatement = " |
|---|
| 88 | UPDATE |
|---|
| 89 | `".$this->maintable."` |
|---|
| 90 | SET |
|---|
| 91 | `".$this->maintable."`.posted = 1, |
|---|
| 92 | modifiedby = ".$this->modifiedby.", |
|---|
| 93 | modifieddate = NOW() |
|---|
| 94 | WHERE |
|---|
| 95 | ".$whereclause; |
|---|
| 96 | |
|---|
| 97 | $this->db->query($updatestatement); |
|---|
| 98 | |
|---|
| 99 | //return the number of records that successfully posted. |
|---|
| 100 | $records = $this->db->affectedRows(); |
|---|
| 101 | |
|---|
| 102 | $this->updatePostingSession($postsessionid, $records); |
|---|
| 103 | |
|---|
| 104 | return $records; |
|---|
| 105 | |
|---|
| 106 | }//end method |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | function generatePostingSession($source){ |
|---|
| 110 | // in case a posting initiates from a search screen, we |
|---|
| 111 | // need to create the session record |
|---|
| 112 | |
|---|
| 113 | $insertstatement = " |
|---|
| 114 | INSERT INTO |
|---|
| 115 | postingsessions |
|---|
| 116 | (sessiondate, `source`, recordsposted, userid) |
|---|
| 117 | VALUES |
|---|
| 118 | ( |
|---|
| 119 | NOW(), |
|---|
| 120 | '".$source."', |
|---|
| 121 | 0, |
|---|
| 122 | ".$this->modifiedby." |
|---|
| 123 | )"; |
|---|
| 124 | |
|---|
| 125 | $this->db->query($insertstatement); |
|---|
| 126 | |
|---|
| 127 | return $this->db->insertId(); |
|---|
| 128 | |
|---|
| 129 | }//end function generatePostingSession |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | function updatePostingSession($sessionid, $numrecords){ |
|---|
| 133 | |
|---|
| 134 | $updatestatement = " |
|---|
| 135 | UPDATE |
|---|
| 136 | postingsessions |
|---|
| 137 | SET |
|---|
| 138 | recordsposted = recordsposted + ".$numrecords." |
|---|
| 139 | WHERE |
|---|
| 140 | id = ".$sessionid; |
|---|
| 141 | |
|---|
| 142 | $this->db->query($updatestatement); |
|---|
| 143 | |
|---|
| 144 | }//end function updatePostingSession |
|---|
| 145 | |
|---|
| 146 | }//end class |
|---|
| 147 | |
|---|
| 148 | ?> |
|---|