ATK /handlers/class.atkattributeedithandler.inc

Language PHP Lines 600
MD5 Hash cad90b1cbe45a032d72311025888f4b8
Repository https://github.com/ibuildingsnl/ATK.git View Raw File View Project SPDX
  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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
  /**
   * This file is part of the Ibuildings E-business Platform.
   * Detailed copyright and licensing information can be found
   * in the doc/COPYRIGHT and doc/LICENSE files which should be
   * included in the distribution.
   *
   * @package atk
   * @subpackage handlers
   *
   * @author Dennis Luitwieler <dennis@ibuildings.nl>
   *
   * @copyright (c) 2007 Ibuildings.nl BV
   * @license see doc/LICENSE
   *
   * @version $Revision: 6310 $
   * $Id$
   */
  
  atkimport("atk.handlers.atkactionhandler");

  /**
   * Some defines
   * @access private
   */
  define("ATTRIBUTEEDIT_ERROR_DEFAULT", 1);
  define("ATTRIBUTEEDIT_ERROR_UPDATE", 2);
  define("ATTRIBUTEEDIT_ERROR_NO_SELECTOR_SET", 4);
  define("ATTRIBUTEEDIT_ERROR_VALIDATE", 8);

  /**
   * Handler for the 'attributeedit' action of a node. It shows a dialog for altering the value of
   * a selectable attribute for multiple records at the same time.
   *
   * This handler only supports dialogs.
   *
   * @author Dennis Luitwieler <dennis@ibuildings.nl>
   * @package atk
   * @subpackage handlers
   *
   */
  class atkAttributeEditHandler extends atkActionHandler
  {
    var $m_processUrl = null;
    var $m_masterNode = null;
    
    /**
     * Set the maste node
     *
     * @param atkNode $node The master node
     */
    function setMasterNode($node)
    {
      $this->m_masterNode = $node;
    }
        
    /**
     * The action method.
     */
    function action_attributeedit()
    {
      if ($this->m_masterNode == null)
      {
        $this->m_masterNode = $this->m_node;
      }
      
      $this->setRenderMode('dialog');

      if ($this->m_partial == 'process')
      {
        $this->handleProcess();
      }
      elseif ($this->m_partial == 'refreshvaluefield')
      {
        $this->handleRefreshValuesField();
      }
      else
      {
        $atkselector = $this->getSelector();
        if (!isset($atkselector) || $atkselector=="")
        {
          $this->handleError(ATTRIBUTEEDIT_ERROR_NO_SELECTOR_SET, null, false);
        }
        else
        {
          $this->handleDialog();
        }
      }
    }

    /**
     * Get the atkselector from the postvars.
     *
     * The atkselector could be prefixed, depending on the page where we come
     * from. From the admin page it will be prefixed with the formname, from the
     * attributeedit dialog, it will not be prefixed anymore.
     *
     * @return Array
     */
    function getSelector()
    {
      // The selector should be in the atkselector postvar      
      return $this->m_node->m_postvars['atkselector'];
    }

    /**
     * Handle dialog partial.
     */
    function handleDialog()
    {
      $page = &$this->getPage();
      $result = $this->invoke('attributeEditPage');
      $page->addContent($result);
    }

    /**
     * Handle process partial.
     */
    function handleProcess()
    {
      $node = &$this->m_node;
      $page = &$this->getPage();

      $atkselector = $this->getSelector();      

      if (!isset($atkselector) || count($atkselector)==0)
      {
        $this->handleError(ATTRIBUTEEDIT_ERROR_NO_SELECTOR_SET);
        return;
      }
      
      $attributename  = $node->m_postvars["attributename"];
      $attributevalue = $node->m_postvars[$attributename];
      $attribute = $node->getAttribute($attributename);
      
      /** check if input is correct ... */
      if (!is_object($attribute) || empty($attributename))
      {
        $this->handleError(ATTRIBUTEEDIT_ERROR_DEFAULT);
        return;
      }

      // All updates and validates will be successfully executed, until proven otherwise
      $validate = true;
      $success = true;
      foreach ($atkselector as $selector)
      {
        list($rec) = $node->selectDb($selector, "", "", "", array($attributename));

        $rec[$attributename] = $attribute->fetchValue($node->m_postvars);

        // Get all the attributes we are NOT changing.
        $ignoreList = $this->getIgnoreList($attributename);

        // Try to validate the new record.
        if (!$node->validate($rec, 'edit', $ignoreList))
        {
          $validate = false;
          break;
        }

        // If the validation succeeded, we will get here and try to perform the update.
        if (!$node->updateDb($rec, false, "", array($attributename)))
        {
          $success = false;
          triggerError($rec, $attributename, $node->getDb()->getErrorType(), $node->getDb()->getErrorMsg());
          break;
        }
      }

      // on succes, commit the changes and refresh the page where this dialog was initiated.
      if ($validate && $success)
      {
        $node->getDb()->commit();
        $content = "<script type=\"text/javascript\">document.location.href = document.location.href;</script>";
        $page->addContent($content);
        return;
      }

      // On validation error, show a validation error message.
      if (!$validate)
      {
        $this->handleError(ATTRIBUTEEDIT_ERROR_VALIDATE, $rec, true);
        return;
      }

      // On failure, do a rollback (if the db supports it) and show an error page.
      $node->getDb()->rollback();
      $this->handleError(ATTRIBUTEEDIT_ERROR_UPDATE, $rec, true);

    }

    /**
     * Handle errors of different types.
     *
     * @param int $error The AttributeEditHandler error type.
     * @param array $record The record
     * @param bool $reload Reload the page?
     */
    function handleError($error, $record=null, $reload=false)
    {
      atkimport('atk.ui.atkdialog');

      $this->registerExternalFiles();

      $content = $this->getErrorPage($error, $record);

      $page = &$this->getPage();

      if (!$reload)
      {
        $page->addContent($content);
      }
      else
      {
        $script = atkDialog::getUpdateCall($content, false);
        $page->register_loadscript($script);
      }
    }

    /**
     * Get a page indicating an error has occurred.
     *
     * @param Int $error
     * @param array $record The record
     * @param String $customerror
     * @return String HTML Error page
     */
    function getErrorPage($error, $record=null, $customerror='')
    {
      $errortext = '';
      if ($customerror != '')
      {
        $errortext = $customerror;
      }
      elseif ($error == ATTRIBUTEEDIT_ERROR_UPDATE)
      {
        $errortext = $this->m_node->text('error_attributeedit_update');
      }
      elseif ($error == ATTRIBUTEEDIT_ERROR_NO_SELECTOR_SET)
      {
        $errortext = $this->m_node->text("error_attributeedit_noselectorset");
      }
      elseif ($error == ATTRIBUTEEDIT_ERROR_VALIDATE)
      {
        $errortext = $this->m_node->text("error_attributeedit_validationfailed");
      }
      else  // Other errors
      {
        $errortext = $this->m_node->text('error_attributeedit_default');
      }

      $errormsg = '';
      if (isset($record) && isset($record['atkerror']))
      {
        $errormsg = $record['atkerror']['attrib_name'].': '.$record['atkerror']['msg'].'&nbsp;';
      }

      $ui = &$this->m_node->getUi();
      atkimport('atk.ui.atkdialog');
      $params = array();
      $params["content"] = "<b>".$errortext."</b><br />";
      $params["content"].= $errormsg;
      $params["buttons"][] = '<input type="button" class="btn_cancel" value="'.$this->m_node->text('close').'" onClick="'.atkDialog::getCloseCall().'" />';
      $content = $ui->renderAction("attributeedit", $params);

      $params = array();
      $params["title"] = $this->m_node->actionTitle('attributeedit');
      $params["content"] = $content;
      $content = $ui->renderDialog($params);
      return $content;
    }


    /**
     * AttributeEdit page.
     * 
     * @return String The attribute edit page
     */
    function attributeEditPage()
    {
      return $this->getAttributeEditPage();
    }

    /**
     * Returns the attributeEdit page contents.
     *
     * @return string attributeEdit page contents
     */
    function getAttributeEditPage()
    {
      $url = $this->getProcessUrl();
      $controller = &atkController::getInstance();

      $this->registerExternalFiles();

      $params = array();
      $params["formstart"] = $this->getFormStart();
      $params["formend"] = '</form>';
      $params["content"] = $this->getContent();
      $params["buttons"][] = $controller->getDialogButton('save', $this->m_node->text('update_value'), $url);
      $params["buttons"][] = $controller->getDialogButton('cancel');

      return $this->renderAttributeEditPage($params);
    }

    /**
     * Register external files
     *
     */
    function registerExternalFiles()
    {
      $page = &$this->getPage();
      $ui = &$this->getUi();
      $page->register_script(atkconfig("atkroot")."atk/javascript/tools.js");
      $page->register_script(atkconfig('atkroot').'atk/javascript/class.atkattributeedithandler.js');
      $page->register_style($ui->stylePath("style.css"));      
    }

    /**
     * Render the add or copy page using the given parameters.
     *
     * @param array $params parameters
     * @return string rendered page
     */
    function renderAttributeEditPage($params)
    {
      $node = &$this->m_node;
      $ui = &$node->getUi();

      $output = $ui->renderAction("add", $params);
      $this->addRenderBoxVar("title", $node->actionTitle('addorcopy'));
      $this->addRenderBoxVar("content", $output);
      $total = $ui->renderDialog($this->m_renderBoxVars);

      return $total;
    }

    /**
     * Returns the attributeEdit page contents.
     *
     * @return string attributeEdit page contents
     */
    function getContent()
    {
      $content = '
        <div id="dialogcontent">' .
          $this->_getDropDownAttributes() .
          '<br />
           <div id="selectedvaluediv">' .
              $this->_getDropDownValues() .
           '</div>
        </div>';

      return $content;
    }

    /**
     * Get dropdown attributes
     *
     * @param string $selectedAttribute
     * @return String Dropdown with attributenames
     */
    function _getDropDownAttributes($selectedAttribute="")
    {
      $fieldprefix = $this->m_node->m_postvars['atkfieldprefix'];
      if ($fieldprefix == '') $fieldprefix = $this->m_node->getEditFieldPrefix();

      $attributes = $this->getSupportedAttributes();

      $attr = null;

      // Select the first attribute if none is selected
      if ($selectedAttribute=="")
      {
        // select the first (if available)
        $record['attributename'] = isset($attributes[0]) ? $attributes[0]->fieldName() : null;
      }
      else
      {
        $record['attributename'] = $selectedAttribute;
      }

      // Create options and values
      foreach ($attributes as $attribute)
      {
        $options[] = $this->m_node->text($attribute->label($record));
        $values[]  = $attribute->fieldName();
      }

      $list = &new atkListAttribute('attributename', $options, $values);
      $list->addFlag(AF_LIST_NO_NULL_ITEM);
      $list->m_ownerInstance = &$this->m_node;
      $list->addOnChangeHandler($this->onChange());
      return $list->edit($record,$fieldprefix);
    }

    /**
     * Return the onchange code
     *
     * @return String The onchange javascript code
     */
    function onChange()
    {
      $url = partial_url($this->m_masterNode->atkNodeType(), 'attributeedit', 'refreshvaluefield');
      $script = "
        ATK.AttributeEditHandler.refreshvalues('$url');
      ";

      return $script;
    }

    /**
     * Get the dropdown with the possible values for
     * the selected attribute.
     *
     * @param String $selectedAttribute
     * @return unknown
     */
    function _getDropDownValues($selectedAttribute="")
    {
      $fieldprefix = $this->m_node->m_postvars['atkfieldprefix'];

      $attr = null;

      // Select the first attribute if none is selected
      if ($selectedAttribute=="")
      {
        // get first attribute
        list($attr) = $this->getSupportedAttributes();
      }
      else
      {
        $attr = &$this->m_node->getAttribute($selectedAttribute);
      }

      if (is_object($attr))
      {
        $record[$attr->fieldName()] = $this->m_node->m_postvars[$attr->fieldName()];
        return $attr->edit($record, $fieldprefix, 'edit');
      }

      return "";
    }

    /**
     * Get the supported attributes.
     *
     * If no supported attributes were set, ATK determines them by itself.
     *
     * @return array Array with attribute objects
     */
    function getSupportedAttributes()
    {
      $supported = array();
      if (method_exists($this->m_node, 'getAttributeEditSupportedAttributes'))
      {
        $supported_attributes = $this->m_node->getAttributeEditSupportedAttributes();

        if (isset($supported_attributes) && is_array($supported_attributes))
        {
          foreach ($supported_attributes as $attribname)
          {
            $attr = $this->m_node->getAttribute($attribname);
            if (is_object($attr))
              $supported[] = $attr;
          }
        }

        return $supported;
      }

      // We let ATK determine the available attributes.
      $attributes = $this->m_node->getAttributes();

      // We do not need certain attributes.
      foreach ($attributes as $index=>$attr)
      {
        // Attributes without labels will not be selectable.
        if ($attr->hasFlag(AF_NO_LABEL) ||  $attr->hasFlag(AF_BLANK_LABEL))
          continue;

        // Hidden attributes will not be selectable
        if ($attr->hasFlag(AF_HIDE) || $attr->hasFlag(AF_HIDE_EDIT))
          continue;

        // You cannot give multiple records a value for a field that should be unique.
        $atkselector = $this->getSelector();
        if (isset($atkselector) && count($atkselector)>1 && $attr->hasFlag(AF_UNIQUE))
          continue;

        // You cannot update readonly fields
        if ($attr->hasFlag(AF_READONLY) || $attr->hasFlag(AF_READONLY_EDIT))
          continue;

        // We do not support manytomany relations (for now...).
        if (is_a($attr, 'atkmanytomanyrelation'))
          continue;

        $supported[] = $attr;
      }

      return $supported;
    }

    /**
     * Get a list of attributenames that we can ignore (i.e. when calling
     * validate() on a node.)
     *
     * @param String $selectedattributename
     * @return Array A list of attributenames.
     */
    function getIgnoreList($selectedattributename)
    {
      $attributes = $this->m_node->getAttributes();

      $attribnames = array();
      foreach ($attributes as $attr)
      {
        $attribnames[] = $attr->fieldName();
      }

      // remove the selected attribute from all the available attributes,
      // and we have a list of attributes that we can ignore.
      return array_diff($attribnames, array($selectedattributename));
    }


    /**
     * Returns the form start.
     *
     * @return string Html form start
     */
    function getFormStart()
    {
      $controller = &atkcontroller::getInstance();
      $controller->setNode($this->m_node);

      $formstart = '<form id="dialogform" name="dialogform" action="'.$controller->getPhpFile().'?'.SID.'" method="post">';
      $atkselector = $this->getSelector();

      if (isset($atkselector))
      {
        foreach ($atkselector as &$selector)
        {
          $formstart.= '<input type="hidden" id="atkselector[]" name="atkselector[]" value="'.$selector.'">';
        }
      }

      return $formstart;
    }

    /**
     * Handle refresh values
     *
     */
    function handleRefreshValuesField()
    {
      $page = &$this->getPage();

      atkimport('atk.utils.atkjson');
      // get selected attribute
      $selectedattribute = $this->m_node->m_postvars["attributename"];

      $field = $this->_getDropDownValues($selectedattribute);

      $keys = implode(',', array_keys($this->m_node->m_postvars));
      $content = "<script type=\"text/javascript\">$('selectedvaluediv').innerHTML = ".atkJSON::encode($field)."</script>";

      $page->addContent($content);
    }

    /**
     * Returns the process URL.
     *
     * @return string process URL
     */
    function getProcessUrl()
    {
      if ($this->m_processUrl != null)
      {
        return $this->m_processUrl;
      }
      else
      {
        return partial_url($this->m_masterNode->atkNodeType(), 'attributeedit', 'process');
      }
    }

    /**
     * Override the default process URL.
     *
     * @param string $url process URL
     */
    function setProcessUrl($url)
    {
      $this->m_processUrl = $url;
    }
  }
?>
Back to Top