Subversion Repositories phpLibraryV2

Rev

Rev 34 | Rev 39 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34 Rev 37
Line 150... Line 150...
150
         } // if
150
         } // if
151
         $useAssociativeArray = $save;
151
         $useAssociativeArray = $save;
152
         return $returnValue;
152
         return $returnValue;
153
      }
153
      }
154
      
154
      
155
   
155
      
-
 
156
      /**
-
 
157
       * Builds an HTML select out of a query, or data
-
 
158
       * 
-
 
159
       * $parameters may contain the following keys
-
 
160
       * 
-
 
161
       * Must have one of the following. If both are present, 'query' is
-
 
162
       * ignored
156
      /* the first returned column becomes the value, the second becomes the display element
163
       * 'query' - An SQL query to run. Should return 2 columns; first is value, second id display
-
 
164
       * 'data' - An array of key/value pair. Key is used for value, value is display
-
 
165
       * 
-
 
166
       * 'name' - HTML name for the select. If this is not set, only the options will be returned
-
 
167
       * 'class' - used for class='whatever' in select
157
         if $selectedFieldsQuery is begins with 'select', items matching that query will be selected
168
       * 'selected' - key in 'data' or result of 'query' which will be selected
-
 
169
       * 'label' - used as <label>. If this is not set, no <label> is generated
158
         if  $selectedFieldsQuery is any other value, it is assumed to be the value of the first column
170
       * 'nullok' - adds a entry at top with value of -1 and display of three dashes (---)
-
 
171
       * 
-
 
172
       * 
-
 
173
       * @parameters string $parameters an array of data to decide what/how to process
-
 
174
       * @returns string an HTML select entity
-
 
175
       */
-
 
176
      function htmlSelect ( $parameters ) {
159
         
177
         
-
 
178
         //print "<pre>" . print_r( $parameters, true ) . "</pre>"; die;
160
      */
179
         
161
      function htmlSelect ( $selectedFieldsQuery = '' ) {
180
         if ( empty( $parameters['data'] ) ) {
162
         $html = '';
181
            if ( empty( $paramters['query'] ) )
163
         $selectedFields = array();
182
               return '';
164
         if (strlen( $selectedFieldsQuery )) {
183
            $save = $this->useAssociativeArray;
165
            if (  preg_match ( '/^\s*select/i', $selectedFieldsQuery ) ) { // They passed in a query
184
            $useAssociativeArray = false; // temporarily turn off associative arrays
166
               if (strlen($selectedFieldsQuery) ) {
185
            $this->query( $parameters['query'] );
167
                  $subSet = new DBQuery($selectedFieldsQuery);
186
            if ( $this->rowsAffected ) { // we returned at least one row
168
                  $subSet->run();
187
               foreach ($this->returnData as $rowNumber => $value ) {
169
                  $selectedFields = $subSet->sqlValuesToKeys();
188
                  $parameters['data'][$value[0]] = $value[1];
170
               }
189
               }
171
            } else { // assume the passed in a value
-
 
172
               $selectedFields[$selectedFieldsQuery] = 1;
-
 
173
            }
190
            }
174
         }
191
         }
-
 
192
         $selectedKey = isset( $parameters['selected'] ) ? $parameters['selected'] : '';
175
         $save = $this->useAssociativeArray;
193
         $options = array();
-
 
194
         if ( isset( $parameters['nullok'] ) ) {
176
         $useAssociativeArray = false; // temporarily turn off associative arrays
195
            $options[] = '<option value="-1">---</option>';
177
         $this->run();
196
         }
178
         if ( $this->rowsAffected ) { // we returned at least one row
197
         foreach ( $parameters['data'] as $key => $value ) {
179
            foreach ($this->returnData as $rowNumber => $value ) {
198
            $options[] = sprintf( 
180
               $html .= '<option value="' . $value[0] . '"';
199
               '<option value="%s"%s>%s</option>',
-
 
200
               $key,
181
               if ( $selectedFields[$value[0]] ) {
201
               $key == $selectedKey ? ' selected' : '',
182
                  $html .= ' selected';
202
               $value
183
               }
203
               );
-
 
204
         } // foreach
184
               $html .= '>' . $value[1] . '</option>';
205
         $return = implode( "\n", $options );
-
 
206
         if ( isset( $parameters['name'] ) ) {
-
 
207
            $return = "<select name='$parameters[name]'>\n$return\n</select>";
-
 
208
            if ( isset( $parameters['label'] ) ) {
-
 
209
               $return = "<label>$parameters[label]\n$return\n</label>\n";
185
            }
210
            }
186
         }
211
         }
187
         $useAssociativeArray = $save;
-
 
188
         return $html;
212
         return $return;
189
      }
213
      }
190
   
214
   
-
 
215
      /**
191
      /* function will take a query and turn it into a series of check boxes. It must contain
216
       * Create an HTML checkbox block from a query, or array of data passed in
-
 
217
       * 
192
         two columns, with the first becoming the the name of the checkbox
218
       * if $parameters['data'] is set, no further query is used. $parameters['data']
193
         and the second becoming the displayed value. an optional third column will be used
219
       * should contain an array of data. Each row in that array should be another 
194
         to match if the $checkedValuesQuery is used.
220
       * array with keys 
195
         if $checkedValuesQuery is not empty, it will be run to see what boxes need to be
221
       *    'id' -- 'id' and 'name' of the checkbox
196
         checked by being compared to the third column of the $sql query.
222
       *    'name' -- display name of the checkbox
197
         $htmlBefore will be placed before each check box, and $htmlAfter will be placed after
223
       *    'checked' -- boolean (or 0/1) saying whether this has a check
198
         each checkbox.
-
 
199
   
224
       * 
200
         if $tableColumns is set to a number, the checkboxes will be embedded in a group of
-
 
201
         <tr></tr>, each containing table columns of $tableColumns width. In this case, $htmlBefore
-
 
202
         will have <td> prepended and $htmlAfter will have </td> appended, meaning any passed
225
       * of $parameters['data'] is not set and $parameters['query'] is set
203
         values will be INSIDE of the td. NOTE: the <table></table> tags are NOT put in.
226
       * the query will be run. It MUST contain the column names listed above.
204
   
227
       * 
205
         NOTE: currently, using the table stuff will leave a dangling row with 0 elements if
-
 
206
         the number of elements equal the number of columns.
228
       * Processing will then continue
207
      */
229
      */
208
   
230
   
209
      function htmlCheckBoxes ( $checkedValuesQuery = '', $htmlBefore = '', $htmlAfter = '', $table_columns='' ) {
-
 
210
         $html = '';
-
 
211
         if ($table_columns) {
231
      function htmlCheckBoxes ( $parameters ) {
212
            $htmlBefore = '<td>' . $htmlBefore;
232
         if ( empty( $parameters['template'] ) ) { // they did not send us a template
213
            $htmlAfter .= '</td>';
233
            $parameters['template'] = "<input type='checkbox' id='~~id~~' name='~~name~~' ~~checked~~>\n<label for='~~id~~'>~~name~~</label>\n";
214
            $html .= '<tr>';
-
 
215
         }
234
         }
216
         $numColumns = 0;
-
 
217
         $checkBoxes = queryDatabaseExtended( $sql,0 );
235
         //print "<pre>" . print_r($parameters, true) . "</pre>"; die;
218
         $selectedFields = array();
-
 
219
         if (strlen($checkedValuesQuery) ) {
236
         if ( empty( $parameters['data'] ) ) {
220
            $subSet = new DBQuery($selectedFieldsQuery);
237
            if ( empty( $parameters['query'] ) )
221
            $subSet->run();
238
               return '';
222
            $selectedFields = $subSet->sqlValuesToKeys();
-
 
223
         }
-
 
224
         $save = $this->useAssociativeArray;
239
            $save = $this->useAssociativeArray;
225
         $useAssociativeArray = false; // temporarily turn off associative arrays
240
            $useAssociativeArray = false; // temporarily turn off associative arrays
226
         foreach ($this->returnData as $row => $values) {
241
            $this->doSQL( $parameters['query'] );
227
            if ($table_columns && ++$numColumns == $table_columns) {
242
            if ( $this->rowsAffected ) { // we returned at least one row
228
               $html .= '</tr><tr>';
243
               foreach ($this->returnData as $rowNumber => $value ) {
229
               $numColumns = 0;
244
                  $parameters['data'][] = $value;
230
            }
245
               }
231
            //objectDebugScreen($row);
-
 
232
            $html .= $htmlBefore . '<input type="checkbox" name="' . $values[0] . '"';
-
 
233
            if ( $selectedFields[$values[2]] ) {
-
 
234
               $html .= ' checked';
-
 
235
            }
246
            }
-
 
247
         }
-
 
248
         //print "<pre>" . print_r($parameters, true) . "</pre>"; die;
-
 
249
         foreach ( $parameters['data'] as $key => $value ) {
-
 
250
            $replacement = array(
-
 
251
               'id' => $value['id'], 
236
            $html .= '>' . $values[1] . $htmlAfter;
252
               'name' => $value['name'], 
237
            //<INPUT type="checkbox" checked name="temp">
253
               'checked' => $value['checked'] ? 'checked' : ''
238
         } // foreach
254
               );
-
 
255
            $html[] = $this->templateReplace($parameters['template'],$replacement);
-
 
256
         }
239
         $html .= '</tr>';
257
         return implode( '', $html );
-
 
258
      }
-
 
259
      
-
 
260
      /**
-
 
261
       * Replaces instances of replacement strings in string
-
 
262
       * 
-
 
263
       * Designed to allow the caller to build a string with things to be
240
         $useAssociativeArray = $save;
264
       * replaced, for example
-
 
265
       * <input type='text' name='~~name~~'>
-
 
266
       * All instances of ~~name~~ will be replaced 
-
 
267
       */
-
 
268
      public function templateReplace ( $template, $replacmentStrings, $delimiter = '~~' ) {
-
 
269
         //print "<pre>" . print_r($replacmentStrings, true) . "</pre>";
-
 
270
         //print "<pre>" . print_r($template, true) . "</pre>";
-
 
271
         foreach ( $replacmentStrings as $key => $replace ) {
-
 
272
            $search = '/' . $delimiter . $key . $delimiter . '/';
-
 
273
            //print "<pre>" . print_r($search, true) . "</pre>"; die;
-
 
274
            $template = preg_replace( $search, $replace, $template );
-
 
275
         }
241
         return $html;
276
         return $template;
242
      }
277
      }
243
      
278
      
244
   /*   
279
   /*   
245
      function CSV ( $sql ) {
280
      function CSV ( $sql ) {
246
         $rows = array();
281
         $rows = array();