Jump to content

What is this Function doing???


cvarick

Recommended Posts

To anyone that has a few minutes to waste... I recently started working on a platform which was created by another programmer. I am unable to figure out what he was really doing here. Would be awesome if someone could help me out and give me their opinion on the matter. Maybe do some commenting for me.  :P

 

further detail: the function is being called and "performed" on a sql query using cakpPhp framework.

 

Thanks in advance! See below:

public function myGenerateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null, $separator = '')
    {
        if ($keyPath == null && $valuePath == null && $this->hasField($this->displayField)) {
          $fields = array($this->primaryKey, $this->displayField);
        } else {
          $fields = null;
        }
        $recursive = $this->recursive;
        $result = $this->findAll($conditions, $fields, $order, $limit);
        $this->recursive = $recursive;

        if(!$result) {
          return false;
        }

        if ($keyPath == null) {
          $keyPath = '{n}.' . $this->name . '.' . $this->primaryKey;
        }

        if ($valuePath == null) {
          $valuePath = '{n}.' . $this->name . '.' . $this->displayField;
        }

        // extract the keys as normal
        $keys = Set::extract($result, $keyPath);


        $finalVals = array();

        // explode the value path by our delimiter
        $tmpVals = explode('#', $valuePath);

        $i = 0;
        foreach($tmpVals as $tmpVal) {
          // extract the data for each path sibling
          $vals = Set::extract($result, $tmpVal);

          // and insert it at the appropriate position in our final value array
          // use the separator when we need to append the values
          foreach($vals as $key => $value) {
            if(!array_key_exists($key, $finalVals)) {
              $finalVals[$key] = $value;
            } else {
              $finalVals[$key] .= $separator.$value;
            }
          }
          $i++;
        }


        if (!empty($keys) && !empty($finalVals)) {
          $out = array();
          $return = array_combine($keys, $finalVals);
          return $return;
        }
        return null;
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.